We are going to build a Continuous Integration (CI) Pipeline using GitHub Actions. By the end of this lab, we will have a "Robot" that automatically:
- Lints our code (checks for style errors) every time we push.
- Tests our code every time we push.
- Badges our repository to show off our build status.
To start, it is highly recommended that you create a new repository on your local machine, and an empty repository with the same name in your own GitHub account. Many features are only possible when you have your own account.
- GitHub Actions needs a set of instructions to know what software to install. We provide this via a requirements.txt file. Ensure you have a file named
requirements.txtin the project root. The file should contain at least the following necessary tools:flask pytest flake8 - Copy the pet shop app from this directory to your project root:
|- templates/ |- index.html |- app.py - Commit these initial files to your local repository:
git add . git commit -m "Initial commit" - If you have not created a GitHub repository, create an empty one at GitHub. Afterwards, push the local repository into GitHub:
git remote add origin YOUR_REPOSITORY_LINK git branch -M main git push origin main
GitHub looks for special instructions in a folder named .github.
-
Inside your project folder, create these nested folders:
|- .github/ |- workflows/ -
Inside
.github/workflows/, create a file namedci.yaml. -
For now, create a "Hello World" version of a YAML file to test that the workflow runs properly. Copy the following code into
ci.yaml.name: CI Pipeline on: [push] jobs: build: runs-on: ubuntu-latest steps: - name: Say Hello run: echo "The pipeline is working!"
-
Commit and push the file.
-
Directly after pushing, go to the
Actionstab at GitHub and observe the workflow running.
We are going to implement static analysis using a linter called flake8 to catch bad code styles before we even check if it works.
- Modify
ci.yaml: Update the steps section to check out the code and set up Python.steps: - name: Checkout Code uses: actions/checkout@v3 # Downloads the code to the runner - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.11' - name: Install Dependencies run: | python -m pip install --upgrade pip if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- Add a step to run the linter in the steps part of the
ci.yamlfile.- name: Lint with Flake8 run: flake8 . --count --select=E1,F4 --show-source --statistics
- Commit and push the code to GitHub.
- Go to the
Actionstab in GitHub, and observe the results. - Fix the styling errors found by the linter, push the new code back to GitHub, and repeat until there are no styling errors.
- Copy the unit tests from this directory to your project root.
|- tests/ |- test_app.py |- pytest.ini - Add a step to run pytest in the steps part of the
ci.yamlfile.- name: Run Unit Tests run: pytest
- Commit and push the code to GitHub.
- Go to the
Actionstab in GitHub, and observe the results. - Fix the failing unit tests, push the new code back to GitHub, and repeat until all tests pass.
- The workflow definition lives at
.github/workflows/ci.yamland runs on every push or pull request tomain. - Dependencies are cached between runs,
flake8linting is scoped to this module, andpytestexecutes from14_devops_github_actionswith coverage reporting enabled. - Coverage XML is uploaded as a build artifact so you can download it from the Actions page without re-running the suite locally.