• GitHub Actions default environment variable gotcha

    I was trying to use a GitHub Action default environment variable in a workflow today, and was surprised to find that it didn’t work as expected. The workflow contained a step similar to this:

    jobs:
      build:
        steps:
            - name: Output job info
              run: echo "Job '${{ env.GITHUB_JOB }}'"
    
    

    According to the documentation, GITHUB_JOB should contain the job_id of the current job. In this workflow, the job_id should be “build” (as that’s the name of the current job).

    But when I ran the workflow all I got was Job ''. I found a couple of posts that seem to hint at the answer.

    GitHub sets default environment variables that are available to every step in a workflow run.

    In short, the documentation seems to be a little misleading here. The ${{ env.xxxx }} notation works find for env: context variables that you’ve defined yourself, but not for the default environment variables. Either refer to the variable in the way appropriate for the script language (if you’re using run), or if you’re setting a property for an action you can use a different approach. eg.

    jobs:
      build:
        steps:
            - name: Output job info
              run: echo "Job '$'"
    
    

  • Azure DevOps PowerShell Scripts - List all release definitions and dependant builds

    Azure Pipelines logo It’s easy to open a specific release definition in the Azure DevOps web UI to find out what builds it references, but you can’t do the opposite - open a build to see which release definitions rely on it.

    Last time we got a list of Azure Pipelines Release Definitions. Let’s go the next step and match up the builds that are being consumed by each release definition. With this list, we can now filter down to a build and find which releases use it.

    See Personal access tokens for instructions on how to create the personal access token.

    param (
        [string] $organisation,
        [string] $personalAccessToken
    )
    
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
    $headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    $result = Invoke-RestMethod -Uri "https://dev.azure.com/$organisation/_apis/projects?api-version=6.0" -Method Get -Headers $headers
    
    $projectNames = $result.value.name
    
    $projectNames | ForEach-Object {
        $project = $_
    
        $result = Invoke-RestMethod -Uri "https://vsrm.dev.azure.com/$organisation/$project/_apis/release/definitions?api-version=6.1-preview.4&`$expand=artifacts" -Method Get -Headers $headers
    
        $result.value | Select-Object name, 
            @{ Name = "Url"; Expression = { $_._links.web.href }},
            @{ Name="Artifacts"; Expression= { $_.artifacts.alias }}, 
            @{ Name="artifactSourceDefinitionUrl"; Expression= { $_.artifacts.definitionReference.artifactSourceDefinitionUrl.id }}
    }
    

    This script also makes use of the Definitions - List REST API. This time we’re requesting the artifacts property to be included in the results. It is this property that contains information about builds and their artifacts that are being consumed by the release definition.

  • Azure DevOps PowerShell Scripts - List all release definitions

    Azure Pipelines logo If you want to list all the Azure Pipelines Release Definitions for all projects in an Azure DevOps organisation, this script will return a list of their names, the date of the latest release and a link to view the definition within the web UI.

    Note that Release definitions are part of the “classic” release pipelines. If you’re using YAML-based deployments then those will be viewable via the Pipelines script.

    See Personal access tokens for instructions on how to create the personal access token.

    param (
        [string] $organisation,
        [string] $personalAccessToken
    )
    
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
    $headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    $result = Invoke-RestMethod -Uri "https://dev.azure.com/$organisation/_apis/projects?api-version=6.0" -Method Get -Headers $headers
    
    $projectNames = $result.value.name
    
    $projectNames | ForEach-Object {
        $project = $_
    
        $result = Invoke-RestMethod -Uri "https://vsrm.dev.azure.com/$organisation/$project/_apis/release/definitions?api-version=6.1-preview.4&`$expand=lastRelease" -Method Get -Headers $headers
    
        $result.value | Select-Object name, @{ Name="CreatedOn"; Expression= { $_.lastRelease.createdOn }}, @{ Name = "Url"; Expression = { $_._links.web.href }}
    } | Sort-Object 
    

    It makes use of the Definitions - List REST API. Note that the documentation for that API is slightly misleading in the examples. You do need to pass in $expand=lastRelease to get the lastRelease property populated.