In this tutorial, we will explore how to create and manage workflows using GitHub Actions. GitHub Actions is a powerful automation tool that allows you to automate your software development workflow. You can set up continuous integration (CI), continuous deployment (CD), automated testing, and more directly within your GitHub repository.
Before diving into creating workflows, ensure you have the following:
A workflow is a set of instructions for automating tasks. In GitHub Actions, workflows are defined in YAML files located in the .github/workflows directory of your repository. Each workflow file can contain one or more jobs, and each job consists of multiple steps.
Let's create a simple workflow that runs tests whenever code is pushed to the main branch.
Create the Workflow File
Navigate to your repository on GitHub and go to the .github/workflows directory. If this directory doesn't exist, you can create it. Then, create a new file named ci.yml.
Define the Workflow
Open the ci.yml file and add the following content:
name: CI
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- 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
Explanation:
name: The name of the workflow.on: Specifies the event that triggers the workflow. In this case, it runs on pushes to the main branch.jobs: Contains one or more jobs. Here, we have a single job named build.runs-on: Specifies the type of runner machine to use for the job. ubuntu-latest is a common choice for Node.js projects.steps: A sequence of tasks that are executed in order.Commit and Push
Commit the ci.yml file to your repository:
git add .github/workflows/ci.yml
git commit -m "Add CI workflow"
git push origin main
View Workflow Runs
After pushing, navigate to the "Actions" tab in your GitHub repository. You should see the workflow running and you can monitor its progress.
You can view the history of all workflow runs by navigating to the "Actions" tab in your repository. Here, you can see a list of all workflows, their statuses, and details about each run.
To edit a workflow, simply navigate to the .github/workflows directory in your repository, open the desired workflow file, make changes, and commit them as usual.
If you need to temporarily disable a workflow, you can rename or delete the workflow file. This will prevent it from running until you restore it.
You can use conditions to control whether a step runs based on certain criteria. For example:
- name: Run tests only if Node.js version is 14
run: npm test
if: github.event_name == 'push' && github.ref == 'refs/heads/main' && runner.os == 'ubuntu-latest'
GitHub Actions allows you to store sensitive information as secrets, which can be used in your workflows. To use a secret:
Go to your repository settings.
Click on "Secrets" and then "Actions".
Add a new secret (e.g., API_KEY).
Use the secret in your workflow:
- name: Use API key
run: echo ${{ secrets.API_KEY }}
The matrix strategy allows you to run jobs across multiple configurations, such as different operating systems or Node.js versions:
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [12.x, 14.x]
steps:
- uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
GitHub Actions provides a robust framework for automating your software development processes. By creating and managing workflows, you can streamline your CI/CD pipeline, improve code quality, and enhance collaboration among team members. With the knowledge gained from this tutorial, you are now equipped to leverage GitHub Actions effectively in your projects.