In this exercise, you will create and run your first CI/CD pipeline on GitHub Actions. You will learn how to configure a simple pipeline and how to run it, and also explore some advanced features of GitHub Actions.
-
(Must Read) Understanding GitHub Actions: https://docs.github.com/en/actions/about-github-actions/understanding-github-actions
-
GitHub Actions syntax reference guide: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
-
Tutorial: Create a GitHub Actions workflow: https://docs.github.com/en/actions/quickstart
-
More Information about GitHub-hosted runners:
- Public Repositories: (Click Here)
- Private Repositories (Click Here)
-
How to use the checkout action in GitHub Actions: (Click Here)
Before starting this exercise, ensure you have:
-
A GitHub account
- If you do not have one, you can sign up at GitHub.com
-
Gitinstalled on your local machine- To test if it's installed, open your Command Line Terminal and run:
git --version - If not installed, download it from git-scm.com
- To test if it's installed, open your Command Line Terminal and run:
-
Pythoninstalled on your local machine (Python 3.7 or higher recommended)- To test if it's installed, open your Command Line Terminal and run:
or
python --versionpython3 --version - If not installed, you can download Python here:
- Windows OS: https://www.python.org/downloads/
- macOS: https://www.python.org/downloads/macos/
- To test if it's installed, open your Command Line Terminal and run:
Ensure all prerequisites are met before proceeding with the exercise.
Your task is to complete the .github/workflows/github-ci.yml file to create a working CI/CD pipeline with two jobs: build and test.
- Fork this repository on your GitHub account.
- Clone this forked repository on your GitHub account to local,
OR
You can edit the
.github/workflows/github-ci.ymlfile on the browser version of GitHub. - Open the
.github/workflows/github-ci.ymlfile in your preferred text editor. - Follow the TODO steps in the
github-ci.ymlfile to complete the configuration.
Follow these steps to complete the GitHub Actions workflow file:
- Uncomment the
name:line and replace<WORKFLOW NAME>with a descriptive name for your workflow (e.g., Python CI/CD Pipeline).
- Uncomment the
on:section and thepush:andpull_request:lines to trigger the workflow on push and pull request events.
- Uncomment the
<ENTER JOB NAME 1>:line and replace it with a suitable job name (e.g., build). - For TODO-STEP 3a, uncomment the
runs-on:line. - For TODO-STEP 3b, enter a name for your step to display on GitHub (e.g., Install dependencies).
- For TODO-STEP 3c, add commands to install dependencies, replacing
<FILE NAME>withrequirements.
- Uncomment the
<ENTER JOB NAME 2>:line and replace it with a suitable job name (e.g., test). - For TODO-STEP 4a, uncomment the
needs:line and replace<ENTER JOB NAME 1>with the name you used for the first job. - For TODO-STEP 4b, uncomment the
Install dependenciesstep. - For TODO-STEP 4c, uncomment the
Run testsstep.
- Make sure to keep the
uses: actions/setup-python@v2anduses: actions/checkout@v2steps as is.- Remember to remove the
#symbol to uncomment lines when replacing content in the placeholders.- Make sure your indentation is correct. YAML is sensitive to indentation.
After completing the github-ci.yml file:
- Commit your changes in your local machine.
- Push the changes to your GitHub repository.
- Go to your project on GitHub and navigate to the
"Actions"tab to see your workflow running. - Debug any issues that arise and make necessary adjustments to your
github-ci.ymlfile. - Once your workflow run is successful, try making changes to the backend code and pushing the changes to see any changes in workflow execution or logs.
- Explore more options of the GitHub Actions configuration, including job logs and workflow visualizations.
Wohoo! You have successfully run your first CI/CD pipeline on GitHub Actions.
-
Conditional Job Execution: Implement a job that only runs when changes are made to specific files or directories:
- HINT: Use the
pathskeyword in theonsection to run a job only when changes are made to Python files ('**.py'). For more details (Click Here).
- HINT: Use the
-
Manual Workflow Dispatch: Create a workflow that can be triggered manually from the GitHub Actions UI.
- HINT: Use the
workflow_dispatchevent trigger. For more details (Click Here).
- HINT: Use the
-
Use Environment Variables: Create a job that uses a custom environment variable, and set its value using GitHub Secrets.
- HINT: Use
envto define environment variables and${{ secrets.SECRET_NAME }}to access GitHub Secrets.
- HINT: Use
SOLUTION: The answers and examples of above task can be found in the file
Answer/github-ci.yml.
-
Execute workflow only on specific branches: Set up a job that runs only on specific branches: i.e. Create a job that runs only on the
developmentbranch using thebrancheskeyword under theonsection.- HINT: Refer
onfor more details (Click Here)
- HINT: Refer
-
Create Job Dependencies: Create a job that depends on the successful completion of another job.
- HINT: Use the
needskeyword to specify job dependencies. For more details (Click Here)
- HINT: Use the
-
Matrix Strategy: Create a test job that runs on multiple Python versions using a matrix strategy.
- HINT: Use the
strategyandmatrixkeywords to define multiple Python versions. For more details (Click Here)
- HINT: Use the
-
Caching Dependencies: Implement caching for pip dependencies to speed up subsequent workflow runs.
- HINT: Use the
actions/cacheaction to cache pip dependencies. For more details:
- HINT: Use the
-
Create a Docker Image Job: Create a job in the workflow file which builds a Docker image of your Python application and pushes it to DockerHub.
-
Jobs with Artifacts: Create a job that generates a test coverage report and saves it as an artifact that can be downloaded after the workflow completes.
Congratulations! You have completed this exercise in GitHub Actions. You have learned how to create a basic workflow, run the workflow, and also about many other advanced features of the YAML configuration. Continue to explore and read further in GitHub's documentation to learn more about the best practices and advanced CI/CD configurations.
All the best, and happy learning!