|
1 | 1 | #!/usr/bin/env bash
|
2 |
| -# Usage: ./deploy-pr.sh --skip-build |
| 2 | +# Usage: ./deploy-pr.sh [--skip-build -s] [--dry-run -n] [--yes -y] |
3 | 3 | # deploys the current branch to a PR environment and posts login credentials to
|
4 | 4 | # [#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 |
6 | 5 |
|
7 |
| -set -euox pipefail |
| 6 | +set -euo pipefail |
8 | 7 |
|
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 |
10 | 33 |
|
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 |
15 | 41 | fi
|
16 | 42 |
|
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 |
20 | 48 | #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 |
23 | 52 | echo "Image not found"
|
24 | 53 | echo "${prNumber} tag not found in ghcr.io/coder/coder-preview"
|
25 | 54 | echo "Please remove --skip-build and try again"
|
26 | 55 | exit 1
|
27 | 56 | fi
|
28 |
| -else |
29 |
| - skipBuild=false |
30 | 57 | fi
|
31 | 58 |
|
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