Skip
Arish's avatar

5. Secrets and Variables


GitHub Secrets

Store sensitive data securely.

Setting Secrets

  1. Go to Repository → Settings → Secrets
  2. Click "New repository secret"
  3. Add name and value

Using Secrets

yaml
1steps:
2  - name: Deploy
3    env:
4      DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
5      DATABASE_URL: ${{ secrets.DATABASE_URL }}
6    run: ./deploy.sh

Environment Variables

yaml
1env:
2  RAILS_ENV: test
3  
4jobs:
5  test:
6    env:
7      CI: true
8    steps:
9      - env:
10          NODE_ENV: test
11        run: npm test

GitHub Context Variables

yaml
1steps:
2  - run: |
3      echo "Repo: ${{ github.repository }}"
4      echo "Branch: ${{ github.ref }}"
5      echo "SHA: ${{ github.sha }}"
6      echo "Actor: ${{ github.actor }}"

Environment Protection

yaml
1jobs:
2  deploy:
3    environment: production  # Requires approval
4    steps:
5      - run: ./deploy.sh

Configure environments in Settings → Environments.