4 2-Github Action
4 2-Github Action
Github action
1
20/10/2024
2
20/10/2024
Exercises
1. Create a .github folder in the root of your repository, and add a sub-
folder called workflows.
2. Review the GitHub Actions guide for Python (focusing on automated
testing for Python code)
3. Place the provided workflow file called tests.yaml in the
.github/workflows/ folder.
4. Defining requirements.txt and requirements_tests.txt
5. Pushing Changes to Repository and Running Tests
6. Expanding CI Tests for Multiple OS and Python Versions
7. Implementing Caching in Workflow
8. Adding Code Coverage to Workflow
9. Setting Up Branch Protection Rules
10.Continuous Integration Data Testing
3
20/10/2024
Workflow File
Example of tests.yaml
4
20/10/2024
• requirements.txt:
• Contains all packages necessary to run the code.
• requirements_tests.txt:
• Contains additional packages required for running tests.
• Can be empty if no extra testing packages are needed.
• Used when specific testing libraries are not needed for the main code to
function.
10
10
5
20/10/2024
11
11
12
12
6
20/10/2024
13
13
14
14
7
20/10/2024
15
15
2. Update your workflow file to include steps for running coverage and
uploading results:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'
- run: pip install -r requirements.txt
- run: coverage run -m pytest # Run tests with coverage
- run: coverage xml # Generate coverage report in XML format
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml # Upload the coverage report
3. Ensure your repository is set up with Codecov by logging in and adding
your project.
4. Once implemented, your workflow will upload the code coverage report
as an artifact after running the tests.
16
16
8
20/10/2024
17
17
18
18
9
20/10/2024
1. Name the ruleset and set the target branches to the Default branch
(e.g., main or master).
2. Key rules to consider:
• Require a pull request before merging:
• Ensures changes are merged via pull requests.
• Allows for code review and testing before merging into the main branch.
• Optionally, set a requirement for a specific number of reviewers or approvals.
• Require status checks to pass:
• Ensures all workflows pass before merging.
• Select which workflows are mandatory (e.g., tests, code coverage).
19
19
20
20
10
20/10/2024
21
21
3. Successful Implementation:
• If the rules are created correctly, you should see a notification similar to the
image below when attempting to merge a pull request.
• All three checks must pass before merging:
• At least one approval from a reviewer.
• All workflows must be successful.
• All conversations must be resolved.
• A bypass rule for the repository admin should also be visible, allowing them to
merge regardless of the checks.
22
22
11
20/10/2024
23
23
24
24
12
20/10/2024
25
25
26
26
13
20/10/2024
• Next step: Update the workflow file with the following code:
- uses: iterative/setup-dvc@v1
- name: Get data
run: dvc pull
env:
GDRIVE_CREDENTIALS_DATA: ${{
secrets.GDRIVE_CREDENTIALS_DATA }}
• Purpose: This code runs dvc pull using the secret authentication file.
• Additional resource: For guidance, visit the provided small repository that
implements a similar workflow.
• Final steps:
• Add changes to the repository.
• Commit the updates.
• Push the changes.
• Confirm functionality: Ensure that unit tests relying on input data can run successfully.
27
27
28
28
14
20/10/2024
🧠 Knowledge check
29
29
🧠 Knowledge check
30
30
15
20/10/2024
31
31
16