forked from pactflow/example-consumer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
131 lines (105 loc) · 4.14 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
# Why are we using a Makefile? PactFlow has around 30 example consumer and provider projects that show how to use Pact.
# We often use them for demos and workshops, and Makefiles allow us to provide a consistent language and platform agnostic interface
# for each project. You do not need to use Makefiles to use Pact in your own project!
# Default to the read only token - the read/write token will be present on Travis CI.
# It's set as a secure environment variable in the .travis.yml file
GITHUB_ORG="pactflow"
PACTICIPANT="pactflow-example-consumer"
GITHUB_WEBHOOK_UUID := "04510dc1-7f0a-4ed2-997d-114bfa86f8ad"
PACT_CLI="docker run --rm -v ${PWD}:${PWD} -e PACT_BROKER_BASE_URL -e PACT_BROKER_TOKEN pactfoundation/pact-cli"
.EXPORT_ALL_VARIABLES:
GIT_COMMIT?=$(shell git rev-parse HEAD)
GIT_BRANCH?=$(shell git rev-parse --abbrev-ref HEAD)
ENVIRONMENT?=production
# Only deploy from master (to production env) or test (to test env)
ifeq ($(GIT_BRANCH),master)
ENVIRONMENT=production
DEPLOY_TARGET=deploy
else
ifeq ($(GIT_BRANCH),test)
ENVIRONMENT=test
DEPLOY_TARGET=deploy
else
DEPLOY_TARGET=no_deploy
endif
endif
all: test
## ====================
## CI tasks
## ====================
ci: test publish_pacts can_i_deploy $(DEPLOY_TARGET)
# Run the ci target from a developer machine with the environment variables
# set as if it was on CI.
# Use this for quick feedback when playing around with your workflows.
fake_ci: .env
@CI=true \
REACT_APP_API_BASE_URL=http://localhost:8080 \
make ci
publish_pacts: .env
@echo "\n========== STAGE: publish pacts ==========\n"
@"${PACT_CLI}" publish ${PWD}/pacts --consumer-app-version ${GIT_COMMIT} --branch ${GIT_BRANCH}
## =====================
## Build/test tasks
## =====================
test: .env
@echo "\n========== STAGE: test (pact) ==========\n"
npm run test:pact
## =====================
## Deploy tasks
## =====================
create_environment:
@"${PACT_CLI}" broker create-environment --name production --production
deploy: deploy_app record_deployment
no_deploy:
@echo "Not deploying as not on master branch"
can_i_deploy: .env
@echo "\n========== STAGE: can-i-deploy? ==========\n"
@"${PACT_CLI}" broker can-i-deploy \
--pacticipant ${PACTICIPANT} \
--version ${GIT_COMMIT} \
--to-environment ${ENVIRONMENT} \
--retry-while-unknown 30 \
--retry-interval 10
deploy_app:
@echo "\n========== STAGE: deploy ==========\n"
@echo "Deploying to ${ENVIRONMENT}"
record_deployment: .env
@"${PACT_CLI}" broker record-deployment --pacticipant ${PACTICIPANT} --version ${GIT_COMMIT} --environment ${ENVIRONMENT}
## =====================
## PactFlow set up tasks
## =====================
# This should be called once before creating the webhook
# with the environment variable GITHUB_TOKEN set
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\":\"githubCommitStatusToken\",\"description\":\"Github token for updating commit statuses\",\"value\":\"${GITHUB_TOKEN}\"}"
# This webhook will update the Github commit status for this commit
# so that any PRs will get a status that shows what the status of
# the pact is.
create_or_update_github_commit_status_webhook:
@"${PACT_CLI}" \
broker create-or-update-webhook \
'https://api.github.com/repos/pactflow/example-consumer/statuses/$${pactbroker.consumerVersionNumber}' \
--header 'Content-Type: application/json' 'Accept: application/vnd.github.v3+json' 'Authorization: token $${user.githubCommitStatusToken}' \
--request POST \
--data @${PWD}/pactflow/github-commit-status-webhook.json \
--uuid ${GITHUB_WEBHOOK_UUID} \
--consumer ${PACTICIPANT} \
--contract-published \
--provider-verification-published \
--description "Github commit status webhook for ${PACTICIPANT}"
test_github_webhook:
@curl -v -X POST ${PACT_BROKER_BASE_URL}/webhooks/${GITHUB_WEBHOOK_UUID}/execute -H "Authorization: Bearer ${PACT_BROKER_TOKEN}"
## ======================
## Misc
## ======================
.env:
touch .env
output:
mkdir -p ./pacts
touch ./pacts/tmp
clean: output
rm pacts/*