GitHub Actions Tutorial with Top 10 Action Examples

GitHub Actions is a powerful automation platform provided by GitHub that allows you to automate your software development workflows. You can use it to build, test, and deploy your code automatically whenever changes are pushed to your GitHub repository. In this tutorial, we'll explore the basics of GitHub Actions and provide you with the top 10 action examples to get started.

Table of Contents

  1. Introduction to GitHub Actions
  2. Setting Up GitHub Actions
  3. Workflow Configuration
  4. Top 10 GitHub Action Examples
  5. Conclusion

Introduction to GitHub Actions

GitHub Actions enables you to automate various tasks, known as workflows, based on specific events within your GitHub repository. These events can include code pushes, pull requests, issue creation, and more. Actions are defined in YAML files and can be triggered by events or scheduled to run at specific intervals.

Setting Up GitHub Actions

To start using GitHub Actions, follow these steps:

  1. Access Your Repository: Open your GitHub repository.

  2. Navigate to Actions: Click on the "Actions" tab in your repository.

  3. Create a New Workflow: Click on the "New workflow" button, or select a template to get started.

Workflow Configuration

GitHub Actions workflows are defined in YAML files placed in the .github/workflows directory in your repository. A simple workflow configuration might look like this:

name: CI/CD

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Install dependencies
      run: npm install

    - name: Run tests
      run: npm test

    - name: Deploy
      run: ./deploy.sh

This YAML file specifies a workflow that triggers on pushes to the main branch. It runs on the latest Ubuntu image, checks out the code, installs dependencies, runs tests, and deploys the code.

Top 10 GitHub Action Examples

Below are examples for each of the top 10 GitHub Actions mentioned earlier:

1. Build and Test

name: Build and Test

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Install dependencies
      run: npm install

    - name: Run tests
      run: npm test

This workflow triggers on pushes to the main branch. It checks out the code, installs dependencies (assuming a Node.js project), and runs tests.

2. Deploy to Production

name: Deploy to Production

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Deploy to Production
      run: ./deploy.sh

This workflow deploys your code to production servers when changes are pushed to the main branch. You need to customize the deployment script (deploy.sh) as per your project's needs.

3. Continuous Integration with Docker

name: Continuous Integration with Docker

on:
  push:
    branches:
      - main

jobs:
  build-and-test:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Build and Test with Docker
      uses: docker/build-push-action@v2
      with:
        context: .
        push: false
        tags: my-image:latest

This workflow builds and tests your code in a Docker container on pushes to the main branch. It uses the docker/build-push-action to manage the Docker image.

4. Code Quality Checks

name: Code Quality Checks

on:
  push:
    branches:
      - main

jobs:
  code-quality:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Run Code Linters
      run: npm run lint

This workflow checks the code quality by running code linters when changes are pushed to the main branch (assuming an npm-based project).

5. Automated Release

name: Automated Release

on:
  push:
    tags:
      - v*

jobs:
  release:
    runs-on: ubuntu-latest

    steps:
    - name: Create Release
      id: create_release
      uses: softprops/action-gh-release@v1
      with:
        files: ./dist/*
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

This workflow automatically creates GitHub releases when you push a tag starting with v. It uploads files from the ./dist directory as part of the release.

6. Scheduled Jobs

name: Scheduled Jobs

on:
  schedule:
    - cron: '0 0 * * *' # Run daily at midnight

jobs:
  nightly-job:
    runs-on: ubuntu-latest

    steps:
    - name: Run Nightly Backup
      run: ./backup.sh

This workflow schedules a job to run daily at midnight using a cron expression. It runs a backup script (backup.sh) as an example.

7. Notify on Slack

name: Notify on Slack

on:
  push:
    branches:
      - main

jobs:
  notify:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Notify on Slack
      uses: rtCamp/action-slack-notify@v2
      with:
        status: ${{ job.status }}
        slack_webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}

This workflow sends a notification to a Slack channel when changes are pushed to the main branch.

8. Dependency Updates

name: Dependency Updates

on:
  schedule:
    - cron: '0 0 * * *' # Run daily at midnight

jobs:
  update-dependencies:
    runs-on: ubuntu-latest

    steps:
    - name: Update Dependencies
      run: composer update

This workflow schedules a job to run daily at midnight and updates project dependencies using Composer as an example.

9. PR Comment

name: PR Comment

on:
  pull_request:
    types:
      - opened
      - synchronize

jobs:
  comment:
    runs-on: ubuntu-latest

    steps:
    - name: Comment on PR
      uses: actions/github-script@v5
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
        script: |
          const prNumber = context.payload.pull_request.number;
          github.issues.createComment({
            issue_number: prNumber,
            owner: context.repo.owner,
            repo: context.repo.repo,
            body: 'Thanks for the pull request! :tada:'
          });

This workflow adds a comment to a pull request when it is opened or synchronized.

10. Cross-Platform Testing

name: Cross-Platform Testing

on:
  push:
    branches:
      - main

jobs:
  cross-platform-test:
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macOS-latest]

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Run Tests on ${{ matrix.os }}
      run: |
        if [ "$os" = "ubuntu-latest" ]; then
          # Run tests on Ubuntu
        elif [ "$os" = "windows-latest" ]; then
          # Run tests on Windows
        elif [ "$os" = "macOS-latest" ]; then
          # Run tests on macOS
        fi

This workflow tests your code on multiple operating systems (Ubuntu, Windows, and macOS) when changes are pushed to the main branch.

Feel free to adapt these examples to your specific project requirements by customizing the steps, scripts, and event triggers in your GitHub Actions workflows.

Test your code on multiple operating systems, such as Windows, macOS, and Linux, to ensure compatibility.

Conclusion

GitHub Actions is a powerful automation tool that can streamline your software development workflows. By setting up workflows and using action examples like those mentioned in this tutorial, you can automate tasks, improve code quality, and enhance collaboration among your development team. Explore and customize GitHub Actions to meet your specific project requirements and make your development process more efficient and reliable.