Environments go hand-in-hand with continuous deployment and allow you to add gates and secret overrides to control how & when your deployment occurs.
In a previous exercise, you walked through setting up a continuous integration pipeline that created an app as an artifact (see 13-Continuous-Integration). Later, you modified that process to include continuous delivery which created a Docker image to be deployed (see 16-Packages-And-Continuous-Delivery).
In this exercise, you will add a Continuous Deployment
pipeline that deploys once Continuous Integration & Delivery
completes for the default branch (in the examples, main
). Additionally, you will configure an environment to see the gating functionality.
- From the default branch of your repository, create a new branch of code called
feature/deployment
- Create a new file named
.github/workflows/cd.yaml
- Copy the contents below to the newly created file:
name: Continuous Deployment
on:
workflow_dispatch:
workflow_run:
workflows: [Continuous Integration & Delivery]
branches: [main]
types:
- completed
defaults:
run:
shell: bash
env:
IMAGE_ID: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository. name }}
jobs:
deploy:
name: Deploy Docker Image
runs-on: ubuntu-latest
steps:
# A real example would have deployment steps for a container, like kubectl commands (for Kubernetes)
- name: Log In To Package Registry
run: echo "${{secrets.GITHUB_TOKEN}}" | docker login ghcr.io -u $ --password-stdin
- name: Pull Down The Image
run: docker pull $(echo $IMAGE_ID | tr '[A-Z]' '[a-z]'):latest
- name: Run The Container
run: docker run $(echo $IMAGE_ID | tr '[A-Z]' '[a-z]'):latest
- Add & commit your changes, then publish your branch.
- Go to your repository and open a pull request to merge
feature/deployment
to your default branch. - Click the green
Merge pull request
button on the pull request from step 1.5. This will put your code into the default branch. - Go to the
Actions
tab to see the workflow executions.
The result will be the Continuous Integration & Delivery
workflow executing. Once that completes, the Continuous Deployment
workflow will execute automatically. The workflow doesn't do a real deployment and simply runs the container itself, but you would replace those steps with real container orchestration commands.
- Using the official documentation:
- configure an environment named
production
(case sensitive) - Add yourself as a required reiewer (make sure to save the rules).
- Limit the deployment branches to
selected branches
, and add your default branch (in the examples above,main
) as the only branch allowed.
- configure an environment named
- From the default branch of your repository, create a new branch of code called
feature/environment
- Replace the contents of
.github/workflows/cd.yaml
with:
name: Continuous Deployment
on:
workflow_dispatch:
workflow_run:
workflows: [Continuous Integration & Delivery]
branches: [main]
types:
- completed
defaults:
run:
shell: bash
env:
IMAGE_ID: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository. name }}
jobs:
deploy:
name: Deploy Docker Image
environment: production
runs-on: ubuntu-latest
steps:
# A real example would have deployment steps for a container, like kubectl commands (for Kubernetes)
- name: Log In To Package Registry
run: echo "${{secrets.GITHUB_TOKEN}}" | docker login ghcr.io -u $ --password-stdin
- name: Pull Down The Image
run: docker pull $(echo $IMAGE_ID | tr '[A-Z]' '[a-z]'):latest
- name: Run The Container
run: docker run $(echo $IMAGE_ID | tr '[A-Z]' '[a-z]'):latest
The only real difference here is adding the environment: production
line to the job in the workflow.
- Add & commit your changes, then publish your branch.
- Go to your repository and open a pull request to merge
feature/environment
to your default branch. - Click the green
Merge pull request
button on the pull request from step 1.5. This will put your code into the main branch. - Go to the
Actions
tab to see the workflow executions.
The result will be the same as before (Continuous Integration & Delivery
executes), except the this time Continuous Delivery
will have a waiting
status which will require an approval before it will run.
- From the
Actions
tab in your repo, view theContinuous Delivery
workflow execution that is waiting. - You will see a yellow banner with a
Review deployments
button. Click that, check the box for the environment (production
) and then clickApprove and deploy
.
The result will be the Continuous Delivery
workflow fully executing, the same as before.
- Navigate to the
Code
tab on your repository - On the right side, the
Environments
section will now haveproduction
listed, with it's current status. - Click the
production
environment to see the history.
- Delete the published branch created in Step 1
- Switch back to the default branch locally.