azure-devopsazure-pipelines

Azure DevOps environment to display app version in environment


I'm trying to have a page where I see my current app version on:

my guess is that Azure DevOps environments might be a good solution for that (correct me if I'm wrong). So I'm trying to update the environments in my pipelines. Problem: I'm not good with pipelines. And I can't figure out how to set the status of the environment. It currently looks like this:

enter image description here

what I'd like to change: the last activity doesn't really represent the last activity but only the date the environment was created. I wish it would display the last update timestamp on the environment.

But more importantly: The "status" only displays the name of the pipeline that ran. What I'd love to see is something like:

Environment Status Last activity
git-main (green check): v2.13.01 2025-03-17 11:35

but I find no way to set the status to anything else other than the buildNumber and I fail to set the buildNumber in a deployment step. My current "post-complete.yml", which should upate the "git-main" environment everytime a branch is merged into main looks like this:

trigger:
  branches:
    include:
      - main

stages:
  - stage: Build
    displayName: 'Build Stage'
    jobs:
      - job: ExtractVersion
        displayName: 'Extract Version'
        steps:
          - script: |
              VERSION=$(grep '"version"' backend/package.json | sed -E 's/.*"version": "(.*)",/\1/')
              echo "Version: $VERSION"
              echo "##vso[build.updatebuildnumber]$VERSION"
            displayName: 'Extract Version from package.json'
            name: ExtractStep
      - deployment: UpdateEnvironmentDeployment
        displayName: 'Update Environment Deployment'
        environment: git-main
        strategy:
          runOnce:
            deploy:
              steps:
                - script: |
                    echo "buildNumber: $(Build.BuildNumber)"

anyone with a good idea to set the environment status with some text containing my app version?


Solution

  • It's a limit that changing build name by logging command works in the pipeline but not in the Environment. There is already a suggestion ticket Build Number Not Updated In Environments View If Changed In Pipeline. You can submit a vote if you want such a feature.

    As a workaround, you can use name property to custom your build name. However, the name property doesn't accept runtime variable since it is resolved before runtime. This means that your runtime variable $VERSION is not available for the current build. We need to create a separate pipeline to extract version from package.json and trigger your deployment pipeline and pass in the app version. Refer to the steps below.

    1. Create a separate pipeline used to extract version from package.json and trigger your deployment pipeline via REST API Runs - Run Pipeline. YAML file:

      trigger:
        branches:
          include:
            - main
      
      pool:
        vmImage: ubuntu-latest
      
      steps:
      - script: |
          VERSION=$(grep '"version"' backend/package.json | sed -E 's/.*"version": "(.*)",/\1/')
          echo "Version: $VERSION"
          echo "##vso[task.setvariable variable=appVersion;]$VERSION"
        displayName: 'Extract Version from package.json'
      
      - task: PowerShell@2
        displayName: 'Trigger deployment pipeline'
        inputs:
          targetType: 'inline'
          script: |
            $headers = @{'Authorization' = 'Bearer ' + "$(System.AccessToken)"}
            $url="https://dev.azure.com/{orgName}/{projectName}/_apis/pipelines/{PipelineIdOfYourDeploymentPipeline}/runs?api-version=5.1-preview.1"
            $body = "{
                `"stagesToSkip`": [],
                `"resources`": {
                    `"repositories`": {
                        `"self`": {
                            `"refName`": `"refs/heads/main`"
                        }
                    }
                },
                `"variables`": {
                    `"appversion`": {
                        `"value`": `"$(appVersion)`"
                    }
                }
            }"
            Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body -ContentType application/json
      
    2. Assign build service account with permissions to trigger your deployment pipeline.

      In the above pipeline, it is using System.AccessToken for authentication, so that the identity used to trigger the pipeline is your build service account. Go to Project Settings -> Pipelines -> Settings.

      • If you have turned on Limit job authorization scope to current project for non-release pipelines, add project-level build service account {ProjectName} Build Service (yourOrgName) into the Contributors group of your project.

      • If you have turned off this setting, add org-level build service account Project Collection Build Service (yourOrgName) into the Contributors group.

        enter image description here

        enter image description here

    3. Turn off Limit variables that can be set at queue time settings from Project Settings -> Pipelines -> Settings as shown above. Otherwise, we can't pass app version to the deployment pipeline.

    4. Use name property to custom build name of your deployment pipeline. For example,

      trigger:
      - none
      
      name: appVersion$(appVersion)_$(Date:yyyyMMdd)_$(Hours)$(Minutes)
      
      stages:
      - stage: deploy
        jobs:
        - deployment: DeployWeb
          displayName: deploy Web App
          pool:
            vmImage: 'Ubuntu-latest'
          environment: 
            name: 'git-main'
          strategy:
            runOnce:
              deploy:
                steps:
                - script: echo Hello world
      
    5. Then you can see the app version and build date and time in the Environment.

      enter image description here

      Azure DevOps doesn't support changing the Environment Last activity manually, so that I add the time to the build name.