-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcodefresh.yaml
181 lines (171 loc) · 6.71 KB
/
codefresh.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
version: '1.0'
steps:
# Requires setting GITHUB_TOKEN ENV var in Codefresh UI.
# See https://github.com/scottrigby/cf-export
CFExportPR:
title: Export CF_PR_ ENV vars
image: r6by/cf-github-pr-export:0.2.1
# @todo Branch protection will not work until we have a way to determine the
# Pull Request base branch name (not currently included in ENV vars), which is
# needed to set GITHUB_REF ENV var below. Perhaps export the base branch as
# part of r6by/cf-github-pr-export?
# GitHubUpdateBranchProtection:
# title: Update branch protection
# image: cloudposse/github-status-updater
# environment:
# - GITHUB_ACTION=update_branch_protection
# - GITHUB_TOKEN=${{GITHUB_TOKEN}}
# - GITHUB_OWNER=${{CF_REPO_OWNER}}
# - GITHUB_REPO=${{CF_REPO_NAME}}
# - GITHUB_REF=MISSING_INFO
# - GITHUB_CONTEXT=PR Release
# when:
# condition:
# all:
# prOpen: '"${{CF_PR_STATE}}" == "OPEN"'
# Set ENV vars that must be evaluated (can not be set via Codegresh UI).
CFExportK8S:
title: Export K8S ENV vars
image: 'codefresh/plugin-helm:2.8.0'
commands:
# Helper to get the name to set the context farther below. Codefresh sets
# the kubeconfig based on your clusters, before running build steps. Here
# we assume a single cluster is configured. If you want to specify the
# cluster, "KUBE_CONTEXT" could be set manually as an ENV var in the
# Codefresh UI.
# @todo This would be a nice built-in feature of Codefresh, if there is
# only one K8S cluster configured.
- cf_export KUBE_CONTEXT=$(kubectl config get-contexts -o name | head -n 1)
CFExportCommon:
title: Export common ENV vars
image: alpine:latest
commands:
# Set Helm release name for reference farther below.
- cf_export RELEASE_NAME=$(echo cf-${CF_REPO_OWNER}-${CF_REPO_NAME}-${CF_PR_NUMBER} | awk '{print tolower($0)}' | awk '{gsub("_", "-") ; print $0}')
# Always run.
# @todo Expose PR event trigger actions as ENV vars or structured conditionals.
HelmDelete:
title: Helm Delete
image: 'codefresh/plugin-helm:2.8.0'
commands:
- kubectl config use-context ${{KUBE_CONTEXT}}
- |-
if helm status ${RELEASE_NAME} > /dev/null 2>&1; then
helm delete --purge ${RELEASE_NAME}
echo 'Deleted PR env'
else
echo 'No PR env to delete'
fi
# Requires setting GITHUB_TOKEN ENV var in Codefresh UI.
# We clear any previous deploy status so old build status/url will not be
# mistaken for the current build in progress.
GitHubClearDeployStatus:
title: Set PR deployment status to "pending"
image: cloudposse/github-status-updater
environment:
- GITHUB_ACTION=update_state
- GITHUB_TOKEN=${{GITHUB_TOKEN}}
- GITHUB_OWNER=${{CF_REPO_OWNER}}
- GITHUB_REPO=${{CF_REPO_NAME}}
- GITHUB_REF=${{CF_REVISION}}
- GITHUB_CONTEXT=PR Release
- GITHUB_STATE=pending
- GITHUB_DESCRIPTION=Waiting for successful build
when:
condition:
all:
prOpen: '"${{CF_PR_STATE}}" == "OPEN"'
# Run only if PR is not closed or merged (see HelmDelete step commands above).
BuildingDockerImage:
title: Building Docker Image
type: build
image_name: ${{RELEASE_NAME}}
working_directory: ./
dockerfile: Dockerfile
tag: '${{CF_REVISION}}'
when:
condition:
all:
prOpen: '"${{CF_PR_STATE}}" == "OPEN"'
# Fixes stale builds, which in cases have not included all image layers.
# Note that enabling this field will slow down builds, so it is a trade-off.
# ref: https://codefresh.io/docs/docs/codefresh-yaml/steps/build-1/
# no_cache: true
PushingToDockerRegistry:
title: Pushing to Docker Registry
type: push
candidate: '${{BuildingDockerImage}}'
tag: '${{CF_REVISION}}'
registry: gcr
when:
condition:
all:
prOpen: '"${{CF_PR_STATE}}" == "OPEN"'
# Requires setting NAMESPACE, REGISTRY_DOMAIN and REGISTRY_ACCOUNT ENV var in
# Codefresh UI.
# Note that this step will always be custom to the application and chart,
# but it must export HELM_URL ENV var in order for the following step to work.
HelmInstall:
title: Helm Install
image: 'codefresh/plugin-helm:2.8.0'
# @todo Is setting the working_directory required?
working_directory: ./
commands:
- kubectl config use-context ${KUBE_CONTEXT}
- helm install go-hello/ --namespace=${NAMESPACE} --name ${RELEASE_NAME} --set image.repository=${REGISTRY_DOMAIN}/${REGISTRY_ACCOUNT}/${RELEASE_NAME} --set image.tag=${CF_REVISION}
- |-
SERVICE_NAME=${RELEASE_NAME}-go-hello
PORT=${PORT:-80}
SCHEME=${SCHEME:-http}
for i in $(seq 1 ${RETRIES:=20});
do
echo "Service IP is still pending. Waiting..."
SERVICE_IP=$(kubectl get svc --namespace ${NAMESPACE} ${SERVICE_NAME} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
if [ ! -z "$SERVICE_IP" ]; then
echo "HELM_URL=${SCHEME}://${SERVICE_IP}:${PORT}" >> ${CF_VOLUME_PATH}/env_vars_to_export
echo "Exported HELM_URL ${HELM_URL}"
exit 0
fi
sleep ${WAIT:-5}
done
echo "Service IP didn't resolve in ${RETRIES} retries"
exit 1
when:
condition:
all:
prOpen: '"${{CF_PR_STATE}}" == "OPEN"'
# Requires setting GITHUB_TOKEN ENV var in Codefresh UI.
GitHubSetDeployStatus:
title: Set PR deployment status to "success"
image: cloudposse/github-status-updater
environment:
- GITHUB_ACTION=update_state
- GITHUB_TOKEN=${{GITHUB_TOKEN}}
- GITHUB_OWNER=${{CF_REPO_OWNER}}
- GITHUB_REPO=${{CF_REPO_NAME}}
- GITHUB_REF=${{CF_REVISION}}
- GITHUB_CONTEXT=PR Release
- GITHUB_STATE=success
- GITHUB_DESCRIPTION=Deployed to ${{NAMESPACE}} namespace
# See HelmInstall step.
- GITHUB_TARGET_URL=${{HELM_URL}}
when:
condition:
all:
prOpen: '"${{CF_PR_STATE}}" == "OPEN"'
helmURL: '"${{HELM_URL}}" != ""'
# Optionally send Slack notification by setting SLACK_WEB_URL ENV var in
# Codefresh UI.
slack_notify:
title: Slack notification
image: tutum/curl
commands:
- export GITHUB_PR_URL=https://github.com/${CF_REPO_OWNER}/${CF_REPO_NAME}/pull/${CF_PR_NUMBER}
- export DATA="{\"text\":\"<${GITHUB_PR_URL}|PR ${CF_PR_NUMBER}> release <${HELM_URL}|deployed> :rocket:\"}"
- curl -X POST --data-urlencode "payload=${DATA}" ${SLACK_WEB_URL}
when:
condition:
all:
prOpen: '"${{CF_PR_STATE}}" == "OPEN"'
helmURL: '"${{HELM_URL}}" != ""'
webhookSet: '"${{SLACK_WEB_URL}}" != ""'