|
| 1 | +# This action will trigger when a PR is commentted containing /review-pr by a member of the org. |
| 2 | +name: Deploy PR |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + pr_number: |
| 8 | + description: "PR number" |
| 9 | + required: true |
| 10 | + |
| 11 | +env: |
| 12 | + REPO: ghcr.io/coder/coder-preview |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + packages: write |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | +concurrency: |
| 20 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 21 | + cancel-in-progress: true |
| 22 | + |
| 23 | +jobs: |
| 24 | + pr_commented: |
| 25 | + if: github.event_name == 'issue_comment' && contains(github.event.comment.body, '/deploy-pr') && github.event.comment.author_association == 'MEMBER' || github.event_name == 'workflow_dispatch' |
| 26 | + outputs: |
| 27 | + PR_NUMBER: ${{ steps.pr_number.outputs.PR_NUMBER }} |
| 28 | + PR_TITLE: ${{ steps.pr_number.outputs.PR_TITLE }} |
| 29 | + PR_URL: ${{ steps.pr_number.outputs.PR_URL }} |
| 30 | + COMMENT_ID: ${{ steps.comment_id.outputs.comment-id }} |
| 31 | + CODER_BASE_IMAGE_TAG: ${{ steps.set_tags.outputs.CODER_BASE_IMAGE_TAG }} |
| 32 | + CODER_IMAGE_TAG: ${{ steps.set_tags.outputs.CODER_IMAGE_TAG }} |
| 33 | + |
| 34 | + runs-on: "ubuntu-latest" |
| 35 | + steps: |
| 36 | + - name: Get PR number and title |
| 37 | + id: pr_number |
| 38 | + run: | |
| 39 | + set -euxo pipefail |
| 40 | + if [[ ${{ github.event_name }} == "workflow_dispatch" ]]; then |
| 41 | + PR_NUMBER=${{ github.event.inputs.pr_number }} |
| 42 | + PR_TITLE=$(curl -sSL -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/coder/coder/pulls/$PR_NUMBER" | jq -r '.title') |
| 43 | + else |
| 44 | + PR_NUMBER=${{ github.event.issue.number }} |
| 45 | + PR_TITLE='${{ github.event.issue.title }}' |
| 46 | + fi |
| 47 | + echo "PR_URL=https://github.com/coder/coder/pull/$PR_NUMBER" >> $GITHUB_OUTPUT |
| 48 | + echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 49 | + echo "PR_TITLE=$PR_TITLE" >> $GITHUB_OUTPUT |
| 50 | +
|
| 51 | + - name: Set required tags |
| 52 | + id: set_tags |
| 53 | + run: | |
| 54 | + set -euxo pipefail |
| 55 | + echo "CODER_BASE_IMAGE_TAG=$CODER_BASE_IMAGE_TAG" >> $GITHUB_OUTPUT |
| 56 | + echo "CODER_IMAGE_TAG=$CODER_IMAGE_TAG" >> $GITHUB_OUTPUT |
| 57 | + env: |
| 58 | + CODER_BASE_IMAGE_TAG: ghcr.io/coder/coder-preview-base:pr${{ steps.pr_number.outputs.PR_NUMBER }} |
| 59 | + CODER_IMAGE_TAG: ghcr.io/coder/coder-preview:pr${{ steps.pr_number.outputs.PR_NUMBER }} |
| 60 | + |
| 61 | + - name: Find Comment |
| 62 | + uses: peter-evans/find-comment@v2 |
| 63 | + id: fc |
| 64 | + with: |
| 65 | + issue-number: ${{ steps.pr_number.outputs.PR_NUMBER }} |
| 66 | + comment-author: "github-actions[bot]" |
| 67 | + body-includes: This deployment will be deleted when the PR is closed |
| 68 | + |
| 69 | + - name: Comment on PR |
| 70 | + id: comment_id |
| 71 | + uses: peter-evans/create-or-update-comment@v3 |
| 72 | + with: |
| 73 | + comment-id: ${{ steps.fc.outputs.comment-id }} |
| 74 | + issue-number: ${{ steps.pr_number.outputs.PR_NUMBER }} |
| 75 | + edit-mode: replace |
| 76 | + body: | |
| 77 | + :rocket: Deploying PR ${{ steps.pr_number.outputs.PR_NUMBER }} ... |
| 78 | + :warning: This deployment will be deleted when the PR is closed. |
| 79 | +
|
| 80 | + build: |
| 81 | + needs: pr_commented |
| 82 | + runs-on: ${{ github.repository_owner == 'coder' && 'buildjet-8vcpu-ubuntu-2204' || 'ubuntu-latest' }} |
| 83 | + env: |
| 84 | + DOCKER_CLI_EXPERIMENTAL: "enabled" |
| 85 | + CODER_IMAGE_TAG: ${{ needs.pr_commented.outputs.coder_image_tag }} |
| 86 | + PR_NUMBER: ${{ needs.pr_commented.outputs.pr_number }} |
| 87 | + steps: |
| 88 | + - uses: actions/checkout@v3 |
| 89 | + with: |
| 90 | + fetch-depth: 0 |
| 91 | + |
| 92 | + - uses: ./.github/actions/setup-go |
| 93 | + |
| 94 | + - uses: ./.github/actions/setup-node |
| 95 | + |
| 96 | + - name: Install sqlc |
| 97 | + run: | |
| 98 | + curl -sSL https://github.com/kyleconroy/sqlc/releases/download/v1.18.0/sqlc_1.18.0_linux_amd64.tar.gz | sudo tar -C /usr/bin -xz sqlc |
| 99 | +
|
| 100 | + - name: GHCR Login |
| 101 | + uses: docker/login-action@v2 |
| 102 | + with: |
| 103 | + registry: ghcr.io |
| 104 | + username: ${{ github.actor }} |
| 105 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 106 | + |
| 107 | + - name: Build and push Linux amd64 Docker image |
| 108 | + run: | |
| 109 | + set -euxo pipefail |
| 110 | + go mod download |
| 111 | + make gen/mark-fresh |
| 112 | + export DOCKER_IMAGE_NO_PREREQUISITES=true |
| 113 | + version="$(./scripts/version.sh)" |
| 114 | + export CODER_IMAGE_BUILD_BASE_TAG="$(CODER_IMAGE_BASE=coder-base ./scripts/image_tag.sh --version "$version")" |
| 115 | + make -j build/coder_linux_amd64 |
| 116 | + ./scripts/build_docker.sh \ |
| 117 | + --arch amd64 \ |
| 118 | + --target ${{ env.CODER_IMAGE_TAG }} \ |
| 119 | + --version $version \ |
| 120 | + --push \ |
| 121 | + build/coder_linux_amd64 |
| 122 | +
|
| 123 | + deploy: |
| 124 | + needs: [build, pr_commented] |
| 125 | + if: needs.build.result == 'success' |
| 126 | + runs-on: "ubuntu-latest" |
| 127 | + env: |
| 128 | + CODER_IMAGE_TAG: ${{ needs.pr_commented.outputs.CODER_IMAGE_TAG }} |
| 129 | + PR_NUMBER: ${{ needs.pr_commented.outputs.PR_NUMBER }} |
| 130 | + PR_TITLE: ${{ needs.pr_commented.outputs.PR_TITLE }} |
| 131 | + PR_URL: ${{ needs.pr_commented.outputs.PR_URL }} |
| 132 | + steps: |
| 133 | + - uses: actions/checkout@v3 |
| 134 | + |
| 135 | + - name: "Set up kubeconfig" |
| 136 | + run: | |
| 137 | + set -euxo pipefail |
| 138 | + mkdir -p ~/.kube |
| 139 | + echo "${{ secrets.DELIVERYBOT_KUBECONFIG }}" > ~/.kube/config |
| 140 | + export KUBECONFIG=~/.kube/config |
| 141 | +
|
| 142 | + - name: "Create PR namespace" |
| 143 | + run: | |
| 144 | + set -euxo pipefail |
| 145 | + # try to delete the namespace, but don't fail if it doesn't exist |
| 146 | + kubectl delete namespace "pr${{ env.PR_NUMBER }}" || true |
| 147 | + kubectl create namespace "pr${{ env.PR_NUMBER }}" |
| 148 | +
|
| 149 | + - name: "Install Helm chart" |
| 150 | + run: | |
| 151 | + helm upgrade --install pr${{ env.PR_NUMBER }} ./helm \ |
| 152 | + --namespace "pr${{ env.PR_NUMBER }}" \ |
| 153 | + --set coder.image.repo=${{ env.REPO }} \ |
| 154 | + --set coder.image.tag=pr${{ env.PR_NUMBER }} \ |
| 155 | + --set coder.service.type=ClusterIP \ |
| 156 | + --set coder.env[0].name=CODER_ACCESS_URL \ |
| 157 | + --set coder.env[0].value="" \ |
| 158 | + --force |
| 159 | +
|
| 160 | + - name: "Get deployment URL" |
| 161 | + id: deployment_url |
| 162 | + run: | |
| 163 | + set -euo pipefail |
| 164 | + kubectl rollout status deployment/coder --namespace "pr${{ env.PR_NUMBER }}" |
| 165 | + POD_NAME=$(kubectl get pods -n "pr${{ env.PR_NUMBER }}" | awk 'NR==2{print $1}') |
| 166 | + CODER_ACCESS_URL=$(kubectl logs $POD_NAME -n "pr${{ env.PR_NUMBER }}" | grep "Web UI:" | awk -F ':' '{print $2":"$3}' | awk '{$1=$1};1') |
| 167 | + echo "::add-mask::$CODER_ACCESS_URL" |
| 168 | + echo "CODER_ACCESS_URL=$CODER_ACCESS_URL" >> $GITHUB_OUTPUT |
| 169 | +
|
| 170 | + - name: Send Slack notification |
| 171 | + run: | |
| 172 | + curl -s -o /dev/null -X POST -H 'Content-type: application/json' \ |
| 173 | + -d '{ |
| 174 | + "pr_number": "'"${{ env.PR_NUMBER }}"'", |
| 175 | + "pr_url": "'"${{ env.PR_URL }}"'", |
| 176 | + "pr_title": "'"${{ env.PR_TITLE }}"'", |
| 177 | + "pr_access_url": "'"${{ steps.deployment_url.outputs.CODER_ACCESS_URL }}"'" }' ${{ secrets.PR_DEPLOYMENTS_SLACK_WEBHOOK }} |
| 178 | + echo "Slack notification sent" |
| 179 | +
|
| 180 | + - name: Comment on PR |
| 181 | + uses: peter-evans/create-or-update-comment@v3 |
| 182 | + with: |
| 183 | + issue-number: ${{ env.PR_NUMBER }} |
| 184 | + edit-mode: replace |
| 185 | + comment-id: ${{ needs.pr_commented.outputs.COMMENT_ID }} |
| 186 | + body: | |
| 187 | + :heavy_check_mark: Deployed PR ${{ env.PR_NUMBER }} successfully. |
| 188 | + :rocket: Access the deployment link [here](https://codercom.slack.com/archives/C05DNE982E8). |
| 189 | + :warning: This deployment will be deleted when the PR is closed. |
| 190 | + reactions: "+1" |
0 commit comments