Skip to content

Commit

Permalink
differences for PR #2
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Jun 3, 2024
1 parent ddb84b4 commit 20934c0
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 7 deletions.
File renamed without changes.
181 changes: 181 additions & 0 deletions 4-ci.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
---
title: Continuous Integration
teaching: 30
exercises: 45
---

:::::::::::::::::::::::::::::::::::::: questions

- How can we implement automatic testing each time we push changes to the repository?


::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::: objectives

- Set up Continuous Integration using GitHub Actions

::::::::::::::::::::::::::::::::::::::::::::::::

## Introducing Continuous integration
Follow [these slides](https://lyashevska.github.io/ds-cr-slides/ci/) to understand
the basics of continuous integration.

## Full-cycle collaborative workflow

::: challenge

### Exercise: Full-cycle collaborative workflow

The exercise takes 30-40 minutes.

In this exercise, everybody will:

- A. Set up automated tests with GitHub Actions
- B. Make test fail / find a bug in their repository
- C. Open an issue in their repository
- D. Then each one will clone the repo of one of their exercise partners, fix the bug, and open a pull request (GitHub)
- E. Everybody then merges their co-worker’s change


#### Step 1: Create a new repository on GitHub

- Select a different repository name than your colleagues (otherwise forking the same name will be strange)
- Before you create the repository, select “Initialize this repository with a README” (otherwise you try to clone an empty repo).
- Share the repository URL with your exercise group via shared document or chat

#### Step 2: Clone your own repository, add code, commit, and push

Clone the repository.

Add a file `example.py` containing:

```python
def add(a, b):
return a + b

def subtract(a, b):
return a + b # do not change this line until prompted to do so.
```

Write a test function `def test_add()` for `add` to check that this function is working properly. Do NOT add a test function for `subtract` (yet).
Run pytest to ensure it works

Then stage the file (`git add <filename>`), commit (`git commit -m "some commit message"`),
and push the changes (`git push`).


#### Step 3: Enable automated testing

In this step we will enable GitHub Actions.
- Select "Actions" from your GitHub repository page. You get to a page "Get started with GitHub Actions".
- Select the button for "Set up this workflow" under Python Application.

![](fig/python-application-workflow.png)
Select “Python application” as the starter workflow.

GitHub creates the following file for you in the subfolder `.github/workflows`:


```yaml
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python application

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
```
Commit the change by pressing the "Start Commit" button.
#### Step 4: Verify that tests have been automatically run
Observe in the repository how the test succeeds. While the test is executing, the repository has a yellow marker.
This is replaced with a green check mark, once the test succeeds.
![](fig/ci-tests-succeed-screenshot.png)
Green check means passed.
Also browse the "Actions" tab and look at the steps there and their output.
#### Step 5: Add a test which reveals a problem
After you committed the workflow file, your GitHub repository will be ahead of your local cloned repository. Update your local cloned repository:
```
$ git pull origin main
```

Next uncomment add a test function `test_subtract` for to check that the `subtract` function can subtract two numbers from each other, and push it to your remote repository.
Verify that the test suite now fails on the “Actions” tab (GitHub).


#### Step 6: Open an issue on GitHub
Open a new issue in your repository about the broken test (click the “Issues” button on GitHub and write a title for the issue). The plan is that your colleague will fix the issue through a pull request

#### Step 7: Fork and clone the repository of your colleague

Fork the repository using the GitHub web interface.
Make sure you clone the fork after you have forked it. Do not clone your colleague’s repository directly.


#### Step 8: Fix the broken test

Fix the function now and run pytest to check that it works.
Then push to _your fork_. Check whether the action now also passes.

#### Step 9: Open a pull request (GitHub)

Then before accepting the pull request from your colleague, observe how GitHub Actions automatically tested the code.

If you forgot to reference the issue number in the commit message, you can still add it to the pull request:
`my pull request title, closes #NUMBEROFTHEISSUE`

#### Step 10

Observe how accepting the pull request automatically closes the issue (provided the commit message or the pull request contained the correct issue number).

Discuss whether this is a useful feature. And if it is, why do you think is it useful?

:::

::::::::::::::::::::::::::::::::::::: keypoints

- Use GitHub Actions to automate checking your code each time you push changes to the repository.

::::::::::::::::::::::::::::::::::::::::::::::::


11 changes: 6 additions & 5 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ contact: '[email protected]'
# - another-learner.md

# Order of episodes in your lesson
episodes:
- testing.md
episodes:
- 3-testing.md
- 4-ci.md

# Information for Learners
learners:
learners:

# Information for Instructors
instructors:
instructors:

# Learner Profiles
profiles:
profiles:

# Customisation ---------------------------------------------
#
Expand Down
Binary file added fig/ci-python-application-workflow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fig/ci-tests-succeed-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions md5sum.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"file" "checksum" "built" "date"
"CODE_OF_CONDUCT.md" "c93c83c630db2fe2462240bf72552548" "site/built/CODE_OF_CONDUCT.md" "2022-08-05"
"LICENSE.md" "b24ebbb41b14ca25cf6b8216dda83e5f" "site/built/LICENSE.md" "2023-04-07"
"config.yaml" "17e6170ec8ab17ea31f9e33142c4a34b" "site/built/config.yaml" "2024-03-28"
"config.yaml" "f9a661125dfa0f31d5c4f595afd7493f" "site/built/config.yaml" "2024-06-03"
"index.md" "a02c9c785ed98ddd84fe3d34ddb12fcd" "site/built/index.md" "2022-04-22"
"links.md" "8184cf4149eafbf03ce8da8ff0778c14" "site/built/links.md" "2022-04-22"
"episodes/testing.md" "692b0da17f7a57bb354ca77e4f80bef8" "site/built/testing.md" "2024-03-28"
"episodes/3-testing.md" "692b0da17f7a57bb354ca77e4f80bef8" "site/built/3-testing.md" "2024-06-03"
"episodes/4-ci.md" "057159001402cf2f5e79117115c88f1f" "site/built/4-ci.md" "2024-06-03"
"instructors/instructor-notes.md" "cae72b6712578d74a49fea7513099f8c" "site/built/instructor-notes.md" "2023-03-16"
"learners/reference.md" "1c7cc4e229304d9806a13f69ca1b8ba4" "site/built/reference.md" "2023-03-16"
"learners/setup.md" "5456593e4a75491955ac4a252c05fbc9" "site/built/setup.md" "2024-01-26"
Expand Down

0 comments on commit 20934c0

Please sign in to comment.