Reader small image

You're reading from  GitHub Actions Cookbook

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781835468944
Edition1st Edition
Concepts
Right arrow
Author (1)
Michael Kaufmann
Michael Kaufmann
author image
Michael Kaufmann

Michael Kaufmann believes that developers and engineers can be happy and productive at work. He loves DevOps, GitHub, Azure, and modern work. Microsoft has awarded him with the title Microsoft Regional Director (RD) and Microsoft Most Valuable Professional (MVP) – the latter in the category of DevOps and GitHub. Michael is also the founder and managing director of Xebia Microsoft Services, Germany – a consulting company that helps its customers become digital leaders by supporting them in their cloud, DevOps, and digital transformation. Michael shares his knowledge in books, training, and as a frequent speaker at international conferences.
Read more about Michael Kaufmann

Right arrow

Automate Tasks in GitHub with GitHub Actions

In this chapter, we will focus on learning how to automate common tasks in GitHub with GitHub Actions using GitHub Issues. This is often called IssueOps. In this chapter, we’ll create a simple solution that allows you to manage repositories. This does not make sense for a personal account, but the solution should be easily adaptable for an enterprise context in which governance of repositories – such as naming conventions and permissions – is an important topic. The chapter contains the following recipes:

  • Creating an issue template
  • Using the GitHub CLI and GITHUB_TOKEN to access resources
  • Using environments for approvals and checks
  • Reusable workflows and composite actions

Technical requirements

If you want to follow along with all the details, you will need a GitHub organization. You can create one in GitHub for free. You can also just use your personal account – it will work the same, but it is less of a real-world scenario. You can author the workflows in Visual Studio Code or in the browser – whatever feels right for you.

Creating an issue template

In this recipe, you will create a simple issue template that you can later extend to gather user input for your IssueOps workflows.

Getting ready…

We’ll add the issue template to the repository that you have used in previous chapters. You can clone the repository locally and work in Visual Studio Code or you can do this part in the browser – it doesn’t matter. You can follow the examples in my repository (https://github.com/wulfland/GitHubActionsCookbook).

How to do it…

  1. Create a new file called .github/ISSUE_TEMPLATE/repo_request.yml in the repository. GitHub will automatically treat the file in the .github/ISSUE_TEMPLATE folder as an issue template as long as it is a YAML or Markdown file.
  2. Add a name and description for the template:
    name: '🗒️ Repository Request'
    description: 'Request a new repository.'
  3. Prefill the title of the new issue with a default value:
    title...

Using the GitHub CLI and GITHUB_TOKEN to access resources

In this recipe, we will parse the issue from the previous chapter and interact with the issue using the GitHub CLI in the workflow.

Getting ready…

You will need the issue template from the previous chapter. You can create the workflow either in Visual Studio Code or directly in GitHub.

How to do it…

  1. Create a new workflow .github/workflows/issue-ops.yml and name it issue-ops:
    # Issue ops
    name: issue-ops
  2. Use the issues trigger for the workflow. Note that we are not using the created or edited events but rather labeled. This allows users to relabel issues when modifying the request:
    on:
      issues:
        types: [labeled]
  3. Add an issue-ops job:
    jobs:
        issue-ops:

    We only want to run this job for specific labels. Add a condition like the following to the job:

    if: ${{ github.event.label.name == 'repo-request' }}

    The job can run on the latest Ubuntu...

Using environments for approvals and checks

In this recipe, we are going to use environment approvals to acquire approval before creating the repository. We will also use a GitHub App to authenticate as the repository creation normally happens in the organization scope and cannot be done with GITHUB_TOKEN. You must either use a GitHub App or a personal access token (PAT) with the right scopes.

Getting ready…

Make sure you have completed the previous recipe and continue in the same repository.

How to do it…

  1. In your repository, go to Setting | Environments and click New environment.
  2. Name the environment repo-creation and click Configure environment.
  3. Add yourself as Required reviewer and don’t allow administrators to bypass the rule (see Figure 5.7).
Figure 5.7 – Configuring the environment protection rules

Figure 5.7 – Configuring the environment protection rules

  1. Click Save protection rules.
  2. The next step is to create an app to authenticate. I recommend...

Reusable workflows and composite actions

If you start automating with IssueOps, workflows get very complex very fast. You will end up with a lot of if clauses on jobs and steps. To keep these solutions maintainable, you can use composite actions or reusable workflows to not only reuse functionality but also break down complex workflows into smaller parts.

We have already covered composite actions in previous chapters. In this recipe, we will use a reusable workflow to add the delete functionality to our IssueOps solution.

Getting ready…

Make sure you have completed the previous recipes in this chapter.

How to do it…

  1. Create a new delete-repo.yml workflow file.
  2. As the trigger, we use the workflow_call trigger. This indicates that the workflow is a reusable workflow. Define inputs needed by our workflow:
    on:
      workflow_call:
        inputs:
          REPO_NAME:
          ...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
GitHub Actions Cookbook
Published in: Apr 2024Publisher: PacktISBN-13: 9781835468944
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
Michael Kaufmann

Michael Kaufmann believes that developers and engineers can be happy and productive at work. He loves DevOps, GitHub, Azure, and modern work. Microsoft has awarded him with the title Microsoft Regional Director (RD) and Microsoft Most Valuable Professional (MVP) – the latter in the category of DevOps and GitHub. Michael is also the founder and managing director of Xebia Microsoft Services, Germany – a consulting company that helps its customers become digital leaders by supporting them in their cloud, DevOps, and digital transformation. Michael shares his knowledge in books, training, and as a frequent speaker at international conferences.
Read more about Michael Kaufmann