Lab: Python Upper App CI/CD
This lab will guide you through creating a CI/CD pipeline for the Python Upper App using GitHub Actions. The pipeline tests the app and packages it into platform-specific executables for Windows, macOS, and Linux.
Lab Objectives
- Set up a GitHub Actions workflow for the Python Upper App.
- Test the app across multiple Python versions and operating systems.
- Package the app into standalone executables.
- Upload the executables as artifacts.
Prerequisites
Before starting this lab, ensure that you have read the Python Upper App Overview to have your Python Upper App ready for this lab.
Instructions
Step 1: Initialize the Workflow File
- Navigate to your repository on GitHub.
- Go to Actions > New workflow.
- Select Set up a workflow yourself.
- Name the file
.github/workflows/python-upper-app-ci-cd.yml
. - Add the following base structure to the file:
name: Python Upper App CI/CD
on:
workflow_dispatch:
push:
paths:
- '.github/workflows/python-upper-app-ci-cd.yml'
- 'src/python/upper_project/**'
Step 2: Define the Job Strategy
- Add a job named
test-and-package
. - Configure the matrix strategy to run the workflow on:
- Operating Systems:
windows-latest
,ubuntu-latest
,macos-latest
. - Python Versions:
3.10
,3.11
.
- Operating Systems:
jobs:
test-and-package:
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
python-version: ['3.10', '3.11']
runs-on: $
steps:
- name: Checkout Code
uses: actions/checkout@v4
Step 3: Set Up Python
- Use the
actions/setup-python@v4
action to set up Python. - Specify the
python-version
from the matrix.
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: $
- Add a step to print the Python version for debugging:
- name: Print Python Version
run: python --version
Step 4: Run Unit Tests
- Upgrade
pip
to the latest version:
- name: Upgrade pip
run: python -m pip install --upgrade pip
- Add a step to run the unit tests:
- name: Run Unit Tests
run: python -m unittest discover tests
working-directory: src/python/upper_project
Step 5: Install PyInstaller
- Add a step to install
pyinstaller
, which is required to package the application:
- name: Install pyinstaller
run: pip install pyinstaller
Step 6: Package the Application
- Use
pyinstaller
to create a standalone executable:
- name: Package Executable
run: |
pyinstaller --onefile upper/upper.py
working-directory: src/python/upper_project
- Add a step to list the packaged files for verification:
- name: List Packaged Files
run: ls -R ./src/python/upper_project/dist
Step 7: Upload Executables as Artifacts
- Use the
actions/upload-artifact@v3
action to save the executables.
- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: upper-executable-$-$
path: src/python/upper_project/dist/*
Step 8: Test the Packaged Executable
- Add a step to test the executable:
- Use a conditional statement to handle different OS-specific executable formats.
- name: Test Executable
run: |
if [[ "$RUNNER_OS" == "Windows" ]]; then
./dist/upper.exe Hello World
else
./dist/upper Hello World
fi
working-directory: src/python/upper_project
shell: bash
Final Workflow File
After completing the steps above, your workflow file should look like this:
name: Python Upper App CI/CD
on:
workflow_dispatch:
push:
paths:
- '.github/workflows/python-upper-app-ci-cd.yml'
- 'src/python/upper_project/**'
jobs:
test-and-package:
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
python-version: ['3.10', '3.11']
runs-on: $
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: $
- name: Print Python Version
run: python --version
- name: Upgrade pip
run: python -m pip install --upgrade pip
- name: Run Unit Tests
run: python -m unittest discover tests
working-directory: src/python/upper_project
- name: Install pyinstaller
run: pip install pyinstaller
- name: Package Executable
run: |
pyinstaller --onefile upper/upper.py
working-directory: src/python/upper_project
- name: List Packaged Files
run: ls -R ./src/python/upper_project/dist
- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: upper-executable-$-$
path: src/python/upper_project/dist/*
- name: Test Executable
run: |
if [[ "$RUNNER_OS" == "Windows" ]]; then
./dist/upper.exe Hello World
else
./dist/upper Hello World
fi
working-directory: src/python/upper_project
shell: bash
Validation
- Push your changes to GitHub.
- Trigger the workflow manually or through a new commit.
- Verify:
- All tests pass.
- Artifacts for each OS and Python version are uploaded.
- Executables run successfully.
Summary
In this lab, you learned how to create a CI/CD workflow for the Python Upper App using GitHub Actions. The workflow tests the app, packages it into standalone executables, and uploads the artifacts for distribution. You can further enhance the workflow by adding deployment steps or integrating with external services.