GitHub Actions Introduction and Setup

GitBeginner
Practice Now

Introduction

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 build and test every pull request to your repository, or deploy merged pull requests to production.

In this lab, you will learn the initial setup required to start using GitHub Actions. You will go through the process of creating a GitHub repository, cloning it to your local development environment, and setting up the specific directory structure that GitHub Actions requires to recognize your workflows.

Prerequisites

  • A GitHub account (https://github.com). If you do not have a GitHub account, you must first register for a free account at https://github.com/signup. This is a prerequisite for completing this lab.
  • Basic familiarity with the command line.

Create a new repository on GitHub website

In this step, you will create a new repository on the GitHub website. This repository will serve as the remote storage for your project and the place where GitHub Actions workflows will run.

  1. Open your web browser and navigate to https://github.com.
  2. Log in to your GitHub account.
  3. In the top-right corner of any page, use the + drop-down menu, and select New repository.
  4. In the Repository name box, type github-actions-demo.
  5. Select Public (GitHub Actions is free for public repositories).
  6. Check the box Add a README file. This ensures the repository is initialized and ready to clone.
  7. Click Create repository.

Create a new repository on GitHub website

You have now created a remote repository. In the next steps, you will bring this repository to your local environment.

Clone the repository to Ubuntu VM using git clone command

In this step, you will clone the repository you just created to your local LabEx environment. This allows you to edit files and create workflows locally.

  1. On your GitHub repository page, click the green Code button.
  2. Ensure the HTTPS tab is selected and copy the URL. It should look like https://github.com/your-username/github-actions-demo.git.
  3. Open the terminal in the LabEx environment. The default path is ~/project.
  4. Use the git clone command to download the repository. Replace your-username with your actual GitHub username.
cd ~/project
git clone https://github.com/your-username/github-actions-demo.git

Example Output:

Cloning into 'github-actions-demo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
  1. Verify that the directory has been created:
ls -F

You should see github-actions-demo/ listed.

Create a .github/workflows directory in the repository

GitHub Actions looks for workflow configuration files in a specific directory within your repository: .github/workflows. In this step, you will create this directory structure.

  1. Ensure you are inside the repository directory:
cd ~/project/github-actions-demo
  1. Create the .github directory and the workflows subdirectory inside it using the mkdir command with the -p (parents) flag:
mkdir -p .github/workflows
  1. Git does not track empty directories. To ensure we can commit this structure in the next step, let's create a simple empty workflow file named main.yml inside this directory.
touch .github/workflows/main.yml
  1. Verify the structure:
ls -R .github

Example Output:

.github:
workflows

.github/workflows:
main.yml

This structure is essential. GitHub will only recognize YAML files placed in .github/workflows as Action workflows.

Commit and push the directory changes using git add, git commit, and git push

In this step, you will commit the new directory structure and push it to GitHub.

  1. Stage the new files for commit:
git add .
  1. Commit the changes with a descriptive message:
git commit -m "Setup GitHub Actions workflow directory"

Example Output:

[main ... ] Setup GitHub Actions workflow directory
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 .github/workflows/main.yml
  1. Push the changes to the remote repository on GitHub:
git push

Note on Authentication: When you run git push, the WebIDE will automatically prompt you to authenticate. Follow these detailed steps:

  1. A popup will appear with the message: "The extension 'GitHub' wants to sign in using GitHub." Click Allow.
  2. A new notification will appear. Click "Copy&Continue to GitHub", then click "Open" in the next prompt.
  3. Log in to your GitHub account in the browser window that opens, and enter the authorization code that was copied. After confirming the authorization, the page will automatically close.
  4. Wait a few seconds, and you will see the terminal successfully complete the push operation.

GitHub Authentication

Privacy Note: The WebIDE will request full access to your GitHub account for authentication purposes. You don't need to worry about privacy concerns - the LabEx VM will be immediately destroyed after you complete the current lab, and your credentials and authorization information will not be retained.

This authentication process does not require manual configuration of username or Personal Access Token.

After following the authentication steps, you will see output similar to the following:

Example Output:

Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), 388 bytes | 388.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/your-username/github-actions-demo.git
   2e0917f..a191a97  master -> master
  1. Visit your repository on GitHub in a web browser to confirm that the .github/workflows directory and main.yml file are now visible in the repository structure.

Summary

In this lab, you have successfully set up the foundation for using GitHub Actions. You have:

  1. Created a new repository on GitHub.
  2. Cloned the repository to your local environment.
  3. Created the mandatory .github/workflows directory structure.
  4. Committed and pushed these changes back to GitHub.

With this structure in place, you are now ready to define CI/CD workflows by editing the YAML files within the workflows directory.