GitHub Actions
Estimated time to read: 3 minutes
GitHub Actions is a powerful CI/CD (Continuous Integration/Continuous Deployment) tool that allows you to automate workflows directly within your GitHub repository. You can use GitHub Actions to build, test, and deploy your code, as well as automate tasks like labeling issues, managing releases, and more.
To get started with GitHub Actions, follow these steps:
-
Create a new GitHub Actions workflow: In your GitHub repository, create a new directory named
.github
, and inside that directory, create another directory namedworkflows
. In theworkflows
directory, create a new file with a.yml
or.yaml
extension, such asmain.yml
. This file will define your GitHub Actions workflow. -
Define the workflow: Edit the
main.yml
file to define your workflow using YAML syntax. A basic workflow definition consists of the following elements: -
name
: The name of your workflow. on
: The events that trigger the workflow (e.g., push, pull_request, etc.).jobs
: A collection of jobs that will be executed as part of the workflow. Each job contains a set of steps to be executed.
Here's an example of a simple GitHub Actions workflow that runs whenever code is pushed to the main branch:
name: My First Workflow
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.10
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: pytest
In this example, the workflow is triggered when code is pushed to the main branch. It runs on the latest Ubuntu environment and consists of four steps: checking out the repository, setting up Python, installing dependencies, and running tests.
-
Commit and push the workflow file: Commit the
.github/workflows/main.yml
file to your repository and push it to GitHub. This will automatically enable the workflow, and it will run whenever the specified trigger events occur. -
Monitor the workflow: To monitor the progress of your GitHub Actions workflow, go to the "Actions" tab in your repository. Here, you can see the status of your workflow runs, view logs, and troubleshoot issues if necessary.
-
Customize and expand your workflow: As you become more familiar with GitHub Actions, you can customise and expand your workflows using various actions from the GitHub Actions Marketplace (https://github.com/marketplace/actions) or by creating your own custom actions.
By following these steps, you can start using GitHub Actions to automate your workflows, improve your development processes, and streamline your CI/CD pipeline directly within your GitHub repository.