GitHub actions yml example

GitHub Actions is a powerful automation and workflow platform provided by GitHub. It allows you to define custom workflows and automate various tasks within your software development projects. With GitHub Actions, you can build, test, and deploy your code directly from your GitHub repositories.

GitHub actions example YAML file

With GitHub Actions, you can define custom workflows as code using YAML files, known as workflow configuration files. These workflows can be triggered by events like code pushes, pull requests, issue comments, or scheduled intervals. You have full control over when and how your workflows are triggered. Here’s an example of a GitHub Actions workflow configuration file (.github/workflows/main.yml) in YAML format:

name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

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

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.x'

      - name: Install dependencies
        run: pip install -r requirements.txt

      - name: Run tests
        run: pytest

      - name: Build and Deploy
        run: |
          if [ ${{ github.event_name }} = 'push' ]; then
            # Build and deploy the application
            ./deploy.sh
          else
            echo "Skipping deployment for pull request"
          fi

In this example:

  • The workflow is triggered on push events to the main branch and pull_request events targeting the main branch.
  • The workflow has a single job called build that runs on the latest version of Ubuntu.
  • The steps of the job are as follows:
  • Check out the repository using the actions/checkout action.
  • Set up Python using the actions/setup-python action, specifying the desired Python version.
  • Install dependencies by running the pip install command, assuming the dependencies are specified in a requirements.txt file.
  • Run tests using the pytest command.
  • Perform a build and deployment using a custom deploy.sh script if the event is a push event. For pull requests, it skips the deployment step.

You can customize this example to suit your specific needs. For example, you can modify the trigger conditions, add more steps, incorporate additional actions, or adjust the deployment process to match your application’s requirements.

Reference: https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions