Skip to content

๐Ÿงช Continuous Deployment

Deliver high quality value at speed

Continuous deployment (CD) is the practice of using automation to publish and deploy software updates.

To ultimately fulfill our goal on complete development of the feature, we altogether apply the practice of continuous deployment to validate and ship the value to our customers at high velocity.

Show milestone-continuous-deployment


Exercise: Automate Deployment of Releases

The previous exercise walked us through automating the publication of release. We will continue to build upon that milestone to automate deployment of published releases.


Implement Deployment Workflow

In the file explorer, create a new workflow .github/workflows/continuous.deployment.yml as follows.

.github/workflows/continuous.deployment.yml
name: Deploy Release

on:
  release:
    types:
      - released

concurrency:
  group: github-pages
  cancel-in-progress: false

permissions:
  contents: write
  deployments: write
  pages: write
  id-token: write

env:
  CI: true
  SITE_DIR: site

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}

    steps:
      - uses: actions/create-github-app-token@v1
        id: generate-app-token
        with:
          app-id: ${{ vars.APP_ID_ACTIONS_ASSISTANT }}
          private-key: ${{ secrets.APP_PRIVATE_KEY_ACTIONS_ASSISTANT }}

      - name: Setup Pages
        uses: actions/configure-pages@v3
        with:
          enablement: "true"
          token: "${{ steps.generate-app-token.outputs.token }}"

      - name: Download release asset
        id: download-release-asset
        run: |
          cat << 'EOF' > event.json
          ${{ toJson(github.event) }}
          EOF
          asset_id=$(cat event.json | jq '.release.assets[] | select(.name == "${{ env.SITE_DIR }}.zip") | .id')

          curl --location --output '${{ env.SITE_DIR }}.zip'                          \
            -H "Accept: application/octet-stream"                                     \
            -H "Authorization: Bearer ${{ steps.generate-app-token.outputs.token }}"  \
            -H "X-GitHub-Api-Version: 2022-11-28"                                     \
            "https://api.github.com/repos/${{ github.repository }}/releases/assets/${asset_id}"

          unzip -o "${{ env.SITE_DIR }}.zip" -d .

          echo '##### Debug'
          ls -al "${{ env.SITE_DIR }}"

      - name: Fix site file permissions
        run: |
          chmod -c -R +rX "${{ env.SITE_DIR }}/" | while read line; do
            echo "::warning title=Invalid file permissions automatically fixed::$line"
          done

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v2
        with:
          path: "${{ env.SITE_DIR }}"
          retention-days: "2"

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v2
        with:
          token: ${{ steps.generate-app-token.outputs.token }}

Analysis

  • Lines 3 - 6

    Deployments will be triggered in the event of a release publication.

  • Lines 8 - 10

    Ensure that only a single job or workflow using the same concurrency group will run at a time.

    When a concurrent job or workflow is queued, if another job or workflow using the same concurrency group in the repository is in progress, the queued job or workflow will be pending. Any previously pending job or workflow in the concurrency group will be canceled. If you do not wish to also cancel any currently running job or workflow in the same concurrency group, specify cancel-in-progress: false.

  • Lines 25 - 27

    This will cause the URL of the deployed site to be displayed where the workflow is executed.

  • Lines 36 - 40

    This step enables GitHub Pages and extracts various metadata about a site. It can also be used to configure various static site generators we support as starter workflows.

  • Lines 42 - 65

    Download assets associated with the release publication that triggered this workflow, and where necessary, adjust permissions for the files in the assets.

  • Lines 67 - 71

    Upload the site artifacts to the storage location where it should be ultimately collected by the next step for deployment.

  • Lines 73 - 77

    This step deploys the site.


Commit and publish your changes

You can link your changes to an issue

Recall the issue you created earlier and its respective issue number, you will use it to link your current changes to the issue.

1
2
3
git add .
git commit -m "$(printf 'Create a tetris game to drive site engagement\n\n-Implement continuous deployment\n\n- Resolves #<ISSUE-NUMBER>')"
git push origin feature/tetris-game

Merge your open pull request

  • On the pull request page, open the drop-down from the Merge pull request button to choose the Rebase and merge option.

  • Then press the Rebase and merge button to merge your pull request.

    Show pull request merge view

  • Finally, press Confirm rebase and merge.

When you merge the open pull request, you should make the following observations.

Collaboration

  • The linked issue in your project board automatically gets closed and moved to the โœ… Done column of the board.

    Show done work item

Automation

When you head over to the Actions tab on your repository, and notice...

  • The Version Changes to the Main Branch workflow is executed.
  • The Package Delivery Artifacts & Create Release workflow is executed.

    Consequently the release is announced in Discussions. Show release discussion

  • The Deploy Release workflow is executed.

    And on successful completion of the workflow, the public URL to your site is displayed on the job.

    Show successful execution of release deployment

Stakeholder Enablement

Stakeholders can easily explore deployments of the project.

  • Deployments summary on repository home.

    Show deployment on repository home

  • Detailed deployment environments list.

    Show deployments list


๐Ÿ“š Resources