Pipeline patterns
This page shows common patterns for building pipelines with Pulumi Delivery. If you haven't registered a pipeline yet, start with Get started. The first example runs a smoke.sh script from the repository's delivery directory, shown in Running smoke tests after deployment. The last section builds a container image once and deploys it to every stage.
Reducing duplication with helper functions
When your environments differ by only a few values, you can write functions in your language of choice to extract common functionality and avoid repetition. In this example, a function defines the Stacks and Job resources for each stage of the pipeline:
import * as delivery from "@pulumi/delivery";
const repo = new delivery.GitHubSource("repo", {
repository: "acme/shop",
branch: "main",
trigger: { paths: ["app/**", "delivery/**"] },
});
const pipeline = new delivery.Pipeline("shop");
function deploy(stage: delivery.Stage, name: string, region: string) {
const app = new delivery.Stacks(name, {
stage,
stacks: {
app: { stack: `shop-app/${name}`, source: repo, directory: "app" },
},
});
new delivery.Job(`smoke-${name}`, {
stage,
source: repo,
directory: "delivery",
command: "sh smoke.sh",
environment: {
ENDPOINT: app.outputs.apply((o) => String(o["app"]["endpoint"])),
EXPECTED_REGION: region,
},
});
}
const dev = new delivery.Stage("dev", { pipeline });
deploy(dev, "dev", "eu-central-1");
const staging = new delivery.Stage("staging", { pipeline, from: [dev] });
deploy(staging, "staging", "eu-west-1");
const prod = new delivery.Stage("prod", {
pipeline,
from: [staging],
when: delivery.approval(),
});
deploy(prod, "prod", "us-west-2");Resource names must be unique, so build them from the environment name. The region itself comes from each target stack's config file (for AWS, aws:region in Pulumi.<stack>.yaml). The region argument is passed to the job as EXPECTED_REGION, for checks you add to smoke.sh.
Running smoke tests after deployment
Put a Job in the same stage as the Stacks resource it tests, so the test runs right after the deploy and before the release moves on. A job can read outputs from its own stage or an earlier one, but not from a later stage. This smoke.sh retries, because a new deployment can take a few seconds to answer. On success, it publishes a checked output through DELIVERY_OUTPUTS (see Job outputs):
set -eu
for attempt in 1 2 3 4 5; do
if curl -fsS "$ENDPOINT/health" > /dev/null; then
echo "{\"checked\": \"$ENDPOINT\"}" > "$DELIVERY_OUTPUTS"
exit 0
fi
sleep 10
done
echo "Smoke test failed for $ENDPOINT" >&2
exit 1curl -fsS makes an HTTP error return a non-zero exit code. When the job fails, its stage fails and later stages don't start. Fix the problem, then retry the run from its page in the Pulumi Cloud console.
Building container images
Many pipelines deploy an application as a container image. Build the image once, in a build stage, and pass its digest to the stages that deploy it.
Avoid rebuilding in each stage, because each build can pull a newer base image, newer packages, or a newer build tool, so prod could run a different image from the one you tested in staging. Rebuilding the image also repeats a slow step in every stage. A digest, unlike a tag, always references the same image, so every stage runs exactly the image you tested, with a predictable, reproducible result.
In the build stage, use a stack whose Pulumi program uses the Docker Build provider's Image resource. The program pushes the image to a registry and exports a reference that includes the image digest as a stack output. The pipeline passes that output to the stacks that deploy the image through each entry's config.
This program, in the image directory, builds the application in app and reads the registry and its credentials from stack config:
import * as pulumi from "@pulumi/pulumi";
import * as dockerBuild from "@pulumi/docker-build";
const config = new pulumi.Config();
// Registry host, for example "ghcr.io".
const registry = config.require("registry");
// Repository in that registry, for example "acme/shop".
const repository = config.require("repository");
// Source commit, passed by the pipeline.
const commit = config.require("commit");
const image = new dockerBuild.Image("shop", {
context: { location: "../app" },
// For example "ghcr.io/acme/shop:3f2c1a9e4b7d6c5f8a0b2d4e6f8a1c3e5b7d9f0a".
tags: [`${registry}/${repository}:${commit}`],
push: true,
// Credentials for pushing to the registry.
registries: [{
address: registry,
username: config.require("registryUsername"),
password: config.requireSecret("registryPassword"),
}],
});
// The tag plus the digest, for example
// "ghcr.io/acme/shop:3f2c1a9e4b7d6c5f8a0b2d4e6f8a1c3e5b7d9f0a@sha256:4e8d2f0c5b9a…".
export const imageRef = image.ref;This example pushes to GitHub Container Registry. To use another registry, such as Amazon ECR or Google Artifact Registry, change registry, repository, and the credentials. To pass credentials to the stack, see Cloud credentials. The Image resource reference includes an example that pushes to Amazon ECR.
This pipeline builds on the helper example, without the smoke-test job to keep it short. It runs the image stack in a build stage and passes imageRef to the stacks in every later stage:
import * as delivery from "@pulumi/delivery";
const repo = new delivery.GitHubSource("repo", {
repository: "acme/shop",
branch: "main",
trigger: {
paths: [
"app/**", // Application source and its Dockerfile.
"image/**", // Program that builds and pushes the image.
"infra/**", // Program that deploys the image.
"delivery/**", // This pipeline program.
],
},
});
const pipeline = new delivery.Pipeline("shop");
const build = new delivery.Stage("build", { pipeline });
const image = new delivery.Stacks("image", {
stage: build,
stacks: {
image: {
stack: "shop-image/build",
source: repo,
directory: "image",
config: { commit: repo.commit },
},
},
});
// The outputs are empty until the image stack has run.
const imageRef = image.outputs.apply(o => String(o?.["image"]?.["imageRef"]));
function deploy(stage: delivery.Stage, name: string) {
new delivery.Stacks(name, {
stage,
stacks: {
app: {
stack: `shop-app/${name}`,
source: repo,
directory: "infra",
config: { imageRef },
},
},
});
}
const dev = new delivery.Stage("dev", { pipeline, from: [build] });
deploy(dev, "dev");
const staging = new delivery.Stage("staging", { pipeline, from: [dev] });
deploy(staging, "staging");
const prod = new delivery.Stage("prod", {
pipeline,
from: [staging],
when: delivery.approval(),
});
deploy(prod, "prod");The pipeline passes the commit to the image stack, which uses it as the image tag, so you can trace each image to its source. The stacks deploy the image by its digest, so the tag is only a label.
Next steps
- Pass values between pipelines in Connecting pipelines.
- Check the limits on pipelines and runs.