-
Notifications
You must be signed in to change notification settings - Fork 2
146 lines (119 loc) · 4.67 KB
/
test-contracts.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# This file is part of the Provider API Example.
#
# Copyright (C) 2023 Serghei Iakovlev <[email protected]>
#
# For the full copyright and license information, please view
# the LICENSE file that was distributed with this source code.
name: Contract Tests
on:
# Run this workflow only on PR to main branch
pull_request:
paths-ignore: [ '**.rst' ]
branches: [ main ]
# This will make it possible to make an API call from the consumer's workflow
repository_dispatch:
types: [ pacts_changed ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
inputs:
explicit_branch:
description: Branch in consumer repository
required: true
type: string
default: main
jobs:
contract-tests:
name: Contract Tests
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Detect current Git branch
id: current-branch
run: |
# This condition allows to use the branch the user has explicitly
# chosen using 'workflow_dispatch'
if [ "${{ github.event.inputs.explicit_branch }}" != "" ]; then
ref="${{ github.event.inputs.explicit_branch }}"
else
# Otherwise, define the necessary branch taking into
# account 'repository_dispatch'
if [ "${{ github.head_ref }}" != "" ]; then
ref="${{ github.head_ref }}"
else
ref="${{ github.ref }}"
fi
fi
# Cleanup branch name from 'refs/heads/' prefix
prefix="refs/heads/"
branch=${ref#"$prefix"}
# Store in the output for future use
echo "ref=$branch" >> $GITHUB_OUTPUT
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Setup Python cache
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Set up virtualenv
run: |
make init
source .venv/bin/activate
echo "$(pwd)/.venv/bin" >> $GITHUB_PATH
- name: Install dependencies
run: make install
# In real life, next steps (all with `id: consumer-*`) are not necessary,
# at least at the stage of testing the provider. Usually, when you test a
# provider, you get contracts (pacts) from a broker that would be
# self-hosted or using PactFlow. However, in this, a simplified example,
# we do not have a deployed broker, so we deploy it ourselves, right now.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BROKER START ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
- name: 'Checkout ${{ steps.current-branch.outputs.ref }} consumer brach'
id: consumer-checkout
uses: actions/checkout@v4
continue-on-error: true
with:
repository: sergeyklay/consumer-pact-example
path: consumer-pact-example
ref: ${{ steps.current-branch.outputs.ref }}
# Clone default branch, if the previous step failed.
- name: Checkout default consumer brach
id: consumer-checkout-default
uses: actions/checkout@v4
if: ${{ steps.consumer-checkout.outcome == 'failure' }}
with:
repository: sergeyklay/consumer-pact-example
path: consumer-pact-example
- name: Start broker
id: consumer-broker
run: docker compose up -d --quiet-pull --no-color
working-directory: consumer-pact-example
- name: Print docker logs
id: consumer-broker-logs
if: failure()
run: docker compose logs
working-directory: consumer-pact-example
- name: Publish consumer's pacts to broker
id: consumer-publish-pacts
run: ./publish-contracts.sh
working-directory: consumer-pact-example
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BROKER END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
- name: Download pact_verifier_cli
run: |
repo=https://github.com/pact-foundation/pact-reference
release=pact_verifier_cli-v0.10.4
file=pact_verifier_cli-linux-x86_64
archive="${file}.gz"
curl -o "${archive}" -sSL "${repo}/releases/download/${release}/${archive}"
gzip -dv "${archive}"
mkdir -p "${HOME}/.local/bin"
mv "${file}" "${HOME}/.local/bin/pact_verifier_cli"
chmod +x "${HOME}/.local/bin/pact_verifier_cli"
echo "${HOME}/.local/bin" >> $GITHUB_PATH
- name: Verify contracts
run: ./verify-contracts.sh