Skip to content

Commit

Permalink
ci: add support for python in GH actions
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Aug 22, 2022
1 parent 4720ac6 commit b8ecd5e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 87 deletions.
36 changes: 31 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ on:
workflow_dispatch:

env:
PACT_BROKER_BASE_URL: https://test.pactflow.io
PACT_BROKER_BASE_URL: https://saf.pactflow.io
PACT_BROKER_TOKEN: ${{ secrets.PACTFLOW_TOKEN_FOR_CI_CD_WORKSHOP }}
REACT_APP_API_BASE_URL: http://localhost:8080
GIT_COMMIT: ${{ github.sha }}
GIT_REF: ${{ github.ref }}

Expand All @@ -18,11 +17,38 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v4
with:
python-version: '3.8'
python-version: "3.8"
# - name: Install python version
# uses: gabrielfalcao/pyenv-action@v10
# with:
# default: 3.8.13
# command: pip install -U pip # upgrade pip after installing python
# - name: Create virtualenv for python 3.5.7
# run: pyenv local 3.8.13 && python3 -mvenv .venv
# - name: venv
# run: pyenv local 3.8.13 && python3 -mvenv .venv3813
- name: install pyenv
run: curl https://pyenv.run | bash
- name: set pyenv on path
run: |
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH" >> .bashrc
eval "$(pyenv init -)" >> .bashrc
eval "$(pyenv virtualenv-init -)" >> .bashrc
source ~/.bashrc
echo PATH="$PYENV_ROOT/bin:$PATH" >> $GITHUB_ENV
- name: Install poetry
run: pip install poetry
- name: Setup Virtual env
run: make venv
- name: Install
run: make deps
- name: Test
run: make test
- name: Activate virtual env
run: |
source .venv/bin/activate
make test
# - name: Test
# run: make test
- name: Publish pacts
run: GIT_BRANCH=${GIT_REF:11} make publish_pacts

Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.venv
.aws-sam
.idea
.vscode
.python-version
__pycache__
__pycache__
pacts
.pytest_cache
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fake_ci: .env


publish_pacts: .env
@"${PACT_CLI}" publish ${PWD}/pacts --consumer-app-version ${GIT_COMMIT} --tag ${GIT_BRANCH}
@"${PACT_CLI}" publish ${PWD}/pacts --consumer-app-version ${GIT_COMMIT} --tag ${GIT_BRANCH} --branch ${GIT_BRANCH}

## =====================
## Build/test tasks
Expand Down Expand Up @@ -142,10 +142,10 @@ venv:
@echo "\n$(green)Use it! (populate .python-version)$(sgr0)"
pyenv local ${PROJECT}

deploy:
deploy_sam:
scripts/deploy.sh

publish:
publish_sam:
scripts/publish.sh

logs:
Expand Down
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,34 @@ You're probably familiar with layered architectures such as Ports and Adaptors (

This code base is setup with this modularity in mind:

* [Lambda Handler](src/_lambda/product.py)
* [Event Service](src/product/product_service.py)
* Business Logic
* [Product](src/product/product.py)
* [Repository](src/product/product_repository.py)
- [Lambda Handler](src/_lambda/product.py)
- [Event Service](src/product/product_service.py)
- Business Logic
- [Product](src/product/product.py)
- [Repository](src/product/product_repository.py)

The target of our [consumer pact test](tests/unit/product_service_pact_test.py) is the [Event Service](src/product/product_service.js), which is responsible for consuming a Product update event, and persisting it to a database (the Repository).

See also:

* https://dius.com.au/2017/09/22/contract-testing-serverless-and-asynchronous-applications/
* https://dius.com.au/2018/10/01/contract-testing-serverless-and-asynchronous-applications---part-2/
- https://dius.com.au/2017/09/22/contract-testing-serverless-and-asynchronous-applications/
- https://dius.com.au/2018/10/01/contract-testing-serverless-and-asynchronous-applications---part-2/

## Usage

### Testing

* Run the unit tests: `make test`
* Run a (local) lambda integration test: `make integration`
- Run the unit tests: `make test`
- Run a (local) lambda integration test: `make integration`

### Running

* Deploy the actual app: `make deploy` (see below for more background)
* Publish a test event: `make publish`
* View the lambda logs: `make logs`
- Deploy the actual app: `make deploy_sam` (see below for more background)
- Publish a test event: `make publish_sam`
- View the lambda logs: `make logs`

Here is some sample output publishing and viewing the logs:

```
➜ example-consumer-js-sns git:(master) ✗ npm run publish <aws:pact-dev>
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions tests/unit/product_service_pact_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from src.product.product_service import receive_product_update

CONSUMER_NAME = "pactflow-example-consumer-python-sns"
PROVIDER_NAME = os.getenv("PACT_PROVIDER", "pactflow-example-provider-python-sns")
PACT_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "pacts")
PROVIDER_NAME = os.getenv("PACT_PROVIDER", "pactflow-example-provider-js-sns")
PACT_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),"..","..", "pacts")

@pytest.fixture(scope="session")
def consumer():
Expand Down

0 comments on commit b8ecd5e

Please sign in to comment.