Skip to content

Commit

Permalink
New release publishing process
Browse files Browse the repository at this point in the history
  • Loading branch information
markdomansky committed Jun 20, 2022
1 parent fe9fe6e commit 7395b14
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,4 @@ __pycache__/
/WebJEA/content
/Build/temp
/Build/Package
/Build/publish.json
204 changes: 204 additions & 0 deletions Build/Publish-GithubRelease.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<#
.SYNOPSIS
Publish a new release on a Github repository
.FUNCTIONALITY
CI/CD
.EXAMPLE
Publish-GithubRelease -AccessToken $mySecretToken -TagName "v1.0"
Create a new release for the tag "v1.0".
The name of the repository is assumed to be the same as the BHProjectName.
.EXAMPLE
Publish-GithubRelease -AccessToken $mySecretToken -TagName "v0.1" -Name "Beta Version 0.1" -PreRelease
Create a new pre-release for the tag "v0.1".
.EXAMPLE
Publish-GithubRelease -AccessToken $mySecretToken -TagName "v1.0" -Draft
Create a draft for a release on tag "v1.0".
.EXAMPLE
$release = @{
AccessToken = "00000000000000000000000"
TagName = "v1.0"
Name = "Version 1.0"
ReleaseText = "First version of my cool thing"
Draft = $true
PreRelease = $false
RepositoryName = "MyGithubRepository"
}
Publish-GithubRelease @release
Create a new draft release by using splatting (more info at "about_splatting").
.LINK
https://developer.github.com/v3/repos/releases/
.LINK
https://blog.github.com/2013-05-16-personal-api-tokens/
#>
[CmdletBinding()]
param(
# Personal API Token for authentication
#
# This sha string must be generated by a user that has push access to
# the repository.
# More information can be found at:
# https://blog.github.com/2013-05-16-personal-api-tokens/
[Parameter( Mandatory )]
[ValidateNotNullOrEmpty()]
[String]
$AccessToken,

# Name of the Github user or organization hosting the repository
[Parameter( Mandatory )]
[ValidateNotNullOrEmpty()]
[Alias('Owner')]
[String]
$RepositoryOwner,

# Name of the Github repository
#
# Default: $env:BHProjectName
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]
$RepositoryName,

# Name of the tag
[Parameter( Mandatory )]
[ValidateNotNullOrEmpty()]
[String]
$TagName,

# Specifies the commitish value that determines where the Git tag is
# created from. Can be any branch or commit SHA.
#
# Unused if the Git tag already exists.
# Default: the repository's default branch (usually master).
[Alias('Commit')]
[String]
$TargetCommit,

# Name of the release
[String]
$Name,

# Text describing the contents of the tag.
[Alias('Body')]
[String]
$ReleaseText,

# Create a draft (unpublished) release
[Switch]
$Draft,

# Identify the release as a prerelease
[Switch]
$PreRelease,

# Path to the artifact to upload to the release
[Parameter( ValueFromPipeline, ValueFromPipelineByPropertyName )]
[ValidateScript(
{
if (-not (Test-Path $_ -PathType Leaf))
{
$exception = ([System.ArgumentException]'File not found')
$errorId = 'ParameterValue.FileNotFound'
$errorCategory = 'ObjectNotFound'
$errorTarget = $_
$errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget
$errorItem.ErrorDetails = "No file could be found with the provided path '$_'."
$PSCmdlet.ThrowTerminatingError($errorItem)
}
return $true
}
)]
[Alias('File', 'FullName', 'Path')]
[String[]]
$Artifact
)

begin
{
$body = @{ 'tag_name' = $TagName }
if ($PSBoundParameters.ContainsKey('TargetCommit'))
{
$body['target_commitish'] = $TargetCommit
}
if ($PSBoundParameters.ContainsKey('Name'))
{
$body['name'] = $Name
}
if ($PSBoundParameters.ContainsKey('ReleaseText'))
{
$body['body'] = $ReleaseText
}
if ($PSBoundParameters.ContainsKey('Draft'))
{
$body['draft'] = $true
}
if ($PSBoundParameters.ContainsKey('PreRelease'))
{
$body['prerelease'] = $true
}

$releaseParams = @{
Uri = 'https://api.github.com/repos/{0}/{1}/releases' -f $RepositoryOwner, $RepositoryName
Method = 'POST'
Headers = @{
Authorization = 'Basic ' + [Convert]::ToBase64String(
[Text.Encoding]::ASCII.GetBytes($AccessToken + ':x-oauth-basic')
)
}
ContentType = 'application/json'
Body = $body | ConvertTo-Json
ErrorAction = 'Stop'
}
try
{
$release = Invoke-RestMethod @releaseParams
$release
}
catch
{
throw $_
}
}

process
{
if ($Artifact)
{
foreach ($file in (Get-Item $Artifact))
{
$body = [System.IO.File]::ReadAllBytes($file.FullName)
$uri = $release.upload_url -replace '\{\?name,label\}', "?name=$($file.Name)"

$assetParams = @{
Uri = $uri
Method = 'POST'
Headers = @{
Authorization = 'Basic ' + [Convert]::ToBase64String(
[Text.Encoding]::ASCII.GetBytes($AccessToken + ':x-oauth-basic')
)
}
ContentType = 'application/octet-stream'
Body = $body
}

try
{
Invoke-RestMethod @assetParams
}
catch
{
throw $_
}
}
}
}
3 changes: 3 additions & 0 deletions Build/Publish.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$ErrorActionPreference = 'Stop'

Invoke-psake -buildFile Publish.psake.ps1
40 changes: 40 additions & 0 deletions Build/Publish.psake.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Task Init {
$script:releasepath = "$psscriptroot\release"
$script:outpath = resolve-path ("$psscriptroot\..\release")
}

Task GetVersion -depends Init {
$dllfile = "$releasepath\site\bin\webjea.dll"
$script:curver = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($dllfile).FileVersion
Write-Host "Release Version: $script:curver"
}

Task Compress -depends GetVersion {
$script:outfile = "$script:outpath\webjea-$script:curver.zip"
if ((Test-Path $script:outfile)) { Remove-Item $script:outfile }
Write-Host "Output File; $script:outfile"

Write-Host 'Archive temp directory'
Push-Location $script:releasepath
Compress-Archive -Path .\* -DestinationPath $script:outfile
pop-location
}

Task Tag -depends Compress {
push-location $psscriptroot
#tag
& git tag -a "$script:curver" -m "$script:curver"

#push
& git push origin "$script:curver"

pop-location
}

Task Publish -depends Tag {
$token = gc $psscriptroot\publish.json | convertfrom-json | select -expand accesstoken
.\Publish-GithubRelease.ps1 -AccessToken $token -repositoryowner markdomansky -repositoryname webjea -tagname $script:curver -name $script:curver -artifact $script:outfile
}


Task Default -depends Publish
Binary file removed Build/zip.exe
Binary file not shown.

0 comments on commit 7395b14

Please sign in to comment.