Github Actions
Github Actions
1. Workflow Configuration:
Workflows are defined in YAML files in the .github/workflows
directory
Workflow files have a .yml or .yaml extension
name: CI language-yaml
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: |
echo "Running tests..."
2. Triggers:
Trigger workflows based on events like push , pull_request ,
schedule , etc.
on: language-yaml
push:
branches: [ main ]
pull_request:
branches: [ main ]
schedule:
- cron: '0 0 * * *'
jobs: language-yaml
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
4. Runners:
Specify the runner environment for each job using runs-on
jobs: language-yaml
build:
runs-on: ubuntu-latest
5. Actions:
Use pre-built actions from the GitHub Actions Marketplace or
create custom actions
Reference actions using the uses keyword
steps: language-yaml
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
6. Environment Variables:
Set environment variables for steps using env
steps: language-yaml
- name: Set environment variable
env:
API_KEY: ${{ secrets.API_KEY }}
7. Secrets:
Store sensitive information as encrypted secrets in the
repository settings
Access secrets using the secrets context
steps: language-yaml
- name: Access secret
run: echo ${{ secrets.API_KEY }}
8. Conditions:
Use conditions to control the execution of steps or jobs
steps: language-yaml
- name: Run only on main branch
if: github.ref == 'refs/heads/main'
run: echo "Running on main branch"
9. Matrix Builds:
Run jobs with different configurations using a matrix
jobs: language-yaml
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
10. Artifacts:
Upload and download build artifacts
steps: language-yaml
- uses: actions/upload-artifact@v2
with:
name: my-artifact
path: build/
- uses: actions/download-artifact@v2
with:
name: my-artifact
Regularly review and update your workflows to ensure they are up to date
with
the latest best practices and security recommendations.