Skip to content

Commit 3761205

Browse files
authored
ci: further improve pr deployments (coder#8764)
1 parent e85b88c commit 3761205

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

.github/workflows/pr-cleanup.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Cleanup PR
1+
name: Cleanup PR deployment and image
22
on:
33
pull_request:
44
types: [closed]

.github/workflows/pr-deploy.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# This action will trigger when a PR is commentted containing /review-pr by a member of the org.
1+
# This action will trigger when a PR is commented on with `/deploy-pr` or when the workflow is manually triggered.
22
name: Deploy PR
33
on:
44
issue_comment:

scripts/deploy-pr.sh

+51-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,67 @@
11
#!/usr/bin/env bash
2-
# Usage: ./deploy-pr.sh --skip-build
2+
# Usage: ./deploy-pr.sh [--skip-build -s] [--dry-run -n] [--yes -y]
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
5-
# if --skip-build is passed, the build step will be skipped and the last build image will be used
65

7-
set -euox pipefail
6+
set -euo pipefail
87

9-
branchName=$(gh pr view --json headRefName | jq -r .headRefName)
8+
# default settings
9+
skipBuild=false
10+
dryRun=false
11+
confirm=true
12+
13+
# parse arguments
14+
for arg in "$@"; do
15+
case $arg in
16+
-s | --skip-build)
17+
skipBuild=true
18+
shift # Remove --skip-build from processing
19+
;;
20+
-n | --dry-run)
21+
dryRun=true
22+
shift # Remove --dry-run from processing
23+
;;
24+
-y | --yes)
25+
confirm=false
26+
shift # Remove --yes from processing
27+
;;
28+
*)
29+
shift # Remove generic argument from processing
30+
;;
31+
esac
32+
done
1033

11-
if [[ "$branchName" == "main" ]]; then
12-
prNumber=$(git rev-parse --short HEAD)
13-
else
14-
prNumber=$(gh pr view --json number | jq -r .number)
34+
# confirm if not passed -y or --yes
35+
if $confirm; then
36+
read -p "Are you sure you want to deploy? (y/n) " -n 1 -r
37+
echo
38+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
39+
exit 1
40+
fi
1541
fi
1642

17-
# if --skip-build is passed, the build job will be skipped and the last built image will be used
18-
if [[ "$*" == *--skip-build* ]]; then
19-
skipBuild=true
43+
# get branch name and pr number
44+
branchName=$(gh pr view --json headRefName | jq -r .headRefName)
45+
prNumber=$(gh pr view --json number | jq -r .number)
46+
47+
if $skipBuild; then
2048
#check if the image exists
21-
foundTag=$(curl -fsSL https://github.com/coder/coder/pkgs/container/coder-preview | grep -o "$prNumber" | head -n 1)
22-
if [ -z "${foundTag}" ]; then
49+
foundTag=$(curl -fsSL https://github.com/coder/coder/pkgs/container/coder-preview | grep -o "$prNumber" | head -n 1) || true
50+
echo "foundTag is: '${foundTag}'"
51+
if [[ -z "${foundTag}" ]]; then
2352
echo "Image not found"
2453
echo "${prNumber} tag not found in ghcr.io/coder/coder-preview"
2554
echo "Please remove --skip-build and try again"
2655
exit 1
2756
fi
28-
else
29-
skipBuild=false
3057
fi
3158

32-
gh workflow run pr-deploy.yaml --ref "${branchName}" -f pr_number="${prNumber}" -f skip_build="${skipBuild}"
59+
if $dryRun; then
60+
echo "dry run"
61+
echo "branchName: ${branchName}"
62+
echo "prNumber: ${prNumber}"
63+
echo "skipBuild: ${skipBuild}"
64+
exit 0
65+
fi
66+
67+
gh workflow run pr-deploy.yaml --ref "${branchName}" -f "pr_number=${prNumber}" -f "skip_build=${skipBuild}"

0 commit comments

Comments
 (0)