Getting Started with Creating CI/CD Pipelines on GitHub Actions
GitHub Actions allows you to automate the testing building and deployment of your repositories. The building and tests run on virtual machines in the cloud provided by GitHub.
Click here to fork my repo if you want to follow along.
Setting Our File Structure
A GitHub Actions workflow is defined in a .yaml
file. To setup your workflow you must first create the following folders within your project repository:
your-project-repo/
├── .github/
│ └── workflows/
│ └── main.yaml
└── ...
Anything in the workflow's directory will be picked up by GitHub and setup as a workflow in the cloud. Our YAML file is named main.yaml
but you can name it anything you like.
Let's Write Our Pipeline
Naming our workflow
Give the workflow a name that describes what it does in concise terms
name: Python Calculator Testing Intigration
Setting trigger events for the workflow
In our case, we want this workflow to run every time there is a push into the main branch of out repo.
name: Python Calculator Testing Intigration
on:
push:
branches: ["main"]
Events cover almost all GitHub activity including opening a pull request to the completion of another workflow. Triggers can even be sent from external sources using an API.
Specifying a runner
This is a virtual machine provided by GitHub or self-selfhosted on your own infrastructure to run our jobs within the workflow.
Below, we define a name for our job (test_on_push
in our case), and also specify our runner ubuntu-latest
. GitHub also provides windows and MacOS virtual machines.
name: Python Calculator Testing Intigration
on:
push:
branches: ["main"]
jobs:
test_on_push:
runs-on: ubuntu-latest
Defining the steps of our job
These are the specific instructions for what needs to happen to complete our job.
- First, we need to get our source code into the virtual machine, this is done using an officially maintained action called
checkout
which will bring your code to the current working directory. - Since we need to setup and use a python environment need another officially maintained action called
setup-python
. We also specify the python version we are using semantic version range syntax as”3.11.x”
. - Next, we install the dependencies needed for our code to run smoothly based, but first we must update pip using
python -m pip install — upgrade pip
as it’s a common practice to keeppip
up to date.
name: Python Calculator Continuous Intigration
on:
push:
branches: ["main"]
jobs:
test_on_push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setting up python environment
uses: actions/setup-python@v5
with:
python-version: "3.11.x"
- name: Installing Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Testing with pytest
run: pytest
Conclusion
In conclusion, GitHub Actions provides a robust, flexible platform for automating software workflows directly within GitHub. By setting up actions like the Python Calculator Testing Integration example, developers can automate their testing, building, and deployment processes, significantly enhancing productivity and ensuring consistent quality. Whether you’re working on a small project or managing large-scale deployments, GitHub Actions seamlessly integrates these tasks into your development cycle, making it an indispensable tool for modern development teams.