Environment & Secrets
Storing API keys safely, deployment environments, and required reviewers
Skills taught: cicd.secrets, cicd.environments, cicd.env-vars, cicd.deployment-protection
Secrets — never put API keys in code
Secrets are encrypted values stored in GitHub (Settings → Secrets and variables → Actions). They're injected as environment variables in workflows and masked in logs — if a secret value accidentally appears in output, GitHub replaces it with ***. They are never visible after being saved.
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Secrets are accessed via the secrets context
- name: Deploy to server
env:
SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
SERVER_HOST: ${{ secrets.SERVER_HOST }}
run: |
echo "$SSH_KEY" > /tmp/key && chmod 600 /tmp/key
ssh -i /tmp/key deploy@$SERVER_HOST 'cd /app && docker compose pull && docker compose up -d'
# Many actions accept secrets directly in 'with:'
- name: Push to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# GITHUB_TOKEN is a special auto-generated token — no setup needed
# Has write access to the repo, packages, and PRs
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
files: dist/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Environment variables — different scopes
# Workflow-level env (available to all jobs and steps)
env:
NODE_ENV: production
API_URL: https://api.myapp.com
jobs:
deploy:
runs-on: ubuntu-latest
# Job-level env (overrides workflow-level for this job)
env:
DATABASE_URL: ${{ secrets.PROD_DATABASE_URL }}
steps:
- name: Build
# Step-level env (highest priority, only for this step)
env:
BUILD_TARGET: production
run: npm run build:$BUILD_TARGET
# vars context: non-secret config variables (set in repo settings)
# Use for non-sensitive config that varies per environment
- name: Deploy
run: echo "Deploying to ${{ vars.DEPLOY_REGION }}"
Deployment environments — protection rules
# In GitHub: Settings → Environments → New environment
# Configure: required reviewers, deployment branches, environment secrets
jobs:
deploy-staging:
runs-on: ubuntu-latest
environment: staging # uses 'staging' environment (auto-created)
steps:
- run: echo "Deploying to staging..."
deploy-production:
runs-on: ubuntu-latest
needs: deploy-staging
# 'production' environment can have required reviewers:
# → deploy-production won't proceed until a reviewer approves
environment: production
steps:
- run: echo "Deploying to production..."
# With environment-specific secrets:
# Staging environment has: DEPLOY_HOST=staging.myapp.com
# Production environment has: DEPLOY_HOST=myapp.com
# The right secret is automatically used based on the environment: field
jobs:
deploy:
environment: ${{ inputs.environment }} # from workflow_dispatch input
steps:
- run: ssh deploy@${{ secrets.DEPLOY_HOST }} './deploy.sh'
Tip: Use environment-specific secrets for staging vs production. Create a 'staging' environment and a 'production' environment in GitHub. Give each the same secret names but different values (e.g. DEPLOY_HOST). The right values are automatically injected based on the environment: field in your job — no if-conditions needed.
Frequently Asked Questions
What is the difference between secrets and vars?
'secrets' are encrypted and masked in logs — use for API keys, passwords, tokens. 'vars' (variables) are stored in plain text and visible in logs — use for non-sensitive config like region names, image registries, feature flags. Both can be set at repo level, environment level, or organization level.