Book Image

Automating Workflows with GitHub Actions

By : Priscila Heller
Book Image

Automating Workflows with GitHub Actions

By: Priscila Heller

Overview of this book

GitHub Actions is one of the most popular products that enables you to automate development tasks and improve your software development workflow. Automating Workflows with GitHub Actions uses real-world examples to help you automate everyday tasks and use your resources efficiently. This book takes a practical approach to helping you develop the skills needed to create complex YAML files to automate your daily tasks. You'll learn how to find and use existing workflows, allowing you to get started with GitHub Actions right away. Moving on, you'll discover complex concepts and practices such as self-hosted runners and writing workflow files that leverage other platforms such as Docker as well as programming languages such as Java and JavaScript. As you advance, you'll be able to write your own JavaScript, Docker, and composite run steps actions, and publish them in GitHub Marketplace! You'll also find instructions to migrate your existing CI/CD workflows into GitHub Actions from platforms like Travis CI and GitLab. Finally, you'll explore tools that'll help you stay informed of additions to GitHub Actions along with finding technical support and staying engaged with the community. By the end of this GitHub book, you'll have developed the skills and experience needed to build and maintain your own CI/CD pipeline using GitHub Actions.
Table of Contents (14 chapters)
1
Section 1:Introduction and Overview of Technologies Used with GitHub Actions
4
Section 2: Advanced Concepts and Hands-On Exercises to Create Actions
9
Section 3: Customizing Existing Actions, Migrations, and the Future of GitHub Actions

Introduction to GitHub: creating a user account

GitHub is the largest code-hosting platform in the world. It uses Git as version control, and most activities happen on a repository hosted on GitHub. This and other features such as pull requests, project boards, and GitHub Actions allow software engineers, operations engineers, product managers, and everybody else involved in software development to collaborate in one place.

To start hosting code on GitHub, a user account is needed. Different accounts can be created on GitHub. While some account types are paid for, such as Team and Enterprise accounts, it is also possible to create a free user account. You can learn about all the different account types offered by GitHub, as well as the features offered with each account, by accessing https://docs.github.com/en/free-pro-team@latest/github/getting-started-with-github/types-of-github-accounts.

While the GitHub Free account type will be used throughout this book, learning about the features offered in other account types may help you choose an account that is appropriate for the scope of your project.

In the next few sections, you will learn how to create a free user account, as well as set up authentication options such as personal access tokens (PATs) and a Secure Shell (SSH) key.

Creating a free user account on GitHub

If you already have a GitHub account, you will not need to follow the steps in this section.

When you create a personal user account on GitHub, you have access to features such as unlimited public and private repositories and 2,000 actions minutes per month. After you create a user account, you can use a few different authentication methods to retrieve data related to your account and its resources, outlined as follows:

  • On the web browser, you can provide your username and password.
  • On the application programming interface (API), you can use a PAT.
  • On the command line, you can use an SSH key.

The next steps will show you how to create a GitHub Free user account. You will also learn how to generate a PAT and an SSH key that will be used in upcoming sections and chapters.

To sign up for a GitHub Free personal user account, follow these steps:

  1. Navigate to https://github.com/join.
  2. Fill in the blank fields with your information. Note that you will need to choose a unique username. An existing email address will also be needed, as well as a password.
  3. Solve the verification puzzle and click Verify. Then, click on Create account.
  4. Follow the prompts on the next page and click Complete setup. An email will be sent to the email address you provided in Step 2.
  5. The next screen will ask you to verify your email address. Although the account creation does not depend on having your email verified, you will not be able to use certain features—such as GitHub Actions—without a verified email address. For this reason, verifying your email address is required for the scope of this book.
  6. Open your email account. You should see an email that looks like this:
    Figure 1.1 – An example of the email sent by GitHub requesting you to verify your email address

    Figure 1.1 – An example of the email sent by GitHub requesting you to verify your email address

  7. Click on Verify email address.

Great job! Your personal user account has been created on GitHub, and you should be able to find it by navigating to https://github.com/your-username, where your-username is the unique username that you chose in Step 2.

Creating a PAT

Many popular GitHub Actions workflows use a PAT or an SSH key. This section will walk you through the creation of a PAT, in preparation for future chapters where one will be needed.

A PAT is a string of characters that can be used in place of a password against the GitHub API and on the command line. Different scopes can be attributed to a PAT to specify exactly what level of access is needed. Scopes are often chosen to limit access: nothing beyond the selected scopes can be accessed.

The next steps will guide you in creating a PAT with the repo, user, and workflow scopes. Understanding all the available scopes on GitHub is not part of what will be covered in this book. However, it is helpful to learn what kinds of access each scope grants. To learn more, see this documentation: https://docs.github.com/en/free-pro-team@latest/developers/apps/scopes-for-oauth-apps#available-scopes.

To create a PAT, follow these steps:

  1. Navigate to https://github.com/settings/tokens and click on Generate new token, as illustrated in the following screenshot:
    Figure 1.2 – Generating a new PAT

    Figure 1.2 – Generating a new PAT

  2. On the next screen, type a note that will help you identify what the PAT will be used for. Next, select the repo, workflow, and user scopes. Then, click on Generate token. The PAT scopes are shown in the following screenshot:
    Figure 1.3 – PAT scopes

    Figure 1.3 – PAT scopes

  3. On the next screen, make sure to copy your PAT and store it somewhere so that you can access it later.

Great work so far! You will use your newly created PAT in future sections of this book.

About SSH keys

An SSH key is an identification method that you can use to authenticate against a server. SSH keys are used as an access credential in the SSH protocol, which is a secure method for remote login from one server to another. SSH keys are often used in shell scripting, which is commonly used in GitHub Actions. Another common use of SSH keys is as an access tool while using the SSH protocol to clone a repository hosted on GitHub down to your local computer. Future sections will provide more details about cloning a repository hosted on GitHub.

Checking for existing SSH keys

As best practice and in case you are not sure whether you already have an SSH key, you should check for existing keys, as follows:

  1. Open Terminal (if you're using Linux or macOS) or Git Bash (if you're using Windows).
  2. Enter the following command, which will list all files in the ./~ssh directory (if they exist) and press Enter on your keyboard:
    $ ls -al ~/.ssh
  3. Look for an output that might resemble this:
total 64
drwx------   2 user group  4096 Dec 26  2018 .
drwx--x--x 127 user group 16384 Mar 14 04:41 ..
-rw-------   1 user group  1675 Sep 15  2008 id_rsa
-rw-r--r--   1 user group   394 Mar  7  2010 id_rsa.pub 

If no files are listed, stop here. You will need to generate a new SSH key. Follow the instructions in the Creating an SSH key section.

If you see a list of files like the ones in Step 3, stop here. You will not need to generate a new SSH key, but you may need to add your existing key to the SSH agent. Follow the instructions in the Adding the SSH key to the SSH agent section.

Creating an SSH key

The next steps will help you generate an SSH key on your local device and add it to your GitHub user account. These steps are important to grant your device remote access to the GitHub servers to execute operations such as cloning a repository hosted on GitHub down to your local device.

The steps to create an SSH key are different depending on your device's operating system. The following steps will provide instructions on how to create an SSH key on Windows, Linux, and macOS:

  1. Open Terminal (if you are using Linux or macOS) or Git Bash (if you're using Windows).
  2. Enter the following command, replacing [email protected] with the email address you used to create your GitHub user account:
    $ ssh-keygen -t ed25519 -C "[email protected]"
  3. Press Enter on your keyboard. This command creates a new SSH key, and you will see the following output on your screen:
    Generating public/private ed25519 key pair.
  4. Follow the prompts on the screen. When asked to enter a file in which to save the key, press Enter on your keyboard to accept the default location or type the directory where you would like the key to be saved to.
  5. Although a passphrase is optional, it is recommended to enter it. Without a passphrase, anyone who gains access to your computer can also gain access to other systems that use your SSH key. When prompted, type a passphrase and press Enter on your keyboard. The output will look like this:
Enter file in which to save the key (default-file-path):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in (default-location-or-user-entered-location).
Your public key has been saved in (filesystem-location).
The key fingerprint is:
SHA256:[redacted] [email protected]

Excellent! Your SSH key has been generated. Next, you will need to add this newly generated key to the SSH-agent.

Adding the SSH key to the ssh-agent

Although this step is not mandatory, adding the SSH key to the SSH agent is a best practice that will help keep your SSH key safe.

The SSH-agent is an SSH key manager. It helps keep your SSH key safe because it protects your SSH key from being exported. The SSH agent also saves you from having to type the passphrase you created in Step 5 of the previous section every time your SSH key is used.

Proceed as follows:

  1. Start the SSH-agent in the background, by entering the following command in Terminal or Git Bash and hitting Enter on your keyboard:
    $ eval "$(ssh-agent -s)"
  2. If you are using Windows or Linux, proceed to Step 3. If you are using macOS Sierra 10.12.2 or later, you will need to edit your ~/.ssh/config file to include the UseKeychain option.

    First, verify that the ~/.ssh/config file exists. In Terminal, type the following command, then press Enter on your keyboard:

    $ open ~/.ssh/config 
  3. If the file exists, proceed to the next step. If the file does not exist, use the following command to create it:
    $ touch ~/.ssh/config
  4. Make sure the content of your ~/.ssh/config file looks like that shown in the following code snippet. You may have other lines in your file with different options, and that is OK. For this step, ensure that the AddKeysToAgent line is added to your file:
    Host *
    AddKeysToAgent yes
    Add your SSK key to the SSH-agent:
  5. If you are using macOS, skip this step and proceed to Step 5. If you are using Linux or Windows, open your Terminal or Git Bash, type the following command, and press Enter on your keyboard:
    $ ssh-add ~/.ssh/id_ed25519
  6. If you are using macOS and you are using a passphrase to protect your SSH key, you will need to pass the -K option with the ssh-add command, as shown in the following code snippet. If you are not using a passphrase, you do not need to pass the -K option.
  7. Open your Terminal, type the following command, and press Enter on your keyboard:
$ ssh-add -K ~/.ssh/id_ed25519

Well done! Your SSH key has been added to the SSH agent. Next, you will need to add your newly generated SSH key to your GitHub account on GitHub.

Adding your SSH key to your GitHub user account

When you add your SSH key to your GitHub user account, you have another secure authentication alternative to interact with GitHub features. For example, when you use the SSH protocol and SSH key to clone a GitHub-hosted repository, you will not need to provide your username and PAT. Although you can use other means to clone a repository, such as HyperText Transfer Protocol (HTTP), SSH is more secure and convenient.

To add your SSH key to your GitHub user account, follow these next steps:

  1. Copy your SSH key to the clipboard. The following command will work for both Terminal and Git Bash:
    $ cat ~/.ssh/id_ed25519.pub 
  2. Navigate to your GitHub user account at https://github.com/settings.

    On the left-hand side menu, click on SSH and GPG keys, as illustrated in the following screenshot:

    Figure 1.4 – The SSH and GPG keys menu option on your GitHub account settings page

    Figure 1.4 – The SSH and GPG keys menu option on your GitHub account settings page

  3. On the next screen, click on New SSH key, as illustrated in the following screenshot:
    Figure 1.5 – The SSH keys section of your GitHub account settings

    Figure 1.5 – The SSH keys section of your GitHub account settings

  4. Next, add a Title that will help you identify your SSH key. Then, paste your SSH key that you copied in Step 1 into the Key textbox, as illustrated in the following screenshot:
    Figure 1.6 – Adding a new SSH key

    Figure 1.6 – Adding a new SSH key

  5. Lastly, click on Add SSH key. Once you click the button, you may be prompted to enter your GitHub user account password.

Excellent work! You have configured the basic functionalities of your GitHub user account. Although it is out of the scope of this book, you can customize other features of your GitHub account by navigating to https://github.com/settings.

In the next section, you will learn the basic Git commands and read more about a few more GitHub features that will help build the foundation needed to create GitHub Actions workflows.