AI Hints
Home›CI/CD with GitHub Actions Tutorial›Migrations and Delivery Practice
CI/CD with GitHub Actions · Lesson 99 of 138

Practice: Staging then Production

Two stages.

⏱ 2 min read▶ Video at 1:54:24🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Last delivery practice. On every push to main, deploy to staging, and run a smoke test. Then deploy the same build to production, behind the production environment's approval.

Practice: Staging then Production
From the video: Practice: Staging then Production
  • Push to main
  • Staging plus smoke test
  • Then production

One answer

The staging job deploys the build named after the commit, and runs a smoke test. The production job needs staging, and uses the production environment, where you'd add required reviewers. Both jobs use the same SHA tag, so production gets exactly what staging tested.

.github/workflows/delivery.ymlyaml
name: Delivery
on:
  push:
    branches: [main]
jobs:
  staging:
    runs-on: ubuntu-latest
    environment: staging
    steps:
      - run: echo "Deploy sha-${GITHUB_SHA::7} to staging"
      - run: echo "Smoke test on staging passed"
  production:
    needs: staging
    runs-on: ubuntu-latest
    environment: production
    steps:
      - run: echo "Deploy sha-${GITHUB_SHA::7} to production"

Running it

Our helper runs staging, then production. Both deploy lines show the same SHA, so the artifact is built once, and promoted. On GitHub, the production job would wait here for an approval.

Terminal
run-steps .github/workflows/delivery.yml | grep -vE "^Workflow|✓"
Job: staging
  Environment: staging
      Deploy sha-7418016 to staging
      Smoke test on staging passed
  Job succeeded
Job: production
  Environment: production
      Deploy sha-7418016 to production
  Job succeeded

One deploy at a time

Two pushes close together could start two production deploys at once, and they might fight. Add concurrency with a group named after the environment, so deploys queue up, one after another. For deploys, don't cancel the run in progress. Let it finish, then start the next.

keyvalue
concurrency:group: production
Queueone deploy at a time
cancel-in-progressfalse for deploys
Newest winsolder pending runs drop

Summary

Staging, a smoke test, then production with approval, using one artifact. That's continuous delivery. Next, a quiz on delivery.

Key takeaways
  • Staging first
  • Same SHA
  • Approval for prod
Next up: Delivery Quiz — test yourself