From d77492ca7b2e3312d6ac5b6bf2d2b14d0242b09f Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Thu, 27 Jul 2023 13:32:39 +0300 Subject: [PATCH 1/8] grammer and typo --- .github/workflows/pr-cleanup.yaml | 2 +- .github/workflows/pr-deploy.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pr-cleanup.yaml b/.github/workflows/pr-cleanup.yaml index f3ec9474b136d..5029943371161 100644 --- a/.github/workflows/pr-cleanup.yaml +++ b/.github/workflows/pr-cleanup.yaml @@ -1,4 +1,4 @@ -name: Cleanup PR +name: Cleanup PR deployment and image on: pull_request: types: [closed] diff --git a/.github/workflows/pr-deploy.yaml b/.github/workflows/pr-deploy.yaml index ee4d274341e3f..b02bebfd9681f 100644 --- a/.github/workflows/pr-deploy.yaml +++ b/.github/workflows/pr-deploy.yaml @@ -1,4 +1,4 @@ -# This action will trigger when a PR is commentted containing /review-pr by a member of the org. +# This action will trigger when a PR is commented on with `/deploy` or when the workflow is manually triggered. name: Deploy PR on: issue_comment: From 2b5d1f5699928bdc993df7ee2f80af05bdb98888 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Thu, 27 Jul 2023 13:55:17 +0300 Subject: [PATCH 2/8] only deploy PRs --- .github/workflows/pr-deploy.yaml | 2 +- scripts/deploy-pr.sh | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.github/workflows/pr-deploy.yaml b/.github/workflows/pr-deploy.yaml index 26115a02a12e1..489092bda9c19 100644 --- a/.github/workflows/pr-deploy.yaml +++ b/.github/workflows/pr-deploy.yaml @@ -1,4 +1,4 @@ -# This action will trigger when a PR is commented on with `/deploy` or when the workflow is manually triggered. +# This action will trigger when a PR is commented on with `/deploy-pr` or when the workflow is manually triggered. name: Deploy PR on: issue_comment: diff --git a/scripts/deploy-pr.sh b/scripts/deploy-pr.sh index cd847a5b25591..8ffe4e26362db 100755 --- a/scripts/deploy-pr.sh +++ b/scripts/deploy-pr.sh @@ -3,18 +3,22 @@ # deploys the current branch to a PR environment and posts login credentials to # [#pr-deployments](https://codercom.slack.com/archives/C05DNE982E8) Slack channel # if --skip-build is passed, the build step will be skipped and the last build image will be used +# if --yes or -y is passed, the script will not ask for confirmation before deploying set -euox pipefail -branchName=$(gh pr view --json headRefName | jq -r .headRefName) - -if [[ "$branchName" == "main" ]]; then - prNumber=$(git rev-parse --short HEAD) -else - prNumber=$(gh pr view --json number | jq -r .number) +# ask for user confirmation before deploying also skip confirmation if --yes or -y is passed +if [[ "$*" != *--yes* ]] && [[ "$*" != *-y* ]]; then + read -p "Are you sure you want to deploy? (y/n) " -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + exit 1 + fi fi -# if --skip-build is passed, the build job will be skipped and the last built image will be used +branchName=$(gh pr view --json headRefName | jq -r .headRefName) +prNumber=$(gh pr view --json number | jq -r .number) + if [[ "$*" == *--skip-build* ]]; then skipBuild=true #check if the image exists From 24dc7c27ab3aea8d394c3688f76cb3f2e29e1700 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Thu, 27 Jul 2023 14:49:11 +0300 Subject: [PATCH 3/8] add dry run flag --- scripts/deploy-pr.sh | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/scripts/deploy-pr.sh b/scripts/deploy-pr.sh index 8ffe4e26362db..d521bfc70c988 100755 --- a/scripts/deploy-pr.sh +++ b/scripts/deploy-pr.sh @@ -5,7 +5,7 @@ # if --skip-build is passed, the build step will be skipped and the last build image will be used # if --yes or -y is passed, the script will not ask for confirmation before deploying -set -euox pipefail +set -euo pipefail # ask for user confirmation before deploying also skip confirmation if --yes or -y is passed if [[ "$*" != *--yes* ]] && [[ "$*" != *-y* ]]; then @@ -22,8 +22,9 @@ prNumber=$(gh pr view --json number | jq -r .number) if [[ "$*" == *--skip-build* ]]; then skipBuild=true #check if the image exists - foundTag=$(curl -fsSL https://github.com/coder/coder/pkgs/container/coder-preview | grep -o "$prNumber" | head -n 1) - if [ -z "${foundTag}" ]; then + foundTag=$(curl -fsSL https://github.com/coder/coder/pkgs/container/coder-preview | grep -o "$prNumber" | head -n 1) || true + echo "foundTag is: '${foundTag}'" + if [[ -z "${foundTag}" ]]; then echo "Image not found" echo "${prNumber} tag not found in ghcr.io/coder/coder-preview" echo "Please remove --skip-build and try again" @@ -33,4 +34,15 @@ else skipBuild=false fi +## dry run with --dry-run or -n + +if [[ "$*" == *--dry-run* ]] || [[ "$*" == *-n* ]]; then + echo "dry run" + echo "branchName: ${branchName}" + echo "prNumber: ${prNumber}" + echo "skipBuild: ${skipBuild}" + echo "gh workflow run pr-deploy.yaml --ref "${branchName}" -f pr_number="${prNumber}" -f skip_build="${skipBuild}"" + exit 0 +fi + gh workflow run pr-deploy.yaml --ref "${branchName}" -f pr_number="${prNumber}" -f skip_build="${skipBuild}" From 7e1987975850faf586a058560c7a8446a5d67c60 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Thu, 27 Jul 2023 15:08:26 +0300 Subject: [PATCH 4/8] add -s shortcut --- scripts/deploy-pr.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/deploy-pr.sh b/scripts/deploy-pr.sh index d521bfc70c988..0d563d837567a 100755 --- a/scripts/deploy-pr.sh +++ b/scripts/deploy-pr.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Usage: ./deploy-pr.sh --skip-build +# Usage: ./deploy-pr.sh --skip-build[os -s] --dry-run[or -n] --yes[or -y] # deploys the current branch to a PR environment and posts login credentials to # [#pr-deployments](https://codercom.slack.com/archives/C05DNE982E8) Slack channel # if --skip-build is passed, the build step will be skipped and the last build image will be used @@ -19,7 +19,7 @@ fi branchName=$(gh pr view --json headRefName | jq -r .headRefName) prNumber=$(gh pr view --json number | jq -r .number) -if [[ "$*" == *--skip-build* ]]; then +if [[ "$*" == *--skip-build* ]] || [[ "$*" == *-s* ]]; then skipBuild=true #check if the image exists foundTag=$(curl -fsSL https://github.com/coder/coder/pkgs/container/coder-preview | grep -o "$prNumber" | head -n 1) || true From 4c0d5ada4837a705f246dce5064dd126de90c771 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Thu, 27 Jul 2023 15:15:41 +0300 Subject: [PATCH 5/8] make it better --- scripts/deploy-pr.sh | 85 +++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 32 deletions(-) diff --git a/scripts/deploy-pr.sh b/scripts/deploy-pr.sh index 0d563d837567a..5277656d99949 100755 --- a/scripts/deploy-pr.sh +++ b/scripts/deploy-pr.sh @@ -1,48 +1,69 @@ #!/usr/bin/env bash -# Usage: ./deploy-pr.sh --skip-build[os -s] --dry-run[or -n] --yes[or -y] +# Usage: ./deploy-pr.sh [--skip-build -s] [--dry-run -n] [--yes -y] # deploys the current branch to a PR environment and posts login credentials to # [#pr-deployments](https://codercom.slack.com/archives/C05DNE982E8) Slack channel -# if --skip-build is passed, the build step will be skipped and the last build image will be used -# if --yes or -y is passed, the script will not ask for confirmation before deploying set -euo pipefail -# ask for user confirmation before deploying also skip confirmation if --yes or -y is passed -if [[ "$*" != *--yes* ]] && [[ "$*" != *-y* ]]; then - read -p "Are you sure you want to deploy? (y/n) " -n 1 -r - echo - if [[ ! $REPLY =~ ^[Yy]$ ]]; then - exit 1 - fi +# default settings +skipBuild=false +dryRun=false +confirm=true + +# parse arguments +for arg in "$@" +do + case $arg in + -s|--skip-build) + skipBuild=true + shift # Remove --skip-build from processing + ;; + -n|--dry-run) + dryRun=true + shift # Remove --dry-run from processing + ;; + -y|--yes) + confirm=false + shift # Remove --yes from processing + ;; + *) + shift # Remove generic argument from processing + ;; + esac +done + +# confirm if not passed -y or --yes +if $confirm; then + read -p "Are you sure you want to deploy? (y/n) " -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + exit 1 + fi fi +# get branch name and pr number branchName=$(gh pr view --json headRefName | jq -r .headRefName) prNumber=$(gh pr view --json number | jq -r .number) -if [[ "$*" == *--skip-build* ]] || [[ "$*" == *-s* ]]; then - skipBuild=true - #check if the image exists - foundTag=$(curl -fsSL https://github.com/coder/coder/pkgs/container/coder-preview | grep -o "$prNumber" | head -n 1) || true - echo "foundTag is: '${foundTag}'" - if [[ -z "${foundTag}" ]]; then - echo "Image not found" - echo "${prNumber} tag not found in ghcr.io/coder/coder-preview" - echo "Please remove --skip-build and try again" - exit 1 - fi -else - skipBuild=false +if $skipBuild; then + #check if the image exists + foundTag=$(curl -fsSL https://github.com/coder/coder/pkgs/container/coder-preview | grep -o "$prNumber" | head -n 1) || true + echo "foundTag is: '${foundTag}'" + if [[ -z "${foundTag}" ]]; then + echo "Image not found" + echo "${prNumber} tag not found in ghcr.io/coder/coder-preview" + echo "Please remove --skip-build and try again" + exit 1 + fi fi -## dry run with --dry-run or -n - -if [[ "$*" == *--dry-run* ]] || [[ "$*" == *-n* ]]; then - echo "dry run" - echo "branchName: ${branchName}" - echo "prNumber: ${prNumber}" - echo "skipBuild: ${skipBuild}" - echo "gh workflow run pr-deploy.yaml --ref "${branchName}" -f pr_number="${prNumber}" -f skip_build="${skipBuild}"" - exit 0 +if $dryRun; then + echo "dry run" + echo "branchName: ${branchName}" + echo "prNumber: ${prNumber}" + echo "skipBuild: ${skipBuild}" + echo "gh workflow run pr-deploy.yaml --ref "${branchName}" -f pr_number="${prNumber}" -f skip_build="${skipBuild}"" + exit 0 fi gh workflow run pr-deploy.yaml --ref "${branchName}" -f pr_number="${prNumber}" -f skip_build="${skipBuild}" From d6eb49b3f0964fafebf6650189fc2d3ef3072c8f Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Thu, 27 Jul 2023 12:17:01 +0000 Subject: [PATCH 6/8] fmt --- scripts/deploy-pr.sh | 77 ++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/scripts/deploy-pr.sh b/scripts/deploy-pr.sh index 5277656d99949..35c5eff2cacfe 100755 --- a/scripts/deploy-pr.sh +++ b/scripts/deploy-pr.sh @@ -11,34 +11,33 @@ dryRun=false confirm=true # parse arguments -for arg in "$@" -do - case $arg in - -s|--skip-build) - skipBuild=true - shift # Remove --skip-build from processing - ;; - -n|--dry-run) - dryRun=true - shift # Remove --dry-run from processing - ;; - -y|--yes) - confirm=false - shift # Remove --yes from processing - ;; - *) - shift # Remove generic argument from processing - ;; - esac +for arg in "$@"; do + case $arg in + -s | --skip-build) + skipBuild=true + shift # Remove --skip-build from processing + ;; + -n | --dry-run) + dryRun=true + shift # Remove --dry-run from processing + ;; + -y | --yes) + confirm=false + shift # Remove --yes from processing + ;; + *) + shift # Remove generic argument from processing + ;; + esac done # confirm if not passed -y or --yes if $confirm; then - read -p "Are you sure you want to deploy? (y/n) " -n 1 -r - echo - if [[ ! $REPLY =~ ^[Yy]$ ]]; then - exit 1 - fi + read -p "Are you sure you want to deploy? (y/n) " -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + exit 1 + fi fi # get branch name and pr number @@ -46,24 +45,24 @@ branchName=$(gh pr view --json headRefName | jq -r .headRefName) prNumber=$(gh pr view --json number | jq -r .number) if $skipBuild; then - #check if the image exists - foundTag=$(curl -fsSL https://github.com/coder/coder/pkgs/container/coder-preview | grep -o "$prNumber" | head -n 1) || true - echo "foundTag is: '${foundTag}'" - if [[ -z "${foundTag}" ]]; then - echo "Image not found" - echo "${prNumber} tag not found in ghcr.io/coder/coder-preview" - echo "Please remove --skip-build and try again" - exit 1 - fi + #check if the image exists + foundTag=$(curl -fsSL https://github.com/coder/coder/pkgs/container/coder-preview | grep -o "$prNumber" | head -n 1) || true + echo "foundTag is: '${foundTag}'" + if [[ -z "${foundTag}" ]]; then + echo "Image not found" + echo "${prNumber} tag not found in ghcr.io/coder/coder-preview" + echo "Please remove --skip-build and try again" + exit 1 + fi fi if $dryRun; then - echo "dry run" - echo "branchName: ${branchName}" - echo "prNumber: ${prNumber}" - echo "skipBuild: ${skipBuild}" - echo "gh workflow run pr-deploy.yaml --ref "${branchName}" -f pr_number="${prNumber}" -f skip_build="${skipBuild}"" - exit 0 + echo "dry run" + echo "branchName: ${branchName}" + echo "prNumber: ${prNumber}" + echo "skipBuild: ${skipBuild}" + echo "gh workflow run pr-deploy.yaml --ref "${branchName}" -f pr_number="${prNumber}" -f skip_build="${skipBuild}"" + exit 0 fi gh workflow run pr-deploy.yaml --ref "${branchName}" -f pr_number="${prNumber}" -f skip_build="${skipBuild}" From deb70d8f5e8a64e1591695a2331b731e5c5eb468 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Thu, 27 Jul 2023 15:18:35 +0300 Subject: [PATCH 7/8] unquote --- scripts/deploy-pr.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/deploy-pr.sh b/scripts/deploy-pr.sh index 35c5eff2cacfe..48bda3cde7b05 100755 --- a/scripts/deploy-pr.sh +++ b/scripts/deploy-pr.sh @@ -61,8 +61,8 @@ if $dryRun; then echo "branchName: ${branchName}" echo "prNumber: ${prNumber}" echo "skipBuild: ${skipBuild}" - echo "gh workflow run pr-deploy.yaml --ref "${branchName}" -f pr_number="${prNumber}" -f skip_build="${skipBuild}"" + echo "gh workflow run pr-deploy.yaml --ref ${branchName} -f pr_number=${prNumber} -f skip_build=${skipBuild}" exit 0 fi -gh workflow run pr-deploy.yaml --ref "${branchName}" -f pr_number="${prNumber}" -f skip_build="${skipBuild}" +gh workflow run pr-deploy.yaml --ref ${branchName} -f pr_number=${prNumber} -f skip_build=${skipBuild} From 62894172845582a14384c066d8811701accaf8b6 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Thu, 27 Jul 2023 12:31:31 +0000 Subject: [PATCH 8/8] lint --- scripts/deploy-pr.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/deploy-pr.sh b/scripts/deploy-pr.sh index 48bda3cde7b05..411963af4bd3d 100755 --- a/scripts/deploy-pr.sh +++ b/scripts/deploy-pr.sh @@ -61,8 +61,7 @@ if $dryRun; then echo "branchName: ${branchName}" echo "prNumber: ${prNumber}" echo "skipBuild: ${skipBuild}" - echo "gh workflow run pr-deploy.yaml --ref ${branchName} -f pr_number=${prNumber} -f skip_build=${skipBuild}" exit 0 fi -gh workflow run pr-deploy.yaml --ref ${branchName} -f pr_number=${prNumber} -f skip_build=${skipBuild} +gh workflow run pr-deploy.yaml --ref "${branchName}" -f "pr_number=${prNumber}" -f "skip_build=${skipBuild}"