Skip to content

ci: upgrade pr deployments workflow #8924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 75 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
2d92a15
create-pr
matifali Aug 6, 2023
3ffb0c8
add permission to create serviceaccounts
matifali Aug 6, 2023
24f78d5
refactor
matifali Aug 6, 2023
52faafc
lint
matifali Aug 6, 2023
8b6ba6d
only build if needed
matifali Aug 6, 2023
f8121dd
Discard changes to helm/templates/rbac.yaml
matifali Aug 6, 2023
c633fec
Discard changes to helm/values.yaml
matifali Aug 6, 2023
51413cc
refactor
matifali Aug 6, 2023
4d370f7
wip
matifali Aug 6, 2023
65f1e08
update
matifali Aug 7, 2023
b8e6eab
add volume
matifali Aug 7, 2023
b0aaa69
set KUBECONFIG env variable
matifali Aug 7, 2023
098e5e6
Merge branch 'main' into matifali/pr-deployment-namespace-access
matifali Aug 8, 2023
cc65580
remove `--skip-build` and add `--force`
matifali Aug 8, 2023
003caf0
typo
matifali Aug 8, 2023
24dc6ca
test
matifali Aug 8, 2023
3e95af7
use a custom template
matifali Aug 8, 2023
4ff0966
finalize
matifali Aug 8, 2023
5923471
Merge branch 'main' into matifali/pr-deployment-namespace-access
matifali Aug 8, 2023
c64a7b4
use `set -euo pipefail`
matifali Aug 8, 2023
3f8599b
use `envsubst`
matifali Aug 8, 2023
9fa3d67
fmt
matifali Aug 8, 2023
3403530
fix typos
matifali Aug 9, 2023
fdbdf20
add template
matifali Aug 9, 2023
df4d829
use the included template
matifali Aug 9, 2023
27c175c
update
matifali Aug 9, 2023
0ec9c8a
separate build and deploy
matifali Aug 9, 2023
219a44b
do not force certificate creation
matifali Aug 9, 2023
b0afe7d
Merge branch 'main' into matifali/pr-deployment-namespace-access
matifali Aug 9, 2023
c5cbd16
update docs
matifali Aug 9, 2023
2ac3378
fix template
matifali Aug 9, 2023
1f3c2e2
use HOSTNAME
matifali Aug 9, 2023
d3f57b0
use better condition
matifali Aug 9, 2023
296d352
test `service_account_name`
matifali Aug 9, 2023
de998be
make fmt
matifali Aug 9, 2023
0a9eeeb
remove volumes
matifali Aug 9, 2023
f637501
fix renaming
matifali Aug 9, 2023
a1f2b8e
remove comments
matifali Aug 9, 2023
55524c6
add prefix
matifali Aug 9, 2023
4835b29
`terraform init`
matifali Aug 9, 2023
9576a3f
remove comments
matifali Aug 9, 2023
56d582c
change username to coder
matifali Aug 9, 2023
459b898
update docs
matifali Aug 9, 2023
188dfc9
Merge branch 'main' into matifali/pr-deployment-namespace-access
matifali Aug 9, 2023
7c71058
test parameter fro CLI
matifali Aug 9, 2023
4c8ab01
update condition
matifali Aug 9, 2023
96888a2
update comment
matifali Aug 9, 2023
b198d33
remove comments
matifali Aug 9, 2023
780f51c
use tailscale derpmap
matifali Aug 10, 2023
e3dac1c
use updated path of helm chart
matifali Aug 10, 2023
832e237
refactor to run on push
matifali Aug 10, 2023
76dbe1b
fix
matifali Aug 10, 2023
2062915
update permissions
matifali Aug 10, 2023
5be2f02
update
matifali Aug 10, 2023
4f5abfd
test
matifali Aug 10, 2023
62bcb8e
remove BRANCH NAME
matifali Aug 10, 2023
8a5f3f8
fix permission
matifali Aug 10, 2023
667acef
update checkout action
matifali Aug 10, 2023
40c3b74
update conditions
matifali Aug 10, 2023
fb29a3c
refactor
matifali Aug 10, 2023
232bed4
fix commenting
matifali Aug 10, 2023
e18a2c7
fix tag lookup
matifali Aug 10, 2023
57567e4
fixup!
matifali Aug 10, 2023
1767a70
Merge branch 'main' into matifali/pr-deployment-namespace-access
matifali Aug 10, 2023
1f5a5be
remove extra condition
matifali Aug 10, 2023
1a94dbb
debug
matifali Aug 10, 2023
f3b56a3
Update pr-deploy.yaml
matifali Aug 10, 2023
0bc368a
use PR_HOSTNAME
matifali Aug 11, 2023
b69af83
quoting
matifali Aug 11, 2023
ab7be03
Merge branch 'main' into matifali/pr-deployment-namespace-access
matifali Aug 11, 2023
ef13b94
run when build is true
matifali Aug 11, 2023
5906e8e
set BUILD to true if forced
matifali Aug 11, 2023
49f79f3
simplify condition
matifali Aug 11, 2023
bb6cd55
update deploy conditions
matifali Aug 11, 2023
d3a4c98
do a force deploy
matifali Aug 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
separate build and deploy
  • Loading branch information
matifali committed Aug 9, 2023
commit 0ec9c8ae6a61bee59df6720d1e3a2db5f723808d
6 changes: 3 additions & 3 deletions .github/pr-deployments/kubeconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: v1
kind: Config
clusters:
- cluster:
certificate-authority-data: $CLUSTER_CA
server: $CLUSTER_ENDPOINT
certificate-authority-data: ${CLUSTER_CA}
server: ${CLUSTER_ENDPOINT}
name: pr${PR_NUMBER}
contexts:
- context:
Expand All @@ -15,4 +15,4 @@ current-context: pr${PR_NUMBER}
users:
- name: coder-workspace
user:
token: $TOKEN
token: ${TOKEN}
39 changes: 22 additions & 17 deletions .github/workflows/pr-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ on:
required: false
type: string
default: "*"
force:
description: "Force new build and deploy"
build:
description: "Force new build"
required: false
type: boolean
default: false
deploy:
description: "Force new deployment"
required: false
type: boolean
default: false
Expand Down Expand Up @@ -46,7 +51,7 @@ jobs:
CODER_BASE_IMAGE_TAG: ${{ steps.set_tags.outputs.CODER_BASE_IMAGE_TAG }}
CODER_IMAGE_TAG: ${{ steps.set_tags.outputs.CODER_IMAGE_TAG }}
NEW: ${{ steps.check_deployment.outputs.new }}
BUILD: ${{ steps.filter.outputs.all_count > steps.filter.outputs.ignored_count || steps.check_deployment.outputs.new || github.event.inputs.force == 'true' }}
BUILD: ${{ steps.filter.outputs.all_count > steps.filter.outputs.ignored_count || steps.check_deployment.outputs.new || github.event.inputs.build == 'true' }}

runs-on: "ubuntu-latest"
steps:
Expand Down Expand Up @@ -209,7 +214,7 @@ jobs:
# Run deploy job only if build job was successful or skipped
if: |
always() && (needs.build.result == 'success' || needs.build.result == 'skipped') &&
(github.event_name == 'workflow_dispatch' || needs.get_info.outputs.NEW == 'false')
(github.event_name == 'workflow_dispatch' || needs.get_info.outputs.NEW == 'false' || github.event.inputs.deploy == 'true')
runs-on: "ubuntu-latest"
env:
CODER_IMAGE_TAG: ${{ needs.get_info.outputs.CODER_IMAGE_TAG }}
Expand All @@ -227,7 +232,7 @@ jobs:
export KUBECONFIG=~/.kube/config

- name: Check if image exists
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
run: |
set -euo pipefail
foundTag=$(curl -fsSL https://github.com/coder/coder/pkgs/container/coder-preview | grep -o ${{ env.CODER_IMAGE_TAG }} | head -n 1)
Expand All @@ -238,7 +243,7 @@ jobs:
fi

- name: Add DNS record to Cloudflare
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
run: |
curl -X POST "https://api.cloudflare.com/client/v4/zones/${{ secrets.PR_DEPLOYMENTS_ZONE_ID }}/dns_records" \
-H "Authorization: Bearer ${{ secrets.PR_DEPLOYMENTS_CLOUDFLARE_API_TOKEN }}" \
Expand All @@ -251,15 +256,15 @@ jobs:
ref: ${{ env.PR_BRANCH }}

- name: Create PR namespace
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
run: |
set -euo pipefail
# try to delete the namespace, but don't fail if it doesn't exist
kubectl delete namespace "pr${{ env.PR_NUMBER }}" || true
kubectl create namespace "pr${{ env.PR_NUMBER }}"

- name: Check and Create Certificate
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
run: |
# Using kubectl to check if a Certificate resource already exists
# we are doing this to avoid letsenrypt rate limits
Expand All @@ -282,7 +287,7 @@ jobs:
)

- name: Set up PostgreSQL database
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
run: |
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install coder-db bitnami/postgresql \
Expand All @@ -295,18 +300,18 @@ jobs:
--from-literal=url="postgres://coder:coder@coder-db-postgresql.pr${{ env.PR_NUMBER }}.svc.cluster.local:5432/coder?sslmode=disable"

- name: Create a kubeconfig for the workspace
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
run: |
set -euo pipefail
# Create service account, role, rolebinding and secret
envsubst < ./.github/pr-deployments/rbac.yaml | kubectl apply -f -

# Get the token for the service account
TOKEN=$(kubectl -n pr${{ env.PR_NUMBER }} get secret coder-workspace-token -o jsonpath='{.data.token}' | base64 --decode)
export TOKEN=$(kubectl -n pr${{ env.PR_NUMBER }} get secret coder-workspace-token -o jsonpath='{.data.token}' | base64 --decode)

# get CLUSTER_CA and CLUSTER_ENDPOINT
CLUSTER_CA=$(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}')
CLUSTER_ENDPOINT=$(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.server}')
export CLUSTER_CA=$(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}')
export CLUSTER_ENDPOINT=$(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.server}')

# Create a kubeconfig for the namespace to be used in the workspace
envsubst < ./.github/pr-deployments/kubeconfig.yaml > ./namespace-kubeconfig.yaml
Expand Down Expand Up @@ -344,15 +349,15 @@ jobs:
fi

- name: Install coder-logstream-kube
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
run: |
helm repo add coder-logstream-kube https://helm.coder.com/logstream-kube
helm upgrade --install coder-logstream-kube coder-logstream-kube/coder-logstream-kube \
--namespace "pr${{ env.PR_NUMBER }}" \
--set url="https://pr${{ env.PR_NUMBER }}.${{ secrets.PR_DEPLOYMENTS_DOMAIN }}"

- name: Get Coder binary
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
run: |
set -euo pipefail

Expand All @@ -378,7 +383,7 @@ jobs:
mv "${DEST}" /usr/local/bin/coder

- name: Create first user, template and workspace
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
id: setup_deployment
run: |
set -euo pipefail
Expand Down Expand Up @@ -415,7 +420,7 @@ jobs:
coder stop kube -y

- name: Send Slack notification
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.force == 'true'
if: needs.get_info.outputs.NEW == 'true' || github.event.inputs.deploy == 'true'
run: |
curl -s -o /dev/null -X POST -H 'Content-type: application/json' \
-d \
Expand Down
21 changes: 14 additions & 7 deletions scripts/deploy-pr.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# Usage: ./deploy-pr.sh [--dry-run -n] [--yes -y] [--experiments -e <experiments>] [--force -f]
# Usage: ./deploy-pr.sh [--dry-run -n] [--yes -y] [--experiments -e <experiments>] [--build -b] [--deploy -d]
# deploys the current branch to a PR environment and posts login credentials to
# [#pr-deployments](https://codercom.slack.com/archives/C05DNE982E8) Slack channel

Expand All @@ -8,14 +8,19 @@ set -euo pipefail
# default settings
dryRun=false
confirm=true
force=false
build=false
deploy=false
experiments=""

# parse arguments
while (("$#")); do
case "$1" in
-f | --force)
force=true
-b | --build)
build=true
shift
;;
-d | --deploy)
deploy=true
shift
;;
-n | --dry-run)
Expand Down Expand Up @@ -68,13 +73,15 @@ if $dryRun; then
echo "branchName: ${branchName}"
echo "prNumber: ${prNumber}"
echo "experiments: ${experiments}"
echo "force: ${force}"
echo "build: ${build}"
echo "deploy: ${deploy}"
exit 0
fi

echo "branchName: ${branchName}"
echo "prNumber: ${prNumber}"
echo "experiments: ${experiments}"
echo "force: ${force}"
echo "build: ${build}"
echo "deploy: ${deploy}"

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