Github Actions

Photo by the blowup on Unsplash

Github Actions

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform offered by GitHub, a web-based platform for version control and collaboration on software development projects. GitHub Actions allows you to automate various aspects of your software development workflow, such as building, testing, and deploying your code.

Here are some key features and functionalities of GitHub Actions:

  1. Workflow Automation: GitHub Actions allows you to define custom workflows using YAML files in your repository. Workflows are composed of one or more jobs, and each job can consist of multiple steps. These workflows can be triggered by events such as code pushes, pull requests, or external events.

  2. Continuous Integration (CI): You can set up CI workflows to automatically build and test your code whenever changes are pushed to your repository. GitHub Actions provides a range of virtual machine environments and containers for running these tasks.

  3. Continuous Deployment (CD): With GitHub Actions, you can automate the deployment of your applications to various hosting platforms or cloud services like AWS, Azure, or GitHub Pages. This allows you to release new versions of your software with confidence.

  4. Event-Driven Triggers: GitHub Actions can be triggered by various events, including code commits, pull requests, issue comments, and custom webhooks. You can define which events should trigger specific workflows.

  5. Custom Actions: You can create reusable actions to encapsulate common tasks or steps in your workflows. These actions can be shared with the GitHub community or used privately within your organization.

  6. Marketplace: GitHub provides a marketplace where you can discover and use pre-built GitHub Actions created by the community to integrate with different services and tools.

  7. Secrets Management: GitHub Actions allows you to securely store and use sensitive information, such as API tokens and credentials, as secrets in your workflows.

  8. Matrix Builds: You can set up matrix builds to test your code against multiple versions of programming languages, dependencies, or platforms in parallel.

GitHub Actions simplifies and streamlines the automation of your software development processes, helping you save time and ensure the quality of your code. It is tightly integrated with GitHub repositories, making it a popular choice for many development teams for managing their CI/CD pipelines.

Components of Github Action

Diagram of an event triggering Runner 1 to run Job 1, which triggers Runner 2 to run Job 2. Each of the jobs is broken into multiple steps.

  1. Workflow: A workflow is a set of automated tasks defined in a YAML file (typically named main.workflow or .github/workflows/*.yml) in your repository. Workflows define the actions to be taken when specific events occur, such as code pushes, pull requests, or scheduled jobs.

  2. Jobs: A workflow is composed of one or more jobs, each of which represents a separate set of tasks that can run concurrently on the same runner (execution environment). Jobs are defined within the jobs section of the workflow YAML file.

  3. Steps: Jobs are made up of one or more steps, which are individual units of work within a job. Each step can execute a specific action, script, or command. Steps are defined within the steps section of a job and are executed sequentially.

  4. Actions: Actions are reusable units of code that can be included in your workflows to perform specific tasks or actions. Actions can be defined in your repository or sourced from the GitHub Marketplace or other repositories. You can use pre-built actions or create custom actions to suit your needs.

  5. Runners: Runners are the execution environments where your workflows run. GitHub provides virtual environments (GitHub-hosted runners) for common platforms, including Linux, macOS, and Windows. You can also set up your own self-hosted runners for more customized workflows or for environments that GitHub does not provide.

  6. Events: Events are specific occurrences that trigger workflows. These events can include code pushes, pull requests, issue comments, and more. Workflows are configured to run in response to one or more specific events.

  7. Workflow Files: Workflow files are written in YAML format and define the structure and behavior of your workflows. These files are stored in the .github/workflows/ directory in your repository and are used to specify which events trigger the workflow, the jobs to run, and the steps within those jobs.

  8. Contexts: GitHub Actions provides various context variables that contain information about the event, repository, and workflow run. You can access these context variables in your workflow to make decisions or customize actions based on the current context.

  9. Secrets: Secrets are encrypted environment variables that you can store in your repository's settings. These secrets can be used in your workflow to securely store sensitive information such as API keys, access tokens, and passwords.

  10. Artifacts: Artifacts are files or data produced by a workflow that you want to persist or pass between jobs. You can use artifacts to store build outputs, test results, or any other data that needs to be retained for further processing.

  11. Caching: Caching allows you to store and reuse dependencies or build artifacts between workflow runs to improve build performance. You can specify caching in your workflow to speed up repetitive tasks.

  12. Environment Variables: You can define environment variables within your workflow to store configuration settings or pass data between steps.

  13. Workflow Status: GitHub Actions provides a visual representation of the status of your workflows, jobs, and steps within the GitHub repository interface. You can monitor the progress and outcome of your workflows directly on GitHub.

Sample Workflow

GitHub Actions uses YAML syntax to define the workflow. Each workflow is stored as a separate YAML file in your code repository, in a directory named .github/workflows.

You can create an example workflow in your repository that automatically triggers a series of commands whenever code is pushed. In this workflow, GitHub Actions checks out the pushed code, installs the bats testing framework, and runs a basic command to output the bats version: bats -v.

  1. In your repository, create the .github/workflows/ directory to store your workflow files.

  2. In the .github/workflows/ directory, create a new file called learn-github-actions.yml and add the following code.

    YAML

     name: learn-github-actions
     run-name: ${{ github.actor }} is learning GitHub Actions
     on: [push]
     jobs:
       check-bats-version:
         runs-on: ubuntu-latest
         steps:
           - uses: actions/checkout@v4
           - uses: actions/setup-node@v3
             with:
               node-version: '14'
           - run: npm install -g bats
           - run: bats -v
    
  3. Commit these changes and push them to your GitHub repository.

Your new GitHub Actions workflow file is now installed in your repository and will run automatically each time someone pushes a change to the repository. To see the details about a workflow's execution history, see "Viewing the activity for a workflow run."

This GitHub Actions workflow is named "learn-github-actions." It is designed to check the version of the Bats testing framework and verify that it's correctly installed in the GitHub Actions runner environment. Here's a breakdown of the workflow:

  1. Name and Run Name:

    • name: learn-github-actions: This sets the name of the workflow to "learn-github-actions."

    • run-name: ${{ github.actor }} is learning GitHub Actions: The run-name specifies a dynamic name for each workflow run, incorporating the GitHub actor's username (the user or bot triggering the workflow) to make each run's name unique.

  2. Trigger Event:

    • on: [push]: This workflow is triggered by the push event, meaning it will run whenever code is pushed to the repository.
  3. Jobs:

    • The workflow defines a single job named "check-bats-version."

    • runs-on: ubuntu-latest: This job runs on the latest version of the Ubuntu runner provided by GitHub Actions.

  4. Steps:

    • uses: actions/checkout@v4: This step checks out the code from the repository using the actions/checkout action. It fetches the repository's code so that the workflow can work with it.

    • uses: actions/setup-node@v3: This step sets up a Node.js environment using the actions/setup-node action. It specifies Node.js version 14 as the desired version.

    • run: npm install -g bats: This step installs the Bats testing framework globally using npm. The -g flag ensures that Bats is available system-wide.

    • run: bats -v: Finally, this step runs the bats -v command to check the version of Bats installed. It verifies that Bats is correctly installed in the GitHub Actions runner environment and prints the version.

In summary, this workflow is triggered by code pushes to the repository, and its main purpose is to check the version of the Bats testing framework to ensure it's available for use in subsequent testing steps. The workflow is informative as it includes the actor's name in its run name, making it clear who initiated each workflow run.

More detailed explanation is give in https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions#create-an-example-workflow

What kind of Events can we configure

GitHub Actions can be triggered using a variety of events that occur within a GitHub repository. Some of the common events that can trigger GitHub Actions workflows include:

  1. Push Events: These events are triggered when code is pushed to a repository, such as pushing to a specific branch or creating a new branch.

  2. Pull Request Events: Actions can be triggered when pull requests are opened, updated, or closed. You can also specify conditions based on which pull requests trigger workflows, such as labels or branches.

  3. Issue Events: Actions can be triggered when issues are created, updated, or closed. Like with pull requests, you can specify conditions based on labels or other attributes.

  4. Release Events: When a new release is published for a repository, you can trigger actions to perform tasks like building and publishing release assets.

  5. Scheduled Events: You can schedule actions to run at specific times or intervals using cron syntax. This is useful for tasks like automated backups or regular maintenance.

  6. Webhooks: You can configure custom webhooks to trigger actions when specific events occur, even events outside of the standard GitHub event types.

  7. Deployment Events: Actions can be triggered when a deployment event occurs, such as deploying code to a specific environment.

  8. Workflow Dispatch: You can manually trigger workflows using the GitHub web interface or the GitHub API. This is useful for one-off or manual tasks.

  9. External Events: GitHub Actions can also be triggered by external events using the repository_dispatch event. This allows you to trigger actions from external systems or services.

  10. Repository and Organization Events: Actions can also be triggered by events related to the repository or organization itself, such as when a repository is archived or unarchived.