Skip to content

GitHub Actions Cheat Sheet

Github Actions Official Documentation

Udemy Course References GitHub

# .github/workflows/main.yml

name: CI/CD Pipeline

# Define when this workflow should run
on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  schedule:
    - cron: '0 0 * * *'  # Runs every day at midnight

jobs:
  # Job to check out the repository
  checkout:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

  # Job to set up Node.js with environment variables and caching
  setup-node:
    runs-on: ubuntu-latest
    env:
      NODE_ENV: development
      API_URL: ${{ secrets.API_URL }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'
          cache: 'npm'

      - name: Install dependencies
        run: npm install

      - name: Run tests
        run: npm test

  # Job to build and deploy a Docker image with concurrency control
  build-and-deploy:
    runs-on: ubuntu-latest
    needs: checkout  # Depends on the checkout job
    concurrency: build-deploy-${{ github.ref }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and push Docker image
        run: |
          docker build -t myapp:latest .
          docker tag myapp:latest ${{ secrets.DOCKER_USERNAME }}/myapp:latest
          docker push ${{ secrets.DOCKER_USERNAME }}/myapp:latest

  # Job to deploy to AWS S3 (for static websites)
  deploy-s3:
    runs-on: ubuntu-latest
    needs: checkout  # Depends on the checkout job
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Install AWS CLI
        run: sudo apt-get install awscli

      - name: Deploy to S3
        run: |
          aws s3 sync ./public s3://my-bucket-name --delete
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

  # Job to run linting with artifact uploading
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - name: Install dependencies
        run: npm install

      - name: Run linter
        run: npm run lint

      - name: Upload lint results
        uses: actions/upload-artifact@v3
        with:
          name: lint-results
          path: ./lint-results

  # Job to build the project with outputs
  build:
    runs-on: ubuntu-latest
    outputs:
      build-path: ${{ steps.build.outputs.build-path }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - name: Install dependencies
        run: npm install

      - name: Build the project
        id: build
        run: npm run build
        outputs:
          build-path: ${{ steps.build.outputs.build-path }}

  # Job to run end-to-end tests
  e2e-tests:
    runs-on: ubuntu-latest
    needs: build  # Depends on the build job
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - name: Install dependencies
        run: npm install

      - name: Start the application
        run: npm start &

      - name: Run end-to-end tests
        run: npm run e2e

  # Job to deploy to GitHub Pages
  deploy-gh-pages:
    runs-on: ubuntu-latest
    needs: build  # Depends on the build job
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - name: Install dependencies
        run: npm install

      - name: Build the project
        run: npm run build

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./public

  # Job to use a custom action
  custom-action-job:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Custom Action Step
        uses: owner/repo@v1
        with:
          input1: value1
          input2: value2
        env:
          CUSTOM_ENV_VAR: ${{ secrets.CUSTOM_ENV_VAR }}

# Example of reusable workflow
name: reusable-workflow
on:
  workflow_call:
    inputs:
      name:
        description: 'Person to greet'
        required: true
        type: string
    secrets:
      GREETING:
        required: true

jobs:
  greeting-job:
    runs-on: ubuntu-latest
    steps:
      - name: Greeting
        run: echo "Hello, ${{ inputs.name }}. ${{ secrets.GREETING }}"