Skip to content

Commit 0ec9c8a

Browse files
committed
separate build and deploy
1 parent 27c175c commit 0ec9c8a

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

.github/pr-deployments/kubeconfig.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ apiVersion: v1
22
kind: Config
33
clusters:
44
- cluster:
5-
certificate-authority-data: $CLUSTER_CA
6-
server: $CLUSTER_ENDPOINT
5+
certificate-authority-data: ${CLUSTER_CA}
6+
server: ${CLUSTER_ENDPOINT}
77
name: pr${PR_NUMBER}
88
contexts:
99
- context:
@@ -15,4 +15,4 @@ current-context: pr${PR_NUMBER}
1515
users:
1616
- name: coder-workspace
1717
user:
18-
token: $TOKEN
18+
token: ${TOKEN}

.github/workflows/pr-deploy.yaml

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ on:
1717
required: false
1818
type: string
1919
default: "*"
20-
force:
21-
description: "Force new build and deploy"
20+
build:
21+
description: "Force new build"
22+
required: false
23+
type: boolean
24+
default: false
25+
deploy:
26+
description: "Force new deployment"
2227
required: false
2328
type: boolean
2429
default: false
@@ -46,7 +51,7 @@ jobs:
4651
CODER_BASE_IMAGE_TAG: ${{ steps.set_tags.outputs.CODER_BASE_IMAGE_TAG }}
4752
CODER_IMAGE_TAG: ${{ steps.set_tags.outputs.CODER_IMAGE_TAG }}
4853
NEW: ${{ steps.check_deployment.outputs.new }}
49-
BUILD: ${{ steps.filter.outputs.all_count > steps.filter.outputs.ignored_count || steps.check_deployment.outputs.new || github.event.inputs.force == 'true' }}
54+
BUILD: ${{ steps.filter.outputs.all_count > steps.filter.outputs.ignored_count || steps.check_deployment.outputs.new || github.event.inputs.build == 'true' }}
5055

5156
runs-on: "ubuntu-latest"
5257
steps:
@@ -209,7 +214,7 @@ jobs:
209214
# Run deploy job only if build job was successful or skipped
210215
if: |
211216
always() && (needs.build.result == 'success' || needs.build.result == 'skipped') &&
212-
(github.event_name == 'workflow_dispatch' || needs.get_info.outputs.NEW == 'false')
217+
(github.event_name == 'workflow_dispatch' || needs.get_info.outputs.NEW == 'false' || github.event.inputs.deploy == 'true')
213218
runs-on: "ubuntu-latest"
214219
env:
215220
CODER_IMAGE_TAG: ${{ needs.get_info.outputs.CODER_IMAGE_TAG }}
@@ -227,7 +232,7 @@ jobs:
227232
export KUBECONFIG=~/.kube/config
228233
229234
- name: Check if image exists
230-
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
235+
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
231236
run: |
232237
set -euo pipefail
233238
foundTag=$(curl -fsSL https://github.com/coder/coder/pkgs/container/coder-preview | grep -o ${{ env.CODER_IMAGE_TAG }} | head -n 1)
@@ -238,7 +243,7 @@ jobs:
238243
fi
239244
240245
- name: Add DNS record to Cloudflare
241-
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
246+
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
242247
run: |
243248
curl -X POST "https://api.cloudflare.com/client/v4/zones/${{ secrets.PR_DEPLOYMENTS_ZONE_ID }}/dns_records" \
244249
-H "Authorization: Bearer ${{ secrets.PR_DEPLOYMENTS_CLOUDFLARE_API_TOKEN }}" \
@@ -251,15 +256,15 @@ jobs:
251256
ref: ${{ env.PR_BRANCH }}
252257

253258
- name: Create PR namespace
254-
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
259+
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
255260
run: |
256261
set -euo pipefail
257262
# try to delete the namespace, but don't fail if it doesn't exist
258263
kubectl delete namespace "pr${{ env.PR_NUMBER }}" || true
259264
kubectl create namespace "pr${{ env.PR_NUMBER }}"
260265
261266
- name: Check and Create Certificate
262-
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
267+
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
263268
run: |
264269
# Using kubectl to check if a Certificate resource already exists
265270
# we are doing this to avoid letsenrypt rate limits
@@ -282,7 +287,7 @@ jobs:
282287
)
283288
284289
- name: Set up PostgreSQL database
285-
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
290+
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
286291
run: |
287292
helm repo add bitnami https://charts.bitnami.com/bitnami
288293
helm install coder-db bitnami/postgresql \
@@ -295,18 +300,18 @@ jobs:
295300
--from-literal=url="postgres://coder:coder@coder-db-postgresql.pr${{ env.PR_NUMBER }}.svc.cluster.local:5432/coder?sslmode=disable"
296301
297302
- name: Create a kubeconfig for the workspace
298-
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
303+
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
299304
run: |
300305
set -euo pipefail
301306
# Create service account, role, rolebinding and secret
302307
envsubst < ./.github/pr-deployments/rbac.yaml | kubectl apply -f -
303308
304309
# Get the token for the service account
305-
TOKEN=$(kubectl -n pr${{ env.PR_NUMBER }} get secret coder-workspace-token -o jsonpath='{.data.token}' | base64 --decode)
310+
export TOKEN=$(kubectl -n pr${{ env.PR_NUMBER }} get secret coder-workspace-token -o jsonpath='{.data.token}' | base64 --decode)
306311
307312
# get CLUSTER_CA and CLUSTER_ENDPOINT
308-
CLUSTER_CA=$(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}')
309-
CLUSTER_ENDPOINT=$(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.server}')
313+
export CLUSTER_CA=$(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}')
314+
export CLUSTER_ENDPOINT=$(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.server}')
310315
311316
# Create a kubeconfig for the namespace to be used in the workspace
312317
envsubst < ./.github/pr-deployments/kubeconfig.yaml > ./namespace-kubeconfig.yaml
@@ -344,15 +349,15 @@ jobs:
344349
fi
345350
346351
- name: Install coder-logstream-kube
347-
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
352+
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
348353
run: |
349354
helm repo add coder-logstream-kube https://helm.coder.com/logstream-kube
350355
helm upgrade --install coder-logstream-kube coder-logstream-kube/coder-logstream-kube \
351356
--namespace "pr${{ env.PR_NUMBER }}" \
352357
--set url="https://pr${{ env.PR_NUMBER }}.${{ secrets.PR_DEPLOYMENTS_DOMAIN }}"
353358
354359
- name: Get Coder binary
355-
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
360+
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
356361
run: |
357362
set -euo pipefail
358363
@@ -378,7 +383,7 @@ jobs:
378383
mv "${DEST}" /usr/local/bin/coder
379384
380385
- name: Create first user, template and workspace
381-
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
386+
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
382387
id: setup_deployment
383388
run: |
384389
set -euo pipefail
@@ -415,7 +420,7 @@ jobs:
415420
coder stop kube -y
416421
417422
- name: Send Slack notification
418-
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
423+
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
419424
run: |
420425
curl -s -o /dev/null -X POST -H 'Content-type: application/json' \
421426
-d \

scripts/deploy-pr.sh

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
# Usage: ./deploy-pr.sh [--dry-run -n] [--yes -y] [--experiments -e <experiments>] [--force -f]
2+
# Usage: ./deploy-pr.sh [--dry-run -n] [--yes -y] [--experiments -e <experiments>] [--build -b] [--deploy -d]
33
# deploys the current branch to a PR environment and posts login credentials to
44
# [#pr-deployments](https://codercom.slack.com/archives/C05DNE982E8) Slack channel
55

@@ -8,14 +8,19 @@ set -euo pipefail
88
# default settings
99
dryRun=false
1010
confirm=true
11-
force=false
11+
build=false
12+
deploy=false
1213
experiments=""
1314

1415
# parse arguments
1516
while (("$#")); do
1617
case "$1" in
17-
-f | --force)
18-
force=true
18+
-b | --build)
19+
build=true
20+
shift
21+
;;
22+
-d | --deploy)
23+
deploy=true
1924
shift
2025
;;
2126
-n | --dry-run)
@@ -68,13 +73,15 @@ if $dryRun; then
6873
echo "branchName: ${branchName}"
6974
echo "prNumber: ${prNumber}"
7075
echo "experiments: ${experiments}"
71-
echo "force: ${force}"
76+
echo "build: ${build}"
77+
echo "deploy: ${deploy}"
7278
exit 0
7379
fi
7480

7581
echo "branchName: ${branchName}"
7682
echo "prNumber: ${prNumber}"
7783
echo "experiments: ${experiments}"
78-
echo "force: ${force}"
84+
echo "build: ${build}"
85+
echo "deploy: ${deploy}"
7986

80-
gh workflow run pr-deploy.yaml --ref "${branchName}" -f "pr_number=${prNumber}" -f "experiments=${experiments}" -f "force=${force}"
87+
gh workflow run pr-deploy.yaml --ref "${branchName}" -f "pr_number=${prNumber}" -f "experiments=${experiments}" -f "build=${build}" -f "deploy=${deploy}"

0 commit comments

Comments
 (0)