This tutorial aims to provide a hands-on introduction to GitHub Actions, a powerful tool for automating software workflows.
By the end of this tutorial, you will be able to:
- Understand what GitHub Actions is and its benefits.
- Create your first workflow using GitHub Actions.
- Understand basic concepts like events, jobs, steps, and actions.
GitHub Actions allows you to automate your software development workflows in the same place you store code and collaborate on pull requests and issues. You can write individual tasks, called actions, and combine them to create a custom workflow. Workflows are custom automated processes that you can set up in your repository to build, test, package, release, or deploy any code project on GitHub.
Actions
tab.Set up this workflow
.This is a simple example of a workflow that runs whenever a push or pull request event occurs to the master branch.
name: CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run a one-line script
run: echo Hello, world!
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
name: CI
- This is the name of your workflow, it can be any string.on:
- This is the event that triggers the workflow. In this case, it's either a push or pull request to the master branch.jobs:
- This is where you specify the jobs to be run.build:
- This is the identifier you're assigning to the job.runs-on: ubuntu-latest
- This specifies the type of runner that the job will run on.steps:
- Steps are a sequence of tasks that will be executed as part of the job.uses: actions/checkout@v2
- This is an action that checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.run:
- This is where you'll define the commands to be run.In this tutorial, you've learned the basics of GitHub Actions, including how to create your first workflow. You've also been introduced to concepts like events, jobs, steps, and actions.
Next steps could include learning more about the different events that can trigger workflows, exploring the marketplace of existing actions, or creating your own actions.
Additional Resources:
- GitHub Actions Documentation
- GitHub Learning Lab
Solutions and Explanations
Refer to the code example in this tutorial. The single line script echo Hello, world!
prints "Hello, World!" to the console.
You can add a new job to the workflow like this:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run a one-line script
run: echo Hello, world!
pull_request:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run a one-line script
run: echo New Pull Request
In this case, the pull_request
job includes an if
condition to ensure it only runs on pull request events.
Keep practicing by creating workflows for different events and using different actions available in the marketplace.