Skip to content

Conversation

@github-learning-lab
Copy link
Contributor

Different triggers

Deployments to production can be manual (like through a Chat Ops command), or automated (if, say, we trust our pull request review process and we've followed continuous integration practices).

We'll trigger a deployment to the production environment whenever something is committed to master. Our master branch is protected, so the only way for commits to appear on master is for a pull request to have been created and gone through the proper review process and merged.

Step 8: Write the production deployment trigger

Let's create a new workflow that deals specifically with commits to master and handles deployments to prod.

⌨️ Activity: Write the production deployment trigger on merge to master

  1. Edit the deploy-prod.yml file on this branch, or use this quick link (We recommend opening the quick link in another tab)
  2. Rename the file in this pull request to .github/workflows/deploy-prod.yml
  3. Add a push trigger
  4. Add branches inside the push block
  5. Add - master inside the branches block
  6. Commit your changes to this branch

The file should look like this when you're finished:

name: Production deployment

on: 
  push:
    branches:
      - master

@github-learning-lab
Copy link
Contributor Author

Great! The syntax you used tells GitHub Actions to only run that workflow when a commit is made to the master branch.

Deploying to production

Just like with the other workflow, we'll need to build our application and deploy to AWS using the same action as before because we are working with the same Node.js app.

Continuous delivery is a concept that contains many behaviors and other, more specific concepts. One of those concepts is test in production. That can mean different things to different projects and different companies, and isn't a strict rule that says you are or aren't "doing CD".

In our case, we can match our production environment to be exactly like our staging environment. This minimizes opportunities for surprises once we deploy to production.

Step 9: Complete the deployment to production workflow

⌨️ Commit the steps to the production workflow that allow you to deploy on merge to master

  1. Edit the deploy-prod.yml file on this branch, or use this quick link (We recommend opening the quick link in another tab)
  2. Add a build and deploy job to the workflow

It should look like the file below when you are finished. Note that not much has changed from our staging workflow, except for our trigger.

name: Production deployment

on: 
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1
      - name: npm install and build webpack
        run: |
          npm install
          npm run build
      - uses: actions/upload-artifact@master
        with:
          name: webpack artifacts
          path: public/

  deploy:
    name: Deploy Node.js app to AWS
    needs: build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1

      - name: Download built artifact
        uses: actions/download-artifact@master
        with:
          name: webpack artifacts
          path: public

      - name: Deploy to AWS
        uses: docker://admiralawkbar/aws-nodejs:latest
        env:
          AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }}
          AWS_SECRET_KEY: ${{ secrets.AWS_SECRET_KEY }}

@github-learning-lab
Copy link
Contributor Author

Workflow steps

We'll add a final section to our production workflow that packages up our application in a Docker container and publishes it to GitHub Packages. This step is important for the traceability of your deployed artifacts.

We'll only use one new action here created by a GitHubber, which allows us to push a container to GitHub Packages.

  • mattdavis0351/actions/docker-gpr

All of this happens automatically once a pull request is merged!

Step 10: Create the Docker image and push it to GitHub Packages

⌨️ Activity: Write the steps for the production deployment workflow

  1. Edit the deploy-prod.yml file on this branch, or use this quick link (We recommend opening the quick link in another tab)
  2. Add a job to your workflow as follows:
    Build-and-Push-Docker-Image:
    runs-on: ubuntu-latest
    needs: build
    name: Docker Build, Tag, Push
    steps:
      - name: Checkout
        uses: actions/checkout@v1
    
      - name: Download built artifact
        uses: actions/download-artifact@master
        with:
          name: webpack artifacts
          path: public
    
      - name: Build, Tag, Push
        uses: mattdavis0351/actions/docker-gpr@v1
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          image-name: tic-tac-toe
  3. Commit the workflow to this branch.

The complete workflow file should look like this:

name: Production deployment

on: 
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1
      - name: npm install and build webpack
        run: |
          npm install
          npm run build
      - uses: actions/upload-artifact@master
        with:
          name: webpack artifacts
          path: public/

  deploy:
    name: Deploy Node.js app to AWS
    needs: build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1

      - name: Download built artifact
        uses: actions/download-artifact@master
        with:
          name: webpack artifacts
          path: public

      - name: Deploy to AWS
        uses: docker://admiralawkbar/aws-nodejs:latest
        env:
          AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }}
          AWS_SECRET_KEY: ${{ secrets.AWS_SECRET_KEY }}

  Build-and-Push-Docker-Image:
    runs-on: ubuntu-latest
    needs: build
    name: Docker Build, Tag, Push
    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Download built artifact
        uses: actions/download-artifact@master
        with:
          name: webpack artifacts
          path: public

      - name: Build, Tag, Push
        uses: mattdavis0351/actions/docker-gpr@v1
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          image-name: tic-tac-toe

@github-learning-lab
Copy link
Contributor Author

Completed Workflow

Nice job, you've done it!

Step 11: Merge the production workflow

⌨️ Activity: Merge this pull request and test the production deployment workflow

  1. Merge this pull request
  2. Delete the branch

@janerikcarlsen janerikcarlsen merged commit 41f248d into master Mar 6, 2020
@janerikcarlsen janerikcarlsen deleted the production-deployment-workflow branch March 6, 2020 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants