LLI Wiki — My Digital Knowledge Base

Azure DevOps YAML pipelines: build and deploy PowerShell applications to Azure Batch

Build, package and deploy stages, dev/acc/prd environments with approvals, Key Vault-linked variable groups and a workload identity federation service connection.

A PowerShell application run by a DBA team on Azure Batch should never be deployed by hand from a workstation. I build and deploy it with an Azure DevOps YAML pipeline: same code, same path, three environments. Here is how I set it up.

The flow

flowchart LR
    A["Git repository (main)"] --> B["Build stage: tests + lint"]
    B --> C["Package stage: ZIP + version"]
    C --> D["Pipeline artifact"]
    D --> E["Deploy dev"]
    E --> F["Deploy acc (approval)"]
    F --> G["Deploy prd (approval)"]
    G --> H["Azure Batch: application + default version"]
    KV["Key Vault"] -.-> VG["Variable group"]
    VG -.-> E
    VG -.-> F
    VG -.-> G
    SC["Service connection (WIF)"] -.-> H

Three stages, one role each

Stage Role Output
Build PSScriptAnalyzer, Pester, manifest validation test results
Package assembles the ZIP (application + shared modules), computes the version package artifact
Deploy publishes the ZIP as a Batch application version and makes it the default active version

The key point: build once. The ZIP produced by the Package stage is the artifact that travels through dev, acc and prd. Never rebuild for production, otherwise you are no longer deploying what you tested.

The YAML, simplified

trigger:
  branches: { include: [ main ] }
  paths:    { include: [ apps/export/** ] }

parameters:
  - name: forceRedeploy
    type: boolean
    default: false

stages:
  - stage: Build
    jobs:
      - job: Test
        steps:
          - template: templates/steps-test.yml

  - stage: Package
    dependsOn: Build
    jobs:
      - job: Zip
        steps:
          - pwsh: ./batch/build-package.ps1 -OutputPath "$(Build.ArtifactStagingDirectory)"
          - publish: $(Build.ArtifactStagingDirectory)
            artifact: package

  - ${{ each env in split('dev,acc,prd', ',') }}:
    - stage: Deploy_${{ env }}
      dependsOn: Package
      variables:
        - group: vg-dba-${{ env }}
      jobs:
        - deployment: Batch
          environment: ${{ env }}
          strategy:
            runOnce:
              deploy:
                steps:
                  - template: templates/steps-deploy-batch.yml
                    parameters: { environment: '${{ env }}' }

The three deploy stages are chained: Deploy_acc depends on Deploy_dev, and Deploy_prd on Deploy_acc (omitted here for readability). A deployment job bound to an environment is what lets you attach approvals.

Environments and approvals

The dev, acc and prd environments are created in Azure DevOps (Pipelines > Environments). On acc and prd I add an approval: the designated person must approve before the stage starts. The YAML contains no approval rule; it lives on the environment, so nobody can remove it by editing the file.

Variable groups linked to Key Vault

Each environment has its own variable group (vg-dba-dev, and so on), linked to a Key Vault: secrets are read at run time and never copied into the repository. Non-secret values (Batch account name, resource group, storage account) live in the same group. The pipeline only knows variable names.

Service connection and workload identity federation

The pipeline authenticates to Azure through an Azure Resource Manager service connection configured with workload identity federation: no client secret to rotate, Azure DevOps presents a short-lived OIDC token. The principal behind it only has the rights it needs on the Batch account and its storage.

- task: AzureCLI@2
  inputs:
    azureSubscription: sc-dba-prd
    scriptType: pscore
    scriptLocation: inlineScript
    inlineScript: |
      az batch application package create `
        --resource-group $(batchRg) --name $(batchAccount) `
        --application-name dba-export --version-name $(version) `
        --package-file "$(Pipeline.Workspace)/package/dba-export-$(version).zip"
      az batch application set `
        --resource-group $(batchRg) --name $(batchAccount) `
        --application-name dba-export --default-version $(version)

Lessons learned

Mastodon LLIMastodon SocialFosstodon