DEV Community

Cover image for GitHub Actions Explained
Faizan
Faizan

Posted on

GitHub Actions Explained

In the rapidly evolving landscape of software development, the ability to automate testing and deploy applications efficiently stands as a cornerstone of success. GitHub Actions, a powerful feature within the GitHub platform, has emerged as a pivotal tool in this regard. By enabling developers to create custom workflows for continuous integration (CI) and continuous deployment (CD), GitHub Actions simplifies the complexities of the CI/CD pipelines, fostering a culture of continuous improvement. Leveraging GitHub workflows, automation, and the GitHub marketplace’s vast array of pre-built and custom actions, developers can streamline their development processes, ensuring that integration and deployment are as seamless as possible.

This comprehensive guide aims to unfold the multifaceted capabilities of GitHub Actions, providing readers with a detailed roadmap from the basics to more advanced features.

What You Need to Know About GitHub Actions

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that run tests whenever you push a change to your repository, or that deploy merged pull requests to production. GitHub Actions can automate your workflow, allowing you to build, test, and deploy your code right from GitHub, saving you a lot of time and effort.

Automate Workflows

Automate, customize and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you'd like, including CI/CD, and combine actions in a completely customized workflow.

For example, you can use GitHub Actions to automatically build and test your code every time you push a commit to GitHub, ensuring that your code is always up-to-date and working as expected.

Customizable and Integrative

GitHub Actions are highly customizable. You can create your actions or use actions from the GitHub Marketplace to build workflows that meet your specific needs. GitHub Actions integrates seamlessly with other GitHub features, such as pull requests and issues, making it easy to manage your entire workflow in one place.

Community and Affordability

GitHub Actions has a large and active community. You can find many pre-built actions in the GitHub Marketplace, and you can also share your actions with the community. GitHub Actions is free for public repositories, and you get 2,000 free minutes of build time per month for private repositories, making it an affordable option for developers of all sizes.

Getting Started

GitHub provides starter workflows for a variety of languages and tooling. For instance, you can publish Node.js packages to a registry as part of your continuous integration (CI) workflow, or create a continuous integration (CI) workflow to build and test your PowerShell project. To get started, you can use the user interface of GitHub.com to add a workflow that demonstrates some of the essential features of GitHub Actions.

Building Your First Workflow

To get started with GitHub Actions, you'll need to set up the environment, create a basic YAML file, and test the workflow. Here's a step-by-step guide:

Setting Up the Environment

  1. Create a new directory named .github/workflows within your repository. This is where GitHub will look for workflow files.

  2. Inside the .github/workflows directory, create a new YAML file with an .yml or .yaml extension. You can give it any name you prefer, such as github-actions-demo.yml .

Creating a Basic YAML File

3. Copy the following YAML code into your newly created file :

on: [push]

jobs:
  build:
    name: Hello world
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Write a multi-line message
        run: |
          echo This demo file shows a
          echo very basic and easy-to-understand workflow

Enter fullscreen mode Exit fullscreen mode

This basic workflow is triggered whenever code is pushed to the repository (on: [push]). It defines a single job build that runs on the latest Ubuntu runner (runs-on: ubuntu-latest). The job consists of two steps:

  • The first step checks out the repository code using the actions/checkout@v2 action.

  • The second step runs a multi-line Bash script that prints a message to the console.

4. Commit the YAML file to your repository's default branch.

Testing the Workflow

5. After committing the workflow file, navigate to the "Actions" tab in your repository on GitHub.com. You should see your new workflow listed there.

6. Click on the workflow to view its execution details. The workflow should run automatically after your commit if everything is set up correctly.

7. Expand the job run to see the output of each step. You should see the message you defined in the run step.

By following these steps, you've successfully created and tested your first GitHub Actions workflow. You can now build upon this foundation to automate more complex tasks, such as running tests, building and deploying applications, and more.

Detailed Breakdown of Components

A workflow is a configurable automated process that will run one or more jobs. Workflows are defined by a YAML file checked into your repository and will run when triggered by an event in your repository, or they can be triggered manually, or at a defined schedule.

Workflows, Jobs, and Steps

A workflow must contain one or more events that will trigger it, one or more jobs that will execute on a runner machine and run a series of steps, and each step can either run a script or an action. A job is a set of steps in a workflow executed on the same runner, where each step is either a shell script or an action. An action is a custom application that performs a complex but frequently repeated task, helping reduce repetitive code in workflow files.

Events and Triggers

An event is a specific activity that triggers a workflow run, such as creating a pull request, opening an issue, or pushing a commit. Workflows can also be triggered on a schedule, by posting to a REST API, or manually. There are various types of events, including repository events like creating or modifying issues, pull requests, releases, discussions, and more.

GitHub provides filters to control when a workflow should run based on activity types, branches, file paths, and other criteria. For example, the branches filter causes a workflow to run only when a push to a specific branch occurs, and the paths filter allows it to run workflows based on changed file paths.

Actions and Runners

A runner is a server that runs your workflows when triggered, with each runner executing a single job at a time. GitHub provides Ubuntu Linux, Microsoft Windows, and macOS runners, where each workflow run executes in a fresh, newly-provisioned virtual machine.

GitHub-hosted runners are available for public and private repositories, with different specifications and pricing models. For private repositories, jobs use the account's allotment of free minutes and are then charged per minute. GitHub also offers larger, managed virtual machines with more resources, static IP addresses, Azure private networking, and advanced features like GPU and ARM-powered runners.

The communication between GitHub and self-hosted runners works by the runner connecting to GitHub (outbound) and keeping the connection open, allowing events to be pushed to the runner through the established connection.

Advanced Techniques

Integrating with Third-Party Tools

When a third-party application requests access to your GitHub data, it will display the developer's contact information and the specific data being requested. Since the application is developed by a third party, GitHub does not have information on how the requested data will be used, so it is important to contact the developer if you have any concerns.

Applications can have read or write access to your GitHub data. Read access allows an application to view your data, while write access permits it to modify your data. Scopes are named groups of permissions that an application can request to access public and non-public data. For example, if an app requests the user:email scope, it will have read-only access to your private email addresses. However, currently, you cannot scope source code access to read-only.

It is recommended to regularly review your authorized integrations and remove any unused applications or tokens to minimize potential risks. Additionally, you should use credentials with minimal required permissions and be mindful that any user with write access to your repository has read access to all configured secrets.

Managing Workflow Secrets

Sensitive values should never be stored as plaintext in workflow files but rather as secrets. Secrets are encrypted before reaching GitHub, helping minimize risks related to accidental logging. GitHub uses a mechanism to redact secrets from run logs, but this redaction is not guaranteed due to the various transformations secrets can undergo.

To ensure proper secret redaction and limit associated risks, follow these best practices :

  1. Avoid structured data as secrets: Structured data like JSON or XML can cause redaction failures since redaction relies on finding exact matches.

  2. Register all used secrets: If a secret is used to generate another sensitive value, register that value as a secret to ensure redaction.

  3. Audit secret handling: Review the source code and actions used in your workflows to ensure secrets are not sent to unintended hosts or printed to logs.

  4. Use minimally scoped credentials: Ensure that the credentials used in workflows have the least required privileges.

  5. Audit and rotate secrets: Periodically review and remove unnecessary secrets, and rotate secrets to reduce the window of potential compromise.

  6. Consider requiring reviews for access: You can use required reviewers to control access to environment secrets.

GitHub-hosted runners take measures to mitigate security risks, such as providing software bills of materials (SBOMs) and blocking access to malicious sites. However, self-hosted runners can be persistently compromised by untrusted code and should be used cautiously, especially for public repositories. To improve security, you can use the REST API to create ephemeral, just-in-time (JIT) runners that perform at most one job before being automatically removed.

Scaling with Self-Hosted Runners

You can automatically scale the number of self-hosted runners in your environment based on webhook events with a particular label. However, GitHub recommends implementing autoscaling with ephemeral self-hosted runners, as persistent runners cannot guarantee that jobs are not assigned during the shutdown.

To add an ephemeral runner, include the --ephemeral parameter when registering the runner using config.sh . The runner will be automatically de-registered after processing one job, and you can then automate the process of wiping the runner.

You can create an autoscaling environment by using payloads received from the workflow_job webhook, which contains information about the stages of a workflow job's lifecycle. You can register and delete repository, organization, and enterprise self-hosted runners using the API and appropriate authentication methods.

When adding self-hosted runners, consider the level of ownership and management. If a centralized team will own the runners, add them at the highest mutual organization or enterprise level. If each team will manage its runners, add them at the highest level of team ownership, such as the organization level.

Conclusion

From navigating the basics and crafting your first workflow to diving deep into advanced techniques and security best practices, the guide has aimed to equip you with the necessary knowledge to automate, integrate, and optimize your software development processes efficiently. Emphasizing GitHub Actions' adaptability, the discussion highlighted how this powerful tool can streamline workflows, foster continuous improvement, and enable a culture of innovation by leveraging automation, seamless integration, and community-driven enhancements.

FAQs

1. What exactly are GitHub Actions?
GitHub Actions is a CI/CD platform that automates your software's build, test, and deployment processes. It enables you to create workflows that automatically build and test every pull request to your repository or deploy merged pull requests to production environments.

2. How do GitHub Actions differ from other CI/CD tools?
While both GitHub Actions and other CI/CD tools like GitLab CI/CD automate software deployment processes, they differ in usability and configuration. GitLab CI/CD has a visual editor for simple workflows and requires YAML for more complex setups. GitHub Actions uses YAML extensively, supported by a large community that provides prebuilt workflows and tools to ease configuration.

3. What types of actions can you create with GitHub Actions?
In GitHub Actions, you can develop Docker container actions, JavaScript actions, and composite actions. Each action type requires a metadata file that specifies the inputs, outputs, and the main entry point of the action. The metadata file must be named action.

4. What is the relationship between GitHub Actions and workflows?
GitHub Actions is a tool that automates workflows within the GitHub platform, where you can also manage code and collaborate on pull requests and issues. A workflow in GitHub Actions refers to an automated sequence of operations set up in your GitHub repository to handle tasks like building, testing, and deploying applications.


Thank you for reading! If you have any feedback or notice any mistakes, please feel free to leave a comment below. I'm always looking to improve my writing and value any suggestions you may have. If you're interested in working together or have any further questions, please don't hesitate to reach out to me at fa1319673@gmail.com.

Top comments (0)