Implement a release strategy with GitHub deployment environments
TypeScriptIn order to implement a release strategy with GitHub deployment environments using Pulumi, we will utilize the
github
package to create deployment environments in a GitHub repository. This will allow us to define multiple environments like staging and production, and control deployment configurations for them. We can also set up protected branch policies, automatic review assignments, and timers before deploying to an environment, thereby streamlining the release process.Below you will find a TypeScript program that defines two environments (
staging
andproduction
) for a GitHub repository. This example assumes you have a GitHub repository you want to manage deployments for, and that your Pulumi environment is already set up with the necessary GitHub provider configuration.Let's dive into the program:
import * as github from "@pulumi/github"; const repoName = "example-repo"; // Replace with your repository name // Define a staging environment in Github repository const stagingEnv = new github.RepositoryEnvironment("staging", { repository: repoName, environment: "staging", waitTimer: 0, reviewers: [{ teams: [], // Optionally specify GitHub team IDs to be assigned as reviewers users: [], // Optionally specify GitHub user IDs to be assigned as reviewers }], deploymentBranchPolicy: { protectedBranches: true, customBranchPolicies: false, }, }); // Define a production environment in Github repository const productionEnv = new github.RepositoryEnvironment("production", { repository: repoName, environment: "production", waitTimer: 10, // Optionally define a wait timer (in minutes) before proceeding with the deployment reviewers: [{ teams: [], // Optionally specify GitHub team IDs to be assigned as reviewers users: [], // Optionally specify GitHub user IDs to be assigned as reviewers }], deploymentBranchPolicy: { protectedBranches: true, customBranchPolicies: false, }, }); // Export the IDs of the environments so you can easily reference them in other operations or tools export const stagingEnvironmentId = stagingEnv.id; export const productionEnvironmentId = productionEnv.id;
In the above code:
- We create two
RepositoryEnvironment
resources, one for staging and one for production, using the@pulumi/github
package. - The
waitTimer
property is an optional field that sets a delay before a deployment to the environment can proceed. - The
reviewers
property allows specification of teams and users who need to review deployments to these environments. - The
deploymentBranchPolicy
property enforces that only protected branches can trigger deployments; this means you would usually configure branch protection rules to these branches in your GitHub repository's settings.
The
RepositoryEnvironment
is a high-level abstraction that Pulumi provides to manage GitHub repository deployment environments directly.Remember to replace
"example-repo"
with the name of your repository and set your GitHub provider credentials or token appropriately in your Pulumi environment.This program should be placed in a TypeScript Pulumi project. To run it, you'll need to have Pulumi installed, along with Node.js for TypeScript support.
After you have the Pulumi CLI configured:
- Create a new directory for your Pulumi project.
- Run
pulumi new typescript
in the directory. - Replace the contents of
index.ts
with the code provided above. - Run
pulumi up
to preview and deploy the changes.
Pulumi will communicate with GitHub to create those environments according to your provided configuration. Afterward, you'll be able to use these environments in your GitHub Actions workflows or other deployment systems that interface with GitHub.
Don't forget to check the Pulumi GitHub provider documentation for more details on how to use the resources.
- We create two