Container App pipeline with Pulumi Delivery on Azure
This example lives in the pulumi/examples repository. Check out just this directory to use it:
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examplesgit -C pulumi-examples sparse-checkout set azure-cs-delivery-container-appscd pulumi-examples/azure-cs-delivery-container-appsThis 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, abuildstage and abetastage.image/creates a container registry, then builds the app inapp/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#
- Install Pulumi, version 3.277.0 or later
- Install .NET, version 8 or later
- A Pulumi Cloud organization with Pulumi Delivery available
- An Azure subscription with the
Microsoft.AppandMicrosoft.ContainerRegistryresource providers registered - 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.
-
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.
-
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: truesubjectAttributes:- currentEnvironment.nameenvironmentVariables: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.
-
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.yamlfiles and the stack references inpipeline/Program.csare 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 imagepulumi stack init <org>/azure-cs-delivery-container-apps-image/imagepulumi config env add <project>/<environment> --yespulumi config set azure-native:location westus2cd ../betapulumi stack init <org>/azure-cs-delivery-container-apps-beta/betapulumi config env add <project>/<environment> --yespulumi config set azure-native:location westus2cd .. -
Commit the two stack files. Delivery runs every program from your repository, so each stack’s
Pulumi.<stack>.yamlmust be in git. In a fork ofpulumi/examples, these files are ignored, so add them with-f:Terminal window git add -f image/Pulumi.image.yaml beta/Pulumi.beta.yamlgit commit -m "Add stack files for the Delivery example"git push -
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 pipelinepulumi stack init <org>/azure-cs-delivery-container-apps-pipeline/pipelinepulumi 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>/pipelinegit add -f Pulumi.pipeline.yamlgit commit -m "Point the Delivery pipeline at this repository"git pushcd ..repositoryis your repository, as<owner>/<name>, andbranchis the branch to watch.pathis this example’s directory within the repository, for exampleazure-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 (pathof.), pushes update the pipeline but start no run. -
Register the pipeline:
Terminal window cd pipelinepulumi delivery registercd ..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 indelivery:source. -
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 -
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 '{}' -
Watch the run in Pulumi Cloud. The
buildstage pushes the image. Thebetastage deploys it, then thecheck-urljob requests the app’s URL and records it as the job’scheckedoutput. The run also records the URL as thebeta-urlartifact 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:
(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.