Reader small image

You're reading from  Mastering GitHub Actions

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781805128625
Edition1st Edition
Concepts
Right arrow
Author (1)
Eric Chapman
Eric Chapman
author image
Eric Chapman

Eric Chapman holds the position of Senior Delivery and Engineering Manager at a leading retailer in home improvement and trade in Australia and New Zealand. He primarily oversees integration, encompassing platforms such as API Gateway, EventMesh, authorization systems, developer portals, and extract, transform and load (ETL) platforms. Eric leads a team with a broad range of responsibilities and skills, overseeing all business areas. Previously, Eric and his team were instrumental in designing and developing an in-house point-of-sale system. This singular application accommodated four countries' tax and auditing requirements, supported multiple payment processing gateways, and incorporated a range of unique market-leading features.
Read more about Eric Chapman

Right arrow

Exploring Workflows

GitHub workflows are the foundation of GitHub Actions, enabling you to automate various tasks in your software development process, such as building, testing, and deploying code. They can also be helpful in managing processes and orchestrating complex automation with other functions within the GitHub platform.

A GitHub workflow is defined using a YAML configuration file in your repository’s .github/workflows directory. By reading this chapter, you will be best placed to follow along with the rest of this book and use examples to test out these concepts for use. In this chapter, we are going to cover the following main topics:

  • Exploring the capabilities of workflows
  • Best practices for creating maintainable and readable workflows
  • Jobs and how they work
  • Running workflow jobs as containers and services

Technical requirements

In this chapter, we’re going to need a repository to create workflows and try out the functionality. Create and initialize a repository named scratchpad in the organization we created in Chapter 1.

We’re going to need a Node.js application in this chapter to demonstrate how some of the features work. Copy the application in the GitHub repository (https://github.com/PacktPublishing/Mastering-GitHub-Actions) under Chapter 2 in the app directory into the repository.

Exploring workflow capabilities

GitHub workflows offer a wide range of capabilities, including niche features catering to specific use cases or scenarios. In this section, we’re going to talk through the capabilities in detail we mentioned before, which will provide you with a foundation-level knowledge of the capabilities and the chance to apply these new learnings.

Before we jump into the specifics, here is a quick reminder on the structure of a workflow and what it primarily consists of:

  • Events/triggers: Workflows are triggered by specific events, such as pushing code to a branch, creating a pull request, calls via the GitHub API, or scheduling a cronjob.
  • Jobs: A workflow can consist of multiple jobs, each running a sequence of steps. Jobs can run in parallel or sequentially, depending on your requirements.
  • Steps: Each job consists of steps that perform individual tasks, such as running a script, checking out code, or using a pre-built action.

Now...

Workflow structuring and good habits

Structuring your GitHub Actions workflows and establishing good habits early on is critical for ensuring maintainability, readability, and efficiency. Here are some best practices:

  • Keep workflows focused and concise: It can be tempting to make a single workflow handle many aspects of your CI/CD process, but this can lead to workflows that are complex, hard to maintain, and difficult to understand.

    Instead, each workflow should have a single, clear purpose. For example, one workflow might be responsible for linting and running tests whenever code is pushed. Another workflow could handle deploying your application to a staging environment when a pull request is merged. This division of responsibilities makes it easier to understand the role of each workflow, simplifies troubleshooting when things go wrong, and allows for more flexible customization since changes in one workflow won’t affect others. It also allows you to take advantage...

Exploring workflow jobs

In this section, we’ll explore how you can use containers and services to run jobs in your GitHub Actions workflows. This powerful feature allows you to control the environment in which your jobs run and can be incredibly useful when your jobs have specific dependencies. But before we do this, let’s take a look at how jobs work.

Understanding how jobs work

In GitHub Actions, a workflow run is made up of one or more jobs. Jobs are defined in the workflow file and represent the tasks the workflow will perform. Each job runs in an environment specified by runs-on. Here’s a high-level look at how jobs work:

  • Run on a virtual host: Each job runs on a fresh instance of the virtual environment specified by runs-on. This could be GitHub-hosted runners, such as ubuntu-latest, windows-latest, and macos-latest, or self-hosted runners.
  • Run in parallel: By default, jobs run in parallel. If you need jobs to run sequentially, you can define...

Summary

In this comprehensive chapter, we dived deep into the world of GitHub Actions, focusing on key elements that make up an efficient and effective GitHub Actions pipeline. We began by exploring the fundamental building blocks of GitHub Actions: workflows and their event triggers. You learned about the capabilities of GitHub and how you can wire it up to respond to a variety of GitHub platform events. This knowledge will allow you to design workflows that react intelligently and dynamically to the specific context of your projects.

Next, we discussed best practices in writing workflows. We emphasized the importance of readability, modularity, and the efficient use of resources, which can significantly improve the maintainability and performance of your workflows. These practices serve as a compass to guide you through the complexity of large-scale projects and their pipelines.

The chapter then moved on to discuss jobs, an integral part of workflows. We looked at their capabilities...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering GitHub Actions
Published in: Mar 2024Publisher: PacktISBN-13: 9781805128625
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Eric Chapman

Eric Chapman holds the position of Senior Delivery and Engineering Manager at a leading retailer in home improvement and trade in Australia and New Zealand. He primarily oversees integration, encompassing platforms such as API Gateway, EventMesh, authorization systems, developer portals, and extract, transform and load (ETL) platforms. Eric leads a team with a broad range of responsibilities and skills, overseeing all business areas. Previously, Eric and his team were instrumental in designing and developing an in-house point-of-sale system. This singular application accommodated four countries' tax and auditing requirements, supported multiple payment processing gateways, and incorporated a range of unique market-leading features.
Read more about Eric Chapman