Skip to content

Commit

Permalink
Automate release process (#1064)
Browse files Browse the repository at this point in the history
Automate:
- The generation of modifying release notes with the version number (and
revert)
- Creation of a release in AL-Go repo

Adds:
- Check that a successful end 2 end test was run on the same commit you
are releasing

---------

Co-authored-by: freddydk <freddydk@users.noreply.github.com>
Co-authored-by: Alexander Holstrup <117829001+aholstrup1@users.noreply.github.com>
  • Loading branch information
3 people committed May 2, 2024
1 parent bb22cfd commit 92365fb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
75 changes: 71 additions & 4 deletions .github/workflows/Deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ on:
description: Push directly to the target branch. If not set, a PR will be created.
required: false
default: false
requireEndToEndTests:
type: boolean
description: Require successful end 2 end tests before deploying
required: false
default: true
createRelease:
type: boolean
description: Create a release in this repository
required: false
default: true
defaultBcContainerHelperVersion:
description: 'Which version of BcContainerHelper to use? (latest, preview, private, a specific version number or a direct download URL like https://github.com/freddydk/navcontainerhelper/archive/master.zip). Leave empty to use latest (or preview for preview branches)'
required: false
Expand All @@ -28,24 +38,46 @@ defaults:
shell: pwsh

jobs:
CheckEndToEnd:
runs-on: [ ubuntu-latest ]
steps:
- name: Check successful end 2 end tests have run
if: github.repository_owner == 'microsoft' && github.event.inputs.requireEndToEndTests == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
$errorActionPreference = "Stop"
$end2endRuns = (gh api /repos/$($env:GITHUB_REPOSITORY)/actions/runs?per_page=100 | ConvertFrom-Json).workflow_runs | Where-Object { $_.name -eq 'End 2 End Tests' }
$latestSha = (gh api /repos/$($env:GITHUB_REPOSITORY)/commits/main | ConvertFrom-Json).sha
$latestRun = $end2endruns | Where-Object { $_.head_sha -eq $latestSha }
if (!$latestRun) {
throw "No End 2 End Tests run found for the latest commit on main"
}
if ($latestRun.status -ne 'completed') {
throw "End 2 End Tests run for the latest commit on main is not completed"
}
if ($latestRun.conclusion -ne 'success') {
throw "End 2 End Tests run for the latest commit on main did not succeed"
}
Deploy:
runs-on: [ ubuntu-latest ]
needs: [ CheckEndToEnd ]
environment: Production
steps:
- name: Validate Deployment
if: github.repository_owner == 'microsoft'
env:
GH_TOKEN: ${{ github.token }}
branch: ${{ github.event.inputs.branch }}
repository: ${{ github.repository }}
runId: ${{ github.run_id }}
run: |
$errorActionPreference = "Stop"
if ($env:branch -eq 'preview') {
Write-Host "Deploying to preview branch. No validation required"
} else {
$approval = gh api /repos/$($env:repository)/actions/runs/$($env:runId)/approvals | ConvertFrom-Json
$run = gh api /repos/$($env:repository)/actions/runs/$($env:runId) | ConvertFrom-Json
$approval = gh api /repos/$($env:GITHUB_REPOSITORY)/actions/runs/$($env:runId)/approvals | ConvertFrom-Json
$run = gh api /repos/$($env:GITHUB_REPOSITORY)/actions/runs/$($env:runId) | ConvertFrom-Json
if ($approval.user.login -eq $run.actor.login) {
throw "You cannot approve your own deployment"
Expand All @@ -67,7 +99,7 @@ jobs:
if (!$token) {
throw "In order to run the Deploy workflow, you need a Secret called OrgPAT containing a valid Personal Access Token"
}
$githubOwner = "$ENV:GITHUB_REPOSITORY_OWNER"
$githubOwner = "$env:GITHUB_REPOSITORY_OWNER"
if ("$env:defaultBcContainerHelperVersion" -eq "") {
if ($env:branch -eq 'preview') {
$env:defaultBcContainerHelperVersion = 'preview'
Expand All @@ -90,3 +122,38 @@ jobs:
Write-Host "::Error::Error deploying repositories. The error was $($_.Exception.Message)"
exit 1
}
- name: Calculate Release Notes
if: github.repository_owner == 'microsoft' && github.event.inputs.createRelease == 'true'
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
$releaseNotesFile = Join-Path $env:GITHUB_WORKSPACE "RELEASENOTES.md"
$releaseNotes = (Get-Content -Encoding utf8 -Path $releaseNotesFile) -join "`n"
$lastVersion = $releaseNotes.indexof("`n## ")
$releaseNotes = $releaseNotes.Substring(0, $lastVersion)
Add-Content -encoding UTF8 -Path $env:GITHUB_ENV -Value "ReleaseNotes=$([Uri]::EscapeDataString($releaseNotes))"
- name: Create release
if: github.repository_owner == 'microsoft' && github.event.inputs.createRelease == 'true'
uses: actions/github-script@v7
id: createrelease
env:
branch: ${{ github.event.inputs.branch }}
bodyMD: ${{ env.ReleaseNotes }}
with:
github-token: ${{ secrets.OrgPAT }}
script: |
var bodyMD = process.env.bodyMD
const createReleaseResponse = await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: '${{ env.branch }}',
name: '${{ env.branch }}',
body: decodeURIComponent(bodyMD),
draft: false,
prerelease: false
});
const {
data: { id: releaseId, html_url: htmlUrl, upload_url: uploadUrl }
} = createReleaseResponse;
core.setOutput('releaseId', releaseId);
10 changes: 9 additions & 1 deletion Internal/Deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,15 @@ try {
[System.IO.File]::WriteAllText($dstFile, "$($lines -join "`n")`n")
}
if (Test-Path -Path (Join-Path '.' '.github') -PathType Container) {
Copy-Item -Path (Join-Path $baseRepoPath "RELEASENOTES.md") -Destination (Join-Path "./.github" "RELEASENOTES.copy.md") -Force
$releaseNotesFile = Join-Path $baseRepoPath "RELEASENOTES.md"
$releaseNotes = (Get-Content -Encoding utf8 -Path $releaseNotesFile) -join "`n"
if ($config.branch -eq 'preview') {
$releaseNotes = "## $($config.branch)`n`nNote that when using the preview version of AL-Go for GitHub, we recommend you Update your AL-Go system files, as soon as possible when informed that an update is available.`n`n$reserveReleaseNotes"
}
else {
$releaseNotes = "## $($config.branch)`n`n$releaseNotes"
}
Set-Content -Path (Join-Path "./.github" "RELEASENOTES.copy.md") -Value $releaseNotes -Encoding utf8
}
PushChanges -BaseBranch $branch -CommitMessage "Deploying AL-Go from $algoBranch ($srcSHA) to $branch" -DirectCommit $directCommit

Expand Down
4 changes: 0 additions & 4 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
## Preview

Note that when using the preview version of AL-Go for GitHub, we recommend you Update your AL-Go system files, as soon as possible when informed that an update is available.

### Issues

- Issue 1019 CI/CD Workflow still being scheduled after it was disabled
Expand Down

0 comments on commit 92365fb

Please sign in to comment.