AI Hints
Home›CI/CD with GitHub Actions Tutorial›Environments and Approvals
CI/CD with GitHub Actions · Lesson 85 of 138

Environments

Staging and production.

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

Most teams deploy to more than one place. A staging environment that copies production, for a final check. And production, where real users are. GitHub has a feature for this, called environments.

Environments
From the video: Environments
  • Staging for a final check
  • Production for users
  • GitHub environments

Jobs with environments

Each job names its environment. The staging job deploys to staging. The production job needs staging, so it only runs after staging succeeds. Its environment also has a URL, which GitHub shows as a link on the run.

.github/workflows/deploy.ymlyaml
name: Deploy
on:
  push:
    branches: [main]
jobs:
  staging:
    runs-on: ubuntu-latest
    environment: staging
    steps:
      - run: echo "Deploying to staging"
  production:
    needs: staging
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://shop.example.com
    steps:
      - run: echo "Deploying to production"

Running it

Our helper runs staging, then production, and prints each job's environment. The real rules for an environment, like approvals and its secrets, live in the GitHub settings, so here we just see the order.

Terminal
run-steps .github/workflows/deploy.yml
Workflow: Deploy  (event: push, branch: main)
Job: staging
  Environment: staging
  ✓ Run echo "Deploying to staging"
      Deploying to staging
  Job succeeded
Job: production
  Environment: production
  ✓ Run echo "Deploying to production"
      Deploying to production
  Job succeeded

What an environment gives you

In the repository settings, each environment can have its own secrets and variables. Protection rules, like required reviewers and wait timers. And branch rules, so only main can deploy to production. Plus a history of every deployment.

keyvalue
Secretsper environment
Reviewersapprove before deploy
Branch rulesonly main → prod
Historyevery deployment

Summary

Environments give each deploy target its own rules and secrets. Staging first, then production. Next, requiring approval before production.

Key takeaways
  • environment: staging
  • needs: staging
  • url: on the run
Next up: Approvals — a human says go