
Automating Workflows with GitHub Actions
By :

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.
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:
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:
Figure 1.1 – An example of the email sent by GitHub requesting you to verify your 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.
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:
Figure 1.2 – Generating a new PAT
repo
, workflow
, and user
scopes. Then, click on Generate token. The PAT scopes are shown in the following screenshot:Figure 1.3 – PAT scopes
Great work so far! You will use your newly created PAT in future sections of this book.
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.
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:
./~ssh directory
(if they exist) and press Enter on your keyboard:$ ls -al ~/.ssh
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.
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:
[email protected]
with the email address you used to create your GitHub user account:$ ssh-keygen -t ed25519 -C "[email protected]"
Generating public/private ed25519 key pair.
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.
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:
$ eval "$(ssh-agent -s)"
~/.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
$ touch ~/.ssh/config
~/.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:
$ ssh-add ~/.ssh/id_ed25519
-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.$ 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.
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:
$ cat ~/.ssh/id_ed25519.pub
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.5 – The SSH keys section of your GitHub account settings
Figure 1.6 – Adding a new SSH key
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.
Change the font size
Change margin width
Change background colour