Skip to content

Commit

Permalink
Fix artifacts names for CalculateArtifactNames and DownloadProjectDep…
Browse files Browse the repository at this point in the history
…endencies (#756)

_**Issue 1**_:
_CalculateArtifactNames_ uses only `github.ref_name` as branch name when
constructing artifacts names. However, in the case of PR build, the
value is the merge branch, e.g., "123/merge".
Solution:
_**Solution**_: Remove the _branch_ input from _CalculateArtifactNames_
action and let it determine the proper branch on the fly, based on
`$env:GITHUB_HEAD_REF`
([doc](https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables)).

_**Issue 2**_:
_DownloadProjectDependencies_ uses the wrong branches as head and target
branches when running from a PR Build.
Solution:
_**Solution**_: Utilize `$env:GITHUB_HEAD_REF` and `GITHUB_BASE_REF`
([doc](https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables)).
  • Loading branch information
mazhelez authored Oct 5, 2023
1 parent 9042092 commit 6bbfb2b
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 28 deletions.
8 changes: 6 additions & 2 deletions Actions/CalculateArtifactNames/CalculateArtifactNames.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
[string] $project,
[Parameter(HelpMessage = "Build mode used when building the artifacts", Mandatory = $true)]
[string] $buildMode,
[Parameter(HelpMessage = "Name of the branch the workflow is running on", Mandatory = $true)]
[string] $branchName,
[Parameter(HelpMessage = "Suffix to add to the artifacts names", Mandatory = $false)]
[string] $suffix
)
Expand All @@ -20,6 +18,12 @@ if ($project -eq ".") {
$project = $settings.repoName
}

$branchName = $ENV:GITHUB_HEAD_REF
# $ENV:GITHUB_HEAD_REF is specified only for pull requests, so if it is not specified, use GITHUB_REF_NAME
if (!$branchName) {
$branchName = $ENV:GITHUB_REF_NAME
}

$branchName = $branchName.Replace('\', '_').Replace('/', '_')
$projectName = $project.Replace('\', '_').Replace('/', '_')

Expand Down
1 change: 0 additions & 1 deletion Actions/CalculateArtifactNames/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Calculate Artifact Names for AL-Go workflows
| shell | | The shell (powershell or pwsh) in which the PowerShell script in this action should run | powershell |
| project | Yes | Name of the built project or . if the repository is setup for single project | |
| buildMode | Yes |Build mode used when building the artifacts | |
| branchName | Yes | Name of the branch the workflow is running on | |
| suffix | | A suffix to add to artifacts names. **Note:** if a suffix is specified, the current date will be added extra | Build version |

## OUTPUT
Expand Down
6 changes: 1 addition & 5 deletions Actions/CalculateArtifactNames/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ inputs:
buildMode:
description: Build mode used when building the artifacts
required: true
branchName:
description: Name of the branch the workflow is running on
required: true
suffix:
description: Suffix to add to the artifacts names
required: false
Expand Down Expand Up @@ -58,12 +55,11 @@ runs:
env:
_project: ${{ inputs.project }}
_buildMode: ${{ inputs.buildMode }}
_branchName: ${{ inputs.branchName }}
_suffix: ${{ inputs.suffix }}
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
try {
${{ github.action_path }}/CalculateArtifactNames.ps1 -project $ENV:_project -buildMode $ENV:_buildMode -branchName $ENV:_branchName -suffix $ENV:_suffix
${{ github.action_path }}/CalculateArtifactNames.ps1 -project $ENV:_project -buildMode $ENV:_buildMode -suffix $ENV:_suffix
}
catch {
Write-Host "::ERROR::Unexpected error when running action. Error Message: $($_.Exception.Message.Replace("`r",'').Replace("`n",' ')), StackTrace: $($_.ScriptStackTrace.Replace("`r",'').Replace("`n",' <- '))";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ function DownloadDependenciesFromCurrentBuild($baseFolder, $project, $projectsDe
$dependencyBuildMode = 'Default';
}

$currentBranch = $ENV:GITHUB_REF_NAME
$headBranch = $ENV:GITHUB_HEAD_REF
# $ENV:GITHUB_HEAD_REF is specified only for pull requests, so if it is not specified, use GITHUB_REF_NAME
if (!$headBranch) {
$headBranch = $ENV:GITHUB_REF_NAME
}

$baseBranch = $ENV:GITHUB_BASE_REF_NAME
# $ENV:GITHUB_BASE_REF_NAME is specified only for pull requests, so if it is not specified, use the current branch
$baseBranch = $ENV:GITHUB_BASE_REF
# $ENV:GITHUB_BASE_REF is specified only for pull requests, so if it is not specified, use GITHUB_REF_NAME
if (!$baseBranch) {
$baseBranch = $currentBranch
$baseBranch = $ENV:GITHUB_REF_NAME
}

$dependeciesProbingPaths += @(@{
Expand All @@ -54,7 +58,7 @@ function DownloadDependenciesFromCurrentBuild($baseFolder, $project, $projectsDe
"buildMode" = $dependencyBuildMode
"projects" = $dependencyProject
"repo" = "$ENV:GITHUB_SERVER_URL/$ENV:GITHUB_REPOSITORY"
"branch" = $currentBranch
"branch" = $headBranch
"baseBranch" = $baseBranch
"authTokenSecret" = $token
})
Expand Down
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ AL-Go for GitHub allows you to build and test using insider builds without any e
- Issue 739 Workflow specific KeyVault settings doesn't work for localDevEnv
- Using self-hosted runners while using Azure KeyVault for secrets or signing might fail with C:\Modules doesn't exist
- PullRequestHandler wasn't triggered if only .md files where changes. This lead to PRs which couldn't be merged if a PR status check was mandatory.
- Artifacts names for PR Builds were using the merge branch instead of the head branch.

### New Settings
- `enableExternalRulesets`: set this setting to true if you want to allow AL-Go to automatically download external references in rulesets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ jobs:
shell: ${{ inputs.shell }}
project: ${{ inputs.project }}
buildMode: ${{ inputs.buildMode }}
branchName: ${{ github.ref_name }}
suffix: ${{ inputs.artifactsNameSuffix }}

- name: Upload thisbuild artifacts - apps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ jobs:
shell: ${{ inputs.shell }}
project: ${{ inputs.project }}
buildMode: ${{ inputs.buildMode }}
branchName: ${{ github.ref_name }}
suffix: ${{ inputs.artifactsNameSuffix }}

- name: Upload thisbuild artifacts - apps
Expand Down
21 changes: 8 additions & 13 deletions Tests/CalculateArtifactNames.Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ Describe 'CalculateArtifactNames Action Tests' {

It 'should include buildmode name in artifact name if buildmode is not default' {
$buildMode = "Clean"
$branchName = "main"
$env:GITHUB_HEAD_REF = "main"
& $scriptPath `
-project $project `
-buildMode $buildMode `
-branchName $branchName
-buildMode $buildMode

$generatedOutPut = Get-Content $env:GITHUB_OUTPUT -Encoding UTF8
$generatedOutPut | Should -Contain "ThisBuildAppsArtifactsName=thisbuild-ALGOProject-CleanApps"
Expand All @@ -50,11 +49,10 @@ Describe 'CalculateArtifactNames Action Tests' {

It 'should not include buildmode name in artifact name if buildmode is default' {
$buildMode = "Default"
$branchName = "main"
$env:GITHUB_HEAD_REF = "main"
& $scriptPath `
-project $project `
-buildMode $buildMode `
-branchName $branchName
-buildMode $buildMode

$generatedOutPut = Get-Content $env:GITHUB_OUTPUT -Encoding UTF8
$generatedOutPut | Should -Contain "ThisBuildAppsArtifactsName=thisbuild-ALGOProject-Apps"
Expand All @@ -70,11 +68,10 @@ Describe 'CalculateArtifactNames Action Tests' {

It 'should escape slashes and backslashes in artifact name' {
$buildMode = "Default"
$branchName = "releases/1.0"
$env:GITHUB_HEAD_REF = "releases/1.0"
& $scriptPath `
-project $project `
-buildMode $buildMode `
-branchName $branchName
-buildMode $buildMode

$generatedOutPut = Get-Content $env:GITHUB_OUTPUT -Encoding UTF8
$generatedOutPut | Should -Contain "ThisBuildAppsArtifactsName=thisbuild-ALGOProject-Apps"
Expand All @@ -90,12 +87,11 @@ Describe 'CalculateArtifactNames Action Tests' {

It 'should use the specified suffix if provided' {
$buildMode = "Default"
$branchName = "releases/1.0"
$env:GITHUB_HEAD_REF = "releases/1.0"
$suffix = "Current"
& $scriptPath `
-project $project `
-buildMode $buildMode `
-branchName $branchName `
-suffix $suffix

# In rare cases, when this test is run at the end of the day, the date will change between the time the script is run and the time the test is run.
Expand All @@ -117,12 +113,11 @@ Describe 'CalculateArtifactNames Action Tests' {
It 'handles special characters in project name' {
$project = "ALGOProject_øåæ"
$buildMode = "Default"
$branchName = "releases/1.0"
$env:GITHUB_HEAD_REF = "releases/1.0"
$suffix = "Current"
& $scriptPath `
-project $project `
-buildMode $buildMode `
-branchName $branchName `
-suffix $suffix

# In rare cases, when this test is run at the end of the day, the date will change between the time the script is run and the time the test is run.
Expand Down

0 comments on commit 6bbfb2b

Please sign in to comment.