This tutorial is designed to get you up and running with GitHub Actions to automate your project's software lifecycle. GitHub Actions versatility extend much further, but this tutorial focuses specifically on automating the build and test phases of an application.
- Forking the repository
- Executing the default workflow
- Customizing output and test validation
- Modifying the workflow to extract and archive artifacts
- Workflow gists
First, you'll need to fork the repository - giving you a personal copy.
- Fork the repository
- Navigate to the repository:
https://github.com/admeeer/cicd-tutorial
- In the top right, click the dropdown next to
Fork
and clickCreate a new fork
- Click
Create fork
- Navigate to the repository:
These steps show you how to run the default workflow and help get you used to interacting with GitHub Actions. This tutorial assumes you have forked the repository.
- Run the workflow
- Navigate to the
Actions
tab - On the left, click on the
manual-build-and-test-automation
workflow - On the right, click
Run workflow
and thenRun workflow
- Navigate to the
- Check the workflow success
- Click the topmost workflow
- Success (hopefully!)
These steps show you how to clone the repository onto your local machine, personalize the output and test validation, and then push your changes up to your GitHub fork. After, we'll view the workflow executing your new changes.
The ocky way. This tutorial assumes you have forked the repository and installed a Python interpreter and Git. See https://git-scm.com/ and https://www.python.org/downloads/ for further information.
- Clone the repository
- Run
git clone https://github.com/admeeer/cicd-tutorial.git
- Run
- Navigate to the cloned directory
- Run
cd cicd-tutorial
- Run
- Customize
- Open
script.py
and edit the input in theprint
statement - Navigate to the
tests/
folder, runcd tests/
- Open
test_script.py
and edit the assert to your input
- Open
- Optionally, test locally
- Update pip, run
python3 -m pip install --upgrade pip
- Then, run
pip3 install pytest
- If in
tests/
, runpytest test_script.py
, else, in the directory, runpytest tests/
- Update pip, run
- Push your local changes up to GitHub
- Run
git add *
, note: if you modified any other files, this command will capture those changes too - Run
git commit -m "Customized output and modified test to assert towards new output"
- Run
git push
- Run
- Check the workflows success
- Navigate to the
Actions
tab - On the left, click on the
build-and-test-automation
workflow - Click the topmost workflow
- Success (hopefully!)
- Navigate to the
These steps run you through modifying the tests and default workflow to capture pytest output to a file and then upload that output as artifacts to GitHub. This tutorial assumes you have forked the repository.
- Open the workflow
- Navigate to repository and open the default workflow file,
.github/build_and_test.yml
.
- Navigate to repository and open the default workflow file,
- Direct pytests output to a file
- Under the
test
step, changepytest -v tests/
topytest -v tests/ | tee tests/test_output.txt
. This will tell pytest to direct its output to both the console AND an output file in tests/test_output.txt.
- Under the
- Add a new step
- Investigate the other steps and then create one under the last step,
test
. Add a keywordname:
and name the new stepupload test artifacts
. This step should be completely under thetest
step and its instructions. It should be similar to this:
- Investigate the other steps and then create one under the last step,
- Under the
name
of the step, add another keyword calleduses:
. Here, we'll use a predefined GitHub Action that uploads artifacts based on a few parameters that we'll set. Addactions/upload-artifact@v2
to the right of the keyword. Your new step should be similar to this:
- Under the
uses
of the step, add another keyword calledwith
. This keyword defines the parameters of our artifacts. Underwith
, add two indented keywords,name
andpath
. Set thename
to something liketest-output
, this will be the name of the artifacts generated. Setpath
totests/test_output.txt
. Your step is now completed and with luck, looks something like this:
- Commit your changes
- In the top right of the web browser, click
Commit changes
. Add a descriptive commit message, likeAdded artifact extraction and capture to workflow
and then clickCommit changes
.
- In the top right of the web browser, click
- Check the workflows success
- Navigate to the
Actions
tab - On the left, click on the
build-and-test-automation
workflow - Click the topmost workflow
- Wait for the workflow to finish.
- Scroll down and wait for the artifacts to upload.
- Success! (hopefully!)
- Navigate to the
- Did the workflow not succeed or are you stuck? See Workflow gists to review the workflow.
Stuck? These gists are what your workflows should look like.
- The default workflow https://gist.github.com/admeeer/4e449981a730fa016d0780a335b9248d
- The extended workflow with artifact extraction and archival https://gist.github.com/admeeer/383f82bbdfdc5fe5f5998e7657945c95