-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
141 lines (113 loc) · 4.57 KB
/
Makefile
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
PACTICIPANT := "pactflow-example-provider-python"
WEBHOOK_UUID := "c76b601e-d66a-4eb1-88a4-6ebc50c0df8b"
PACT_CLI="docker run --network host --rm -v ${PWD}:${PWD} -e PACT_BROKER_BASE_URL -e PACT_BROKER_TOKEN -e PACT_BROKER_PUBLISH_VERIFICATION_RESULTS -e PACT_BROKER_USERNAME -e PACT_BROKER_PASSWORD pactfoundation/pact-cli:latest"
# Only deploy from master
ifeq ($(GIT_BRANCH),master)
DEPLOY_TARGET=deploy
else
DEPLOY_TARGET=no_deploy
endif
all: test
## ====================
## CI tasks
## ====================
ci: test can_i_deploy $(DEPLOY_TARGET)
# Run the ci target from a developer machine with the environment variables
# set as if it was on GitHub Actions.
# Use this for quick feedback when playing around with your workflows.
fake_ci: .env
CI=true \
GIT_COMMIT=`git rev-parse --short HEAD`+`date +%s` \
GIT_BRANCH=`git rev-parse --abbrev-ref HEAD` \
PACT_BROKER_PUBLISH_VERIFICATION_RESULTS=true \
make ci
ci_webhook: .env
make test
fake_ci_webhook:
CI=true \
GIT_COMMIT=`git rev-parse --short HEAD`+`date +%s` \
GIT_BRANCH=`git rev-parse --abbrev-ref HEAD` \
PACT_BROKER_PUBLISH_VERIFICATION_RESULTS=true \
make ci_webhook
## =====================
## Build/test tasks
## =====================
test: .env
docker compose up pact_verifier --exit-code-from pact_verifier
docker compose logs pact_verifier
test_native: .env
docker compose up pact_verifier_native --exit-code-from pact_verifier_native
docker compose logs pact_verifier_native
## =====================
## Deploy tasks
## =====================
deploy: deploy_app tag record_deployment
no_deploy:
@echo "Not deploying as not on master branch"
can_i_deploy: .env
@echo "can_i_deploy"
@"${PACT_CLI}" broker can-i-deploy \
--pacticipant ${PACTICIPANT} \
--version ${GIT_COMMIT} \
--to-environment production \
--retry-while-unknown 0 \
--retry-interval 10
deploy_app:
@echo "Deploying to prod"
tag: .env
@"${PACT_CLI}" broker create-version-tag \
--pacticipant ${PACTICIPANT} \
--version ${GIT_COMMIT} \
--auto-create-version \
--tag ${GIT_BRANCH}
record_deployment: .env
@"${PACT_CLI}" broker record-deployment --pacticipant ${PACTICIPANT} --version ${GIT_COMMIT} --environment production
## =====================
## PactFlow set up tasks
## =====================
# export the GITHUB_TOKEN environment variable before running this
create_github_token_secret:
curl -v -X POST ${PACT_BROKER_BASE_URL}/secrets \
-H "Authorization: Bearer ${PACT_BROKER_TOKEN}" \
-H "Content-Type: application/json" \
-H "Accept: application/hal+json" \
-d "{\"name\":\"githubToken\",\"description\":\"Github token\",\"value\":\"${GITHUB_TOKEN}\"}"
# NOTE: the github token secret must be created (either through the UI or using the
# `create_github_token_secret` target) before the webhook is invoked.
create_or_update_pact_changed_webhook:
"${PACT_CLI}" \
broker create-or-update-webhook \
"https://api.github.com/repos/${GITHUB_REPO}/dispatches" \
--header 'Content-Type: application/json' 'Accept: application/vnd.github.everest-preview+json' 'Authorization: Bearer $${user.githubToken}' \
--request POST \
--data '{ "event_type": "pact_changed", "client_payload": { "pact_url": "$${pactbroker.pactUrl}" } }' \
--uuid ${PACT_CHANGED_WEBHOOK_UUID} \
--consumer ${PACTICIPANT} \
--contract-content-changed \
--description "Pact content changed for ${PACTICIPANT}"
test_pact_changed_webhook:
@curl -v -X POST ${PACT_BROKER_BASE_URL}/webhooks/${PACT_CHANGED_WEBHOOK_UUID}/execute -H "Authorization: Bearer ${PACT_BROKER_TOKEN}"
## ======================
## Misc
## ======================
PROJECT := example-provider-python
PYTHON_MAJOR_VERSION := 3.11
sgr0 := $(shell tput sgr0)
red := $(shell tput setaf 1)
green := $(shell tput setaf 2)
.env:
touch .env
venv:
@if [ -d "./.venv" ]; then echo "$(red).venv already exists, not continuing!$(sgr0)"; exit 1; fi
@type pyenv >/dev/null 2>&1 || (echo "$(red)pyenv not found$(sgr0)"; exit 1)
@echo "\n$(green)Try to find the most recent minor version of the major version specified$(sgr0)"
$(eval PYENV_VERSION=$(shell pyenv install -l | grep "\s\s$(PYTHON_MAJOR_VERSION)\.*" | tail -1 | xargs))
@echo "$(PYTHON_MAJOR_VERSION) -> $(PYENV_VERSION)"
@echo "\n$(green)Install the Python pyenv version if not already available$(sgr0)"
pyenv install $(PYENV_VERSION) -s
@echo "\n$(green)Make a .venv dir$(sgr0)"
~/.pyenv/versions/${PYENV_VERSION}/bin/python3 -m venv ${CURDIR}/.venv
@echo "\n$(green)Make it 'available' to pyenv$(sgr0)"
ln -sf ${CURDIR}/.venv ~/.pyenv/versions/${PROJECT}
@echo "\n$(green)Use it! (populate .python-version)$(sgr0)"
pyenv local ${PROJECT}