GitHub Actions is a powerful automation tool that allows you to automate, customize, and execute your software development workflows right in your repository. With GitHub Actions, you can build, test, deploy, release, or trigger any job you need to keep your project moving forward.
In this tutorial, we will cover the basics of GitHub Actions, including how to create and configure workflows, understand events that trigger actions, and explore some common use cases.
GitHub Actions work by defining a series of steps in a workflow file. These files are written in YAML and are stored in your repository under the .github/workflows directory. A workflow is made up of one or more jobs, and each job consists of multiple steps.
A basic workflow YAML file might look like this:
name: CI
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run a one-line script
run: echo Hello, world!
Workflows can be triggered by various events such as:
push: Triggered when code is pushed to the repository.pull_request: Triggered when a pull request is opened or updated.schedule: Triggered at specified times using cron syntax.Let's walk through some practical examples to understand how GitHub Actions can be used in different scenarios.
Suppose you want to run tests every time code is pushed to the main branch. Here’s how you can set up a workflow for that:
name: Test Workflow
on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
If you want to automate the deployment of your application to a server, you can use GitHub Actions to handle that:
name: Deployment Workflow
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -t rsa your-server.com >> ~/.ssh/known_hosts
- name: Deploy application
run: |
scp -r . user@your-server.com:/path/to/deploy
You can automate the creation of releases when a new tag is pushed:
name: Release Workflow
on:
push:
tags:
- 'v*.*.*'
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: |
Changes in this Release
Now that you have a basic understanding of GitHub Actions, you can explore more advanced features and integrations. In the next section, we will cover "GitLab CI/CD Basics," where we will learn how to automate workflows using GitLab’s continuous integration and delivery tools.
By mastering GitHub Actions and other automation tools, you can streamline your development process, reduce manual errors, and improve collaboration within your team.