In this session, we are going to be deploying the application to various production style environments. This will take the built Docker Container and deploy it to one or more of the various environments.
Note: Before you add the code below, you will need to setup Github Secrets To help hold credentials and hidden endpoints.
- DockerHub
DOCKERHUB_USERNAME
- Username to authenticate to DockerHubDOCKERHUB_PASSWORD
- Password to authenticate to DockerHub
- Github Container Registry
GCR_USERNAME
- Username to authenticate to GitHubGCR_TOKEN
- GitHub Personal Access Token with access rights to container registry
- AWS
AWS_ACCESS_KEY_ID
- Access key id to authenticate to AWSAWS_SECRET_ACCESS_KEY
- Secret Access key to authenticate to AWSECR_REGISTRY
- AWS ECR Registry to push container imageECR_REPOSITORY
- AWS ECR repository to push container image
- Create a new branch called
Deploy
- Add the following file to your repository:
.github/workflows/deploy-prod-docker.yml
Click here to add the file
# This is a basic workflow to help you get started with Actions
name: Docker Production
# Controls when the action will run.
on:
push:
branches:
- 'master'
- 'main'
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
docker-prod-release:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# You could use the following lines to help make sure only X people start the workflow
# if: github.actor == 'admiralawkbar' || github.actor == 'jwiebalk'
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout source code
uses: actions/checkout@v2
#########################
# Install Docker BuildX #
#########################
- name: Install Docker BuildX
uses: docker/setup-buildx-action@v1
######################
# Login to DockerHub #
######################
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
# Update deployment API
- name: start deployment
uses: bobheadxi/[email protected]
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: Production
# Create a GitHub Issue with the info from this build
- name: Create GitHub Issue
uses: actions/[email protected]
id: create-issue
with:
# https://octokit.github.io/rest.js/v18#issues-create
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const create = await github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "Deploying to production",
body: 'Currently deploying...'
})
console.log('create', create)
return create.data.number
###########################################
# Build and Push containers to registries #
###########################################
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: |
DOCKER_ORG/demo-action:latest
DOCKER_ORG/demo-action:v1
# Update Deployment API
- name: update deployment status
uses: bobheadxi/[email protected]
if: always()
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: https://github.com/orgs/${{github.repository_owner}}/packages?repo_name=${{github.repository.name}}
- name: Update issue success
uses: actions/[email protected]
if: success()
with:
# https://octokit.github.io/rest.js/v18#issues-create
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: "${{ steps.create-issue.outputs.result }}",
title: "New issue created",
body: "Successful!y deployed production"
})
- name: Update issue failure
uses: actions/[email protected]
if: failure()
with:
# https://octokit.github.io/rest.js/v18#issues-create
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: "${{ steps.create-issue.outputs.result }}",
title: "New issue created",
body: "Failed to deploy to production"
})
- Commit the code
- Open Pull request
- Create a new branch called
Deploy
- Add the following file to your repository:
.github/workflows/deploy-prod-gcr.yml
Click here to add the file
# This is a basic workflow to help you get started with Actions
name: Docker Production
# Controls when the action will run.
on:
push:
branches:
- 'master'
- 'main'
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
docker-prod-release:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# You could use the following lines to help make sure only X people start the workflow
# if: github.actor == 'admiralawkbar' || github.actor == 'jwiebalk'
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout source code
uses: actions/checkout@v2
#########################
# Install Docker BuildX #
#########################
- name: Install Docker BuildX
uses: docker/setup-buildx-action@v1
######################################
# Login to GitHub Container Registry #
######################################
- name: Login to GitHub Container registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ secrets.GCR_USERNAME }}
password: ${{ secrets.GCR_TOKEN }}
# Update deployment API
- name: start deployment
uses: bobheadxi/[email protected]
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: Production
# Create a GitHub Issue with the info from this build
- name: Create GitHub Issue
uses: actions/[email protected]
id: create-issue
with:
# https://octokit.github.io/rest.js/v18#issues-create
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const create = await github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "Deploying to production",
body: 'Currently deploying...'
})
console.log('create', create)
return create.data.number
########################################
# Convert repository name to lowercase #
########################################
- name: Repository Name to lowercase
run: |
echo IMAGE_REPOSITORY=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
###########################################
# Build and Push containers to registries #
###########################################
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: |
ghcr.io/${{ env.IMAGE_REPOSITORY }}:latest
ghcr.io/${{ env.IMAGE_REPOSITORY }}:v1
# Update Deployment API
- name: update deployment status
uses: bobheadxi/[email protected]
if: always()
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: https://github.com/orgs/${{github.repository_owner}}/packages?repo_name=${{github.repository.name}}
- name: Update issue success
uses: actions/[email protected]
if: success()
with:
# https://octokit.github.io/rest.js/v18#issues-create
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: "${{ steps.create-issue.outputs.result }}",
title: "New issue created",
body: "Successful!y deployed production"
})
- name: Update issue failure
uses: actions/[email protected]
if: failure()
with:
# https://octokit.github.io/rest.js/v18#issues-create
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: "${{ steps.create-issue.outputs.result }}",
title: "New issue created",
body: "Failed to deploy to production"
})
- Commit the code
- Open Pull request
- Create a new branch called
Deploy
- Add the following file to your repository:
.github/workflows/deploy-prod-aws.yml
Click here to add the file
# This is a basic workflow to help you get started with Actions
name: Docker Production
# Controls when the action will run.
on:
push:
branches:
- 'master'
- 'main'
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
docker-prod-release:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# You could use the following lines to help make sure only X people start the workflow
# if: github.actor == 'admiralawkbar' || github.actor == 'jwiebalk'
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout source code
uses: actions/checkout@v2
#########################
# Install Docker BuildX #
#########################
- name: Install Docker BuildX
uses: docker/setup-buildx-action@v1
####################
# Config AWS Creds #
####################
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
#################
# Login AWS ECR #
#################
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
# Update deployment API
- name: start deployment
uses: bobheadxi/[email protected]
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: Production
# Create a GitHub Issue with the info from this build
- name: Create GitHub Issue
uses: actions/[email protected]
id: create-issue
with:
# https://octokit.github.io/rest.js/v18#issues-create
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const create = await github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "Deploying to production",
body: 'Currently deploying...'
})
console.log('create', create)
return create.data.number
###########################################
# Build and Push containers to registries #
###########################################
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: |
${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:latest
${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:v1
# Update Deployment API
- name: update deployment status
uses: bobheadxi/[email protected]
if: always()
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: https://github.com/orgs/${{github.repository_owner}}/packages?repo_name=${{github.repository.name}}
- name: Update issue success
uses: actions/[email protected]
if: success()
with:
# https://octokit.github.io/rest.js/v18#issues-create
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: "${{ steps.create-issue.outputs.result }}",
title: "New issue created",
body: "Successful!y deployed production"
})
- name: Update issue failure
uses: actions/[email protected]
if: failure()
with:
# https://octokit.github.io/rest.js/v18#issues-create
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: "${{ steps.create-issue.outputs.result }}",
title: "New issue created",
body: "Failed to deploy to production"
})
- Commit the code
- Open Pull request