This repository provides a hands-on introduction to GitHub Actions, featuring a workflow file walkthrough, as well as labs to help you create, set up, and troubleshoot GitHub Actions.
In this project, you will learn how to use GitHub Actions to automate workflows. The following components are included:
-
GitHub Actions Workflow File Walkthrough
An explanation of the basic components and structure of a GitHub Actions workflow -
GitHub Actions Lab - Creating Repository and Setting up Action
Step-by-step guide for creating a GitHub repository and setting up a basic GitHub Action. -
GitHub Actions Lab - Running and Troubleshooting Workflows
A guide to running workflows and troubleshooting common issues.
- Create GitHub repository.
- Click add create new file
- Create directory structure: your_repo/.github/workflows/superlinter.yml
- Make sure the directory structure follows the format above!
- Copy and paste the YAML code below
- Commit file to main
- Quickly navigate back to your repository and click the Actions tab at the top
- Here you can see your workflow running! Click it to see what it’s doing (it should be setting up the job or linting).
The repository includes a GitHub Actions workflow file to lint the codebase using the Super Linter. Below is the content of the .yml
workflow file:
name: Super-Linter
on: push
jobs:
super-lint:
name: Lint code base
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run Super-Linter
uses: github/super-linter@v4
env:
DEFAULT_BRANCH: main
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
The workflow file consists of several components that define the steps for GitHub Actions to follow when triggered. Here's an in-depth explanation of each part of the .yml
file:
name: Super-Linter
- This specifies the name of the workflow. In this case, the workflow is named "Super-Linter".
on: push
- This defines the event that triggers the workflow. Here, the workflow is triggered when a push event occurs in the repository.
jobs:
super-lint:
name: Lint code base
- The jobs section defines a collection of tasks to be run. In this case, there is one job named super-lint.
name: Lint code base
provides a descriptive name for the job.
runs-on: ubuntu-latest
- This specifies the environment in which the job will run. In this case, it runs on the latest version of Ubuntu.
The steps section contains the sequence of tasks to be executed within the job. These steps are carried out in the order they are listed.
- name: Checkout code
uses: actions/checkout@v2
- This step uses the
actions/checkout@v2
action to check out the repository’s code onto the runner, allowing subsequent steps to access and operate on it.
- name: Run Super-Linter
uses: github/super-linter@v4
env:
DEFAULT_BRANCH: main
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- This step runs the
github/super-linter
action (v4) to lint the codebase. - It sets two environment variables:
DEFAULT_BRANCH
: Specifies the default branch (in this case, main).GITHUB_TOKEN
: A secure token that allows the workflow to interact with the GitHub API. It’s automatically populated by GitHub secrets.
Now we can test if the linter is working. Add a new python file (main.py
) to main and copy-paste the following code:
The following is the draft version of the Python script main.py
:
def hello():
print("hi")
def bye():
print("bye")
print(hello())
hello()
: Prints "hi" to the console.bye()
: Prints "bye" to the console.print(hello())
: Calls thehello()
function and prints its return value, which isNone
sincehello()
does not return anything.
Commit the Python file to main and then quickly navigate back to the Actions tab. Click the latest workflow run at the top of the list, then click the lint codebase job and watch the automation in action!
The linter should fail. To check the errors, scroll down to the Run Super-Linter section.
Fix the errors and commit your Python file again. The lint codebase job should succeed.
def hello():
print("hi")
def bye():
print("bye")
print(hello())
Congratulations! You implemented continuous integration using GitHub Actions!
From here you could deploy a continuous deployment workflow that automatically deploys your code to a server, Docker image, or something else. Thanks for participating!
If you encounter any issues while running the workflows, make sure to check the Actions tab in your GitHub repository. Common issues may include:
- Incorrect branch name in the
DEFAULT_BRANCH
environment variable. - Issues with GitHub token permissions.
For troubleshooting, you can also check the logs for specific error messages.
If you would like to contribute to this project, feel free to fork the repository and submit a pull request. We welcome any contributions that improve the functionality or documentation of the project.