Introduction
In this article, I will explain how to build a CI/CD Testing pipeline using GitHub actions the work on this project was developed together with Elias Shourosh.
The technological stack used to implement the solution is:
Programing language: Python
Testing Framework: pytest
Reporting Framework: Allure
What are GitHub Actions?
according to this article:
GitHub Actions is an API for cause and effect on GitHub: orchestrate any workflow, based on any event, while GitHub manages the execution, provides rich feedback, and secures every step along the way. With GitHub Actions, workflows and steps are just code in a repository, so you can create, share, reuse, and fork your software development practices.
GitHub actions is free and support public repositories. the article will be demonstrated using my public repository, which contains a suite of E2E tests written in python.
Implementing the Solution
We need to create a .github
folder into the root of our project, inside it create a workflow folder. Inside it, we create our workflows, which are YAML files.
This is the workflow content:
name: Python application
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Starting Selenoid Server (aerokube/selenoid) within actions
uses: ajinx/selenoid@2.0
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Install plugin
run: pip install pytest-github-actions-annotate-failures
- name: Test with pytest
run: |
pytest --alluredir=allure-results --browser remote
- name: Allure results
uses: simple-elf/allure-report-action@master
if: always()
id: allure-report
with:
allure_results: allure-results
allure_report: allure-report
gh_pages: gh-pages
allure_history: allure-history
- name: Deploy report to Github Pages
if: always()
uses: peaceiris/actions-gh-pages@v2
env:
PERSONAL_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PUBLISH_BRANCH: gh-pages
PUBLISH_DIR: allure-history
Walkthrough
We are running the workflow on every push or PR to the main branch.
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
It runs the build on an Ubuntu instance provided by GitHub, here is the specification for Github hosted runners. Of course, there is also an option of creating custom runners. this workflow is pretty simple so I used a hosted runner.
runs-on: ubuntu-latest
now a series of workflow steps are executed in order:
- Checking out the latest code with the following action:
- uses: actions/checkout@v2
- Deploying a disposable selenium grid using Selenoid is done to simplify the run of the Selenium driver (ChromeDriver) at the runner, the grid terminates when the run is over. this is done with the following action:
- name: Starting Selenoid Server (aerokube/selenoid) within actions
uses: ajinx/selenoid@2.0
- Installing python with the following action:
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9
- Installing the project dependencies using pip using a bash command:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
Installing a Pytest plugin that annotates test failures in GitHub actions by the following bash command:
- name: Install plugin
run: pip install pytest-github-actions-annotate-failures
- Running our test suite also setting the allure result directory, and that run should happen on the grid and not locally:
- name: Test with pytest
run: |
pytest --alluredir=allure-results --browser remote
- After the run is finished, we are generating the allure report with the following action:
- name: Allure results
uses: simple-elf/allure-report-action@master
if: always()
id: allure-report
with:
allure_results: allure-results
allure_report: allure-report
gh_pages: gh-pages
allure_history: allure-history
- We deploy the report to the project GitHub pages with the following action:
- name: Deploy report to Github Pages
if: always()
uses: peaceiris/actions-gh-pages@v2
env:
PERSONAL_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PUBLISH_BRANCH: gh-pages
PUBLISH_DIR: allure-history
The result of the run is displayed in the PR status as a check, we can determine if this check.
We can determine in the project settings if the check is mandatory for the push.
The result history of the run can be viewed in the action tab.
Allure reports can be found at the GitHub pages of the project:
In conclusion
In this article, we reviewed the creation of a CI/CD automation pipeline using GitHub actions python and pytest.
Further reading on GitHub actions features can be found in this link.
Happy testing!