Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Container App pipeline with Pulumi Delivery on Azure

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set azure-cs-delivery-container-apps
cd pulumi-examples/azure-cs-delivery-container-apps

This example uses Pulumi Delivery to build a container image on every push, push it to Azure Container Registry, deploy it to an Azure Container App in a beta stage, and check that the app answers. The image is built once: the beta stage deploys the exact image the build stage pushed, by digest, so every commit gets a new Container App revision.

The example has three Pulumi projects, all in C#:

  • pipeline/ defines the Delivery pipeline: a GitHub source, a build stage and a beta stage.
  • image/ creates a container registry, then builds the app in app/ and pushes its image.
  • beta/ deploys that image to an Azure Container App and exports the app’s URL.

Every stack authenticates to Azure through Pulumi ESC with OIDC, so no long-lived Azure credentials are stored anywhere.

Prerequisites#

  1. Install Pulumi, version 3.277.0 or later
  2. Install .NET, version 8 or later
  3. A Pulumi Cloud organization with Pulumi Delivery available
  4. An Azure subscription with the Microsoft.App and Microsoft.ContainerRegistry resource providers registered
  5. A GitHub repository that holds this example, for example a fork of pulumi/examples

Setting up the pipeline#

Run the commands below in order, in one shell, starting in this example’s directory.

  1. Give Pulumi Cloud access to your repository. Install the Pulumi GitHub App on your GitHub organization if it isn’t installed yet, then check that it can read the repository that holds this example: in Pulumi Cloud, go to Settings > Version control, open the GitHub installation, choose Configure repository access, and add the repository on GitHub.

  2. Create an ESC environment that logs in to Azure with OIDC. Follow Configuring OIDC for Azure, then expose the credentials to the programs:

    values:
    azure:
    login:
    fn::open::azure-login:
    clientId: <client-id>
    tenantId: <tenant-id>
    subscriptionId: <subscription-id>
    oidc: true
    subjectAttributes:
    - currentEnvironment.name
    environmentVariables:
    ARM_USE_OIDC: 'true'
    ARM_CLIENT_ID: ${azure.login.clientId}
    ARM_TENANT_ID: ${azure.login.tenantId}
    ARM_OIDC_TOKEN: ${azure.login.oidc.token}
    ARM_SUBSCRIPTION_ID: ${azure.login.subscriptionId}

    The application needs to create resource groups, container registries and Container Apps in the subscription, for example through the Contributor role.

  3. Create the image and beta stacks, import the environment into each one, and set the Azure region. Delivery only updates stacks that already exist.

    The project names in the three Pulumi.yaml files and the stack references in pipeline/Program.cs are fixed. A second deployment in the same Pulumi organization must change them first, or it fails with a 409 “already exists” error. Use your names in the commands below.

    Terminal window
    cd image
    pulumi stack init <org>/azure-cs-delivery-container-apps-image/image
    pulumi config env add <project>/<environment> --yes
    pulumi config set azure-native:location westus2
    cd ../beta
    pulumi stack init <org>/azure-cs-delivery-container-apps-beta/beta
    pulumi config env add <project>/<environment> --yes
    pulumi config set azure-native:location westus2
    cd ..
  4. Commit the two stack files. Delivery runs every program from your repository, so each stack’s Pulumi.<stack>.yaml must be in git. In a fork of pulumi/examples, these files are ignored, so add them with -f:

    Terminal window
    git add -f image/Pulumi.image.yaml beta/Pulumi.beta.yaml
    git commit -m "Add stack files for the Delivery example"
    git push
  5. Create the pipeline stack, point it at your repository, and tell Delivery where the pipeline program lives, then commit and push everything:

    Terminal window
    cd pipeline
    pulumi stack init <org>/azure-cs-delivery-container-apps-pipeline/pipeline
    pulumi config set repository <owner>/<name>
    pulumi config set branch <branch>
    pulumi config set path <path>
    pulumi config set --path 'delivery:source.url' https://github.com/<owner>/<name>
    pulumi config set --path 'delivery:source.branch' <branch>
    pulumi config set --path 'delivery:source.directory' <path>/pipeline
    git add -f Pulumi.pipeline.yaml
    git commit -m "Point the Delivery pipeline at this repository"
    git push
    cd ..

    repository is your repository, as <owner>/<name>, and branch is the branch to watch. path is this example’s directory within the repository, for example azure-cs-delivery-container-apps. The example must live in a subdirectory: the pipeline starts runs for pushes that match <path>/**. With the example at the repository root (path of .), pushes update the pipeline but start no run.

  6. Register the pipeline:

    Terminal window
    cd pipeline
    pulumi delivery register
    cd ..

    A successful registration ends with Registered the pipeline of <stack> and a link to the pipeline. Its stages show as awaiting until the first run starts. Registration fails if the checkout has uncommitted changes or the commit is not on the branch in delivery:source.

  7. Let the pipeline open the ESC environment. Registration creates a service principal, named “Delivery azure-cs-delivery-container-apps-pipeline pipeline”, and gives it write access to the pipeline, image and beta stacks. The stacks run as that principal, so it also needs to open the environment they import. In Pulumi Cloud, open the environment, go to Settings, then Access, choose Add Principal, then Service Principal, pick the pipeline’s service principal and give it Environment Open.

    Or from the command line:

    Terminal window
    ./scripts/pulumi-env-access-grant <project>/<environment> --principal "Delivery azure-cs-delivery-container-apps-pipeline pipeline" --by-name --permission open
  8. Start the first run: push a change to a file in this example, or choose Start a run on the pipeline in Pulumi Cloud, under Delivery in your organization. Registration does not start a run.

    Or from the command line, with your organization in place of <org>. It prints the number of the new run:

    Terminal window
    pulumi api DispatchDeliveryRun -F orgName=<org> -F projectName=azure-cs-delivery-container-apps-pipeline -F stackName=pipeline --body '{}'
  9. Watch the run in Pulumi Cloud. The build stage pushes the image. The beta stage deploys it, then the check-url job requests the app’s URL and records it as the job’s checked output. The run also records the URL as the beta-url artifact of the release. From now on, every push that changes files in this example starts a new run.

Cleaning up#

From this example’s directory, destroy the stacks in reverse order and remove them. Each step runs only if the one before it succeeded:

Terminal window
(cd pipeline && pulumi destroy && pulumi stack rm) &&
(cd beta && pulumi destroy && pulumi stack rm) &&
(cd image && pulumi destroy && pulumi stack rm)

Destroying the image stack deletes the container registry and every image in it. Destroying the pipeline stack removes the pipeline’s service principal and its access to the environment.

If you created the ESC environment only for this example, remove it with pulumi env rm <project>/<environment>, along with any Azure OIDC resources you created for it.

Learn more#

Related

The infrastructure platform for humans and agents.