forked from microsoft/AL-Go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
_**Issue**_: Currently, when building partially (e.g. PR Build), the baseline artifacts are fetched from a CICD workflow run on the fly. This could lead to discrepancies: one build job downloading artifacts from one CICD run, while the next one: from another CICD run (which completed in-between the two build jobs in the PR Build). _**Solution**_: Determine the baseline CICD workflow once per PR Build and use artifacts from it in all build jobs. Test run partial build: https://github.com/mazhelez/BCApps/actions/runs/7165434696?pr=2 Test run full build: https://github.com/mazhelez/BCApps/actions/runs/7165432643?pr=5 TODO: - [x] Tests - [x] Propagate workflow changes to all templates - [x] Smarter check to determine the baseline workflow ID only when needed (non-full build) --------- Co-authored-by: Alexander Holstrup <[email protected]> Co-authored-by: Freddy Kristiansen <[email protected]>
- Loading branch information
1 parent
3b3aa14
commit e9c7f5e
Showing
18 changed files
with
524 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
Actions/DetermineBaselineWorkflowRun/DetermineBaselineWorkflowRun.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
Param( | ||
[Parameter(Mandatory = $true)] | ||
[string] $repository, | ||
[Parameter(Mandatory = $true)] | ||
[string] $branch, | ||
[Parameter(Mandatory = $true)] | ||
[string] $token | ||
) | ||
|
||
Import-Module (Join-Path $PSScriptRoot '..\Github-Helper.psm1' -Resolve) | ||
|
||
<# | ||
Checks if all build jobs in a workflow run completed successfully. | ||
#> | ||
function CheckBuildJobsInWorkflowRun { | ||
Param( | ||
[Parameter(Mandatory = $true)] | ||
[string] $token, | ||
[Parameter(Mandatory = $true)] | ||
[string] $repository, | ||
[Parameter(Mandatory = $true)] | ||
[string] $workflowRunId | ||
) | ||
|
||
$headers = GetHeader -token $token | ||
$per_page = 100 | ||
$page = 1 | ||
|
||
$allSuccessful = $true | ||
$anySuccessful = $false | ||
|
||
while($true) { | ||
$jobsURI = "https://api.github.com/repos/$repository/actions/runs/$workflowRunId/jobs?per_page=$per_page&page=$page" | ||
Write-Host "- $jobsURI" | ||
$workflowJobs = InvokeWebRequest -Headers $headers -Uri $jobsURI | ConvertFrom-Json | ||
|
||
if($workflowJobs.jobs.Count -eq 0) { | ||
# No more jobs, breaking out of the loop | ||
break | ||
} | ||
|
||
$buildJobs = @($workflowJobs.jobs | Where-Object { $_.name.StartsWith('Build ') }) | ||
|
||
if($buildJobs.conclusion -eq 'success') { | ||
$anySuccessful = $true | ||
} | ||
|
||
if($buildJobs.conclusion -ne 'success') { | ||
# If there is a build job that is not successful, there is not need to check further | ||
$allSuccessful = $false | ||
break | ||
} | ||
|
||
$page += 1 | ||
} | ||
|
||
return ($allSuccessful -and $anySuccessful) | ||
} | ||
|
||
<# | ||
Gets the last successful CICD run ID for the specified repository and branch. | ||
Successful CICD runs are those that have a workflow run named ' CI/CD' and successfully built all the projects. | ||
If no successful CICD run is found, 0 is returned. | ||
#> | ||
function FindLatestSuccessfulCICDRun { | ||
Param( | ||
[Parameter(Mandatory = $true)] | ||
[string] $repository, | ||
[Parameter(Mandatory = $true)] | ||
[string] $branch, | ||
[Parameter(Mandatory = $true)] | ||
[string] $token | ||
) | ||
|
||
$headers = GetHeader -token $token | ||
$lastSuccessfulCICDRun = 0 | ||
$per_page = 100 | ||
$page = 1 | ||
|
||
Write-Host "Finding latest successful CICD run for branch $branch in repository $repository" | ||
|
||
# Get the latest CICD workflow run | ||
while($true) { | ||
$runsURI = "https://api.github.com/repos/$repository/actions/runs?per_page=$per_page&page=$page&exclude_pull_requests=true&status=completed&branch=$branch" | ||
Write-Host "- $runsURI" | ||
$workflowRuns = InvokeWebRequest -Headers $headers -Uri $runsURI | ConvertFrom-Json | ||
|
||
if($workflowRuns.workflow_runs.Count -eq 0) { | ||
# No more workflow runs, breaking out of the loop | ||
break | ||
} | ||
|
||
$CICDRuns = @($workflowRuns.workflow_runs | Where-Object { $_.name -eq ' CI/CD' }) | ||
|
||
foreach($CICDRun in $CICDRuns) { | ||
if($CICDRun.conclusion -eq 'success') { | ||
# CICD run is successful | ||
$lastSuccessfulCICDRun = $CICDRun.id | ||
break | ||
} | ||
|
||
# CICD run is considered successful if all build jobs were successful | ||
$areBuildJobsSuccessful = CheckBuildJobsInWorkflowRun -workflowRunId $($CICDRun.id) -token $token -repository $repository | ||
|
||
if($areBuildJobsSuccessful) { | ||
$lastSuccessfulCICDRun = $CICDRun.id | ||
Write-Host "Found last successful CICD run: $($lastSuccessfulCICDRun), from $($CICDRun.created_at)" | ||
break | ||
} | ||
|
||
Write-Host "CICD run $($CICDRun.id) is not successful. Skipping." | ||
} | ||
|
||
if($lastSuccessfulCICDRun -ne 0) { | ||
break | ||
} | ||
|
||
$page += 1 | ||
} | ||
|
||
if($lastSuccessfulCICDRun -ne 0) { | ||
Write-Host "Last successful CICD run for branch $branch in repository $repository is $lastSuccessfulCICDRun" | ||
} else { | ||
Write-Host "No successful CICD run found for branch $branch in repository $repository" | ||
} | ||
|
||
return $lastSuccessfulCICDRun | ||
} | ||
|
||
$workflowRunID = FindLatestSuccessfulCICDRun -token $token -repository $repository -branch $branch | ||
|
||
# Set output variables | ||
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "WorkflowRunID=$workflowRunID" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Determine Baseline Workflow Run | ||
Finds the latest CICD workflow run that completed and built all the AL-Go projects successfully. | ||
This workflow run is to be used as a baseline for all the build jobs in the current workflow run in case incremental build is required. | ||
|
||
## INPUT | ||
|
||
### ENV variables | ||
none | ||
|
||
### Parameters | ||
| Name | Required | Description | Default value | | ||
| :-- | :-: | :-- | :-- | | ||
| shell | | The shell (powershell or pwsh) in which the PowerShell script in this action should run | powershell | | ||
|
||
## OUTPUT | ||
|
||
### ENV variables | ||
none | ||
|
||
### OUTPUT variables | ||
| Name | Description | | ||
| :-- | :-- | | ||
| BaselineWorkflowRunId | The workflow run ID to use as a baseline. 0, if no baseline CICD was found. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Determine Baseline Workflow Run | ||
author: Microsoft Corporation | ||
inputs: | ||
shell: | ||
description: Shell in which you want to run the action (powershell or pwsh) | ||
required: false | ||
default: powershell | ||
outputs: | ||
BaselineWorkflowRunId: | ||
description: The ID of the baseline workflow run | ||
value: ${{ steps.determineBaselineWorkflowRun.outputs.WorkflowRunID }} | ||
runs: | ||
using: composite | ||
steps: | ||
- name: Determine Projects to Build | ||
shell: ${{ inputs.shell }} | ||
id: determineBaselineWorkflowRun | ||
env: | ||
_gitHubToken: ${{ github.token }} | ||
_branch: ${{ github.base_ref }} | ||
_repository: ${{ github.repository }} | ||
run: | | ||
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0 | ||
try { | ||
${{ github.action_path }}/DetermineBaselineWorkflowRun.ps1 -branch $env:_branch -repository $env:_repository -token $env:_gitHubToken | ||
} | ||
catch { | ||
Write-Host "::ERROR::Unexpected error when running action. Error Message: $($_.Exception.Message.Replace("`r",'').Replace("`n",' ')), StackTrace: $($_.ScriptStackTrace.Replace("`r",'').Replace("`n",' <- '))"; | ||
exit 1 | ||
} | ||
branding: | ||
icon: terminal | ||
color: blue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.