From 7ec29c2eeaa29fec99a55f9005341f745848e0e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=AB=98=E7=8C=9B?= Date: Wed, 24 Jan 2024 20:19:39 +0800 Subject: [PATCH 1/8] add workflows/deploy preview --- .github/workflows/deploy_preview.yml | 82 +++++++++++++++++++++ .github/workflows/remove_deploy_preview.yml | 40 ++++++++++ 2 files changed, 122 insertions(+) create mode 100644 .github/workflows/deploy_preview.yml create mode 100644 .github/workflows/remove_deploy_preview.yml diff --git a/.github/workflows/deploy_preview.yml b/.github/workflows/deploy_preview.yml new file mode 100644 index 00000000000..7bc245ddc3e --- /dev/null +++ b/.github/workflows/deploy_preview.yml @@ -0,0 +1,82 @@ +name: VercelPreviewDeployment + +env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + VERCEL_DOMAIN_SUFFIX: ".pr.nextchat.dev" + +permissions: + contents: read + statuses: write + pull-requests: write + +on: + pull_request: + types: + - opened + - synchronize + +jobs: + deploy-preview: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Extract branch name + shell: bash + run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> "$GITHUB_OUTPUT" + id: extract_branch + + - name: Hash branch name + uses: pplanel/hash-calculator-action@v1.3.1 + id: hash_branch + with: + input: ${{ steps.extract_branch.outputs.branch }} + method: MD5 + + - name: Set Environment Variables + id: set_env + if: github.event_name == 'pull_request' + run: | + echo "VERCEL_ALIAS_DOMAIN=${{ github.event.pull_request.number }}-${{ github.workflow }}.${VERCEL_DOMAIN_SUFFIX}" >> $GITHUB_OUTPUT + + - name: Install Vercel CLI + run: npm install --global vercel@latest + + - name: Cache dependencies + uses: actions/cache@v2 + id: cache-npm + with: + path: ~/.npm + key: npm-${{ hashFiles('package-lock.json') }} + restore-keys: npm- + + - name: Pull Vercel Environment Information + run: vercel pull --yes --environment=preview --token=${VERCEL_TOKEN} + + - name: Deploy Project Artifacts to Vercel + id: vercel + env: + META_TAG: ${{ steps.hash_branch.outputs.digest }}-${{ github.run_number }}-${{ github.run_attempt}} + run: | + env + vercel pull --yes --environment=preview --token=${VERCEL_TOKEN} + vercel build --token=${VERCEL_TOKEN} + vercel deploy --prebuilt --archive=tgz --token=${VERCEL_TOKEN} --meta base_hash=${{ env.META_TAG }} + + vercel ls --token=${VERCEL_TOKEN} --meta base_hash=${{ env.META_TAG }} &> vercel-output + DEFAULT_URL=$(cat vercel-output | grep http | awk '{print $2}') + ALIAS_URL=$(vercel alias set ${DEFAULT_URL} ${{ steps.set_env.outputs.VERCEL_ALIAS_DOMAIN }} --token=${VERCEL_TOKEN} | awk '{print $3}') + + echo "New preview URL: ${DEFAULT_URL}" + echo "New alias URL: ${ALIAS_URL}" + echo "META_TAG=${META_TAG}" + echo "VERCEL_URL=${ALIAS_URL}" >> "$GITHUB_OUTPUT" + + - uses: mshick/add-pr-comment@v2 + with: + message: | + Your build has completed! + + [Preview deployment](${{ steps.vercel.outputs.VERCEL_URL }}) diff --git a/.github/workflows/remove_deploy_preview.yml b/.github/workflows/remove_deploy_preview.yml new file mode 100644 index 00000000000..924b8dd0746 --- /dev/null +++ b/.github/workflows/remove_deploy_preview.yml @@ -0,0 +1,40 @@ +name: Removedeploypreview + +permissions: + contents: read + statuses: write + pull-requests: write + +env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + +on: + pull_request: + types: + - closed + +jobs: + delete-deployments: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Extract branch name + shell: bash + run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT + id: extract_branch + + - name: Hash branch name + uses: pplanel/hash-calculator-action@v1.3.1 + id: hash_branch + with: + input: ${{ steps.extract_branch.outputs.branch }} + method: MD5 + + - name: Call the delete-deployment-preview.sh script + env: + META_TAG: ${{ steps.hash_branch.outputs.digest }} + run: | + bash ./scripts/delete-deployment-preview.sh From bdc25adb413ffbdbf9a0fa0a944b6778dbe4a0fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=AB=98=E7=8C=9B?= Date: Wed, 24 Jan 2024 20:49:30 +0800 Subject: [PATCH 2/8] add delete preview scripts --- scripts/delete-deployment-preview.sh | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 scripts/delete-deployment-preview.sh diff --git a/scripts/delete-deployment-preview.sh b/scripts/delete-deployment-preview.sh new file mode 100755 index 00000000000..fbad29a5d29 --- /dev/null +++ b/scripts/delete-deployment-preview.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# Set the pipefail option. +set -o pipefail + +# Get the Vercel API endpoints. +GET_DEPLOYMENTS_ENDPOINT="https://api.vercel.com/v6/deployments" +DELETE_DEPLOYMENTS_ENDPOINT="https://api.vercel.com/v13/deployments" + +# Create a list of deployments. +# deployments=$(curl -s -X GET "$GET_DEPLOYMENTS_ENDPOINT/?projectId=$VERCEL_PROJECT_ID&teamId=$VERCEL_ORG_ID" -H "Authorization: Bearer $VERCEL_TOKEN ") +deployments=$(curl -s -X GET "$GET_DEPLOYMENTS_ENDPOINT/?projectId=$VERCEL_PROJECT_ID" -H "Authorization: Bearer $VERCEL_TOKEN ") + +# Filter the deployments list by meta.base_hash === meta tag. +filtered_deployments=$(echo $deployments | jq --arg META_TAG "$META_TAG" '[.deployments[] | select(.meta.base_hash | type == "string" and contains($META_TAG)) | .uid] | join(",")') +filtered_deployments="${filtered_deployments//\"/}" # Remove double quotes + +# Clears the values from filtered_deployments +IFS=',' read -ra values <<<"$filtered_deployments" + +echo "META_TAG ${META_TAG}" +echo "Filtered deployments ${filtered_deployments}" + +# Iterate over the filtered deployments list. +for uid in "${values[@]}"; do + echo "Deleting ${uid}" + + delete_url=${DELETE_DEPLOYMENTS_ENDPOINT}/${uid} + echo $delete_url + + # Make DELETE a request to the /v13/deployments/{id} endpoint. + curl -X DELETE $delete_url -H "Authorization: Bearer $VERCEL_TOKEN" + + echo "Deleted!" +done From 3bc771a0d9affad24dff4b2babb5f84f90e7a66a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=AB=98=E7=8C=9B?= Date: Wed, 24 Jan 2024 22:03:47 +0800 Subject: [PATCH 3/8] add workflow_dispatch --- .github/workflows/deploy_preview.yml | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/.github/workflows/deploy_preview.yml b/.github/workflows/deploy_preview.yml index 7bc245ddc3e..98d81bff9f3 100644 --- a/.github/workflows/deploy_preview.yml +++ b/.github/workflows/deploy_preview.yml @@ -1,5 +1,12 @@ name: VercelPreviewDeployment +on: + workflow_dispatch: + inputs: + branchName: + description: 'Branch to deploy' + required: true + env: VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} @@ -11,17 +18,13 @@ permissions: statuses: write pull-requests: write -on: - pull_request: - types: - - opened - - synchronize - jobs: deploy-preview: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + with: + ref: ${{ github.event.inputs.branchName }} - name: Extract branch name shell: bash From 998c95da51eca0c629d3ca6bbb11b1ee1ddaa786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=AB=98=E7=8C=9B?= Date: Wed, 24 Jan 2024 22:10:02 +0800 Subject: [PATCH 4/8] test pr deploy --- .github/workflows/deploy_preview.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy_preview.yml b/.github/workflows/deploy_preview.yml index 98d81bff9f3..cd0820775ac 100644 --- a/.github/workflows/deploy_preview.yml +++ b/.github/workflows/deploy_preview.yml @@ -4,7 +4,7 @@ on: workflow_dispatch: inputs: branchName: - description: 'Branch to deploy' + description: 'Branch to deploy .' required: true env: From 2f11e7c08764855ffdddc3aa0b8f85ad99e904af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=AB=98=E7=8C=9B?= Date: Thu, 25 Jan 2024 11:04:32 +0800 Subject: [PATCH 5/8] test pr deploy --- .github/workflows/deploy_preview.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy_preview.yml b/.github/workflows/deploy_preview.yml index cd0820775ac..98d81bff9f3 100644 --- a/.github/workflows/deploy_preview.yml +++ b/.github/workflows/deploy_preview.yml @@ -4,7 +4,7 @@ on: workflow_dispatch: inputs: branchName: - description: 'Branch to deploy .' + description: 'Branch to deploy' required: true env: From 94f534f36f8eee1ae336aa4764e00f02ae4d94b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=AB=98=E7=8C=9B?= Date: Thu, 25 Jan 2024 14:33:38 +0800 Subject: [PATCH 6/8] fix vercel alias domains --- .github/workflows/deploy_preview.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy_preview.yml b/.github/workflows/deploy_preview.yml index 98d81bff9f3..9079ddd4b5c 100644 --- a/.github/workflows/deploy_preview.yml +++ b/.github/workflows/deploy_preview.yml @@ -8,6 +8,7 @@ on: required: true env: + VERCEL_TEAM: ${{ secrets.VERCEL_TEAM }} VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} @@ -63,14 +64,13 @@ jobs: env: META_TAG: ${{ steps.hash_branch.outputs.digest }}-${{ github.run_number }}-${{ github.run_attempt}} run: | - env vercel pull --yes --environment=preview --token=${VERCEL_TOKEN} vercel build --token=${VERCEL_TOKEN} vercel deploy --prebuilt --archive=tgz --token=${VERCEL_TOKEN} --meta base_hash=${{ env.META_TAG }} vercel ls --token=${VERCEL_TOKEN} --meta base_hash=${{ env.META_TAG }} &> vercel-output DEFAULT_URL=$(cat vercel-output | grep http | awk '{print $2}') - ALIAS_URL=$(vercel alias set ${DEFAULT_URL} ${{ steps.set_env.outputs.VERCEL_ALIAS_DOMAIN }} --token=${VERCEL_TOKEN} | awk '{print $3}') + ALIAS_URL=$(vercel alias set ${DEFAULT_URL} ${{ steps.set_env.outputs.VERCEL_ALIAS_DOMAIN }} --token=${VERCEL_TOKEN} --scope ${VERCEL_TEAM}| awk '{print $3}') echo "New preview URL: ${DEFAULT_URL}" echo "New alias URL: ${ALIAS_URL}" From 6206ceb49b26b987adcde5e0715cc83adf500e21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=AB=98=E7=8C=9B?= Date: Thu, 25 Jan 2024 15:00:44 +0800 Subject: [PATCH 7/8] fix: cicd , add pull_request_target --- .github/workflows/deploy_preview.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deploy_preview.yml b/.github/workflows/deploy_preview.yml index 9079ddd4b5c..fc2969b7a1e 100644 --- a/.github/workflows/deploy_preview.yml +++ b/.github/workflows/deploy_preview.yml @@ -1,11 +1,11 @@ name: VercelPreviewDeployment on: + pull_request_target: + types: + - opened + - synchronize workflow_dispatch: - inputs: - branchName: - description: 'Branch to deploy' - required: true env: VERCEL_TEAM: ${{ secrets.VERCEL_TEAM }} From 43631a32718bfa287dfcb10e95087b0ef5a2d3ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=AB=98=E7=8C=9B?= Date: Thu, 25 Jan 2024 15:17:03 +0800 Subject: [PATCH 8/8] fix: cicd, vercel domain suffix secret --- .github/workflows/deploy_preview.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy_preview.yml b/.github/workflows/deploy_preview.yml index fc2969b7a1e..f392e3de996 100644 --- a/.github/workflows/deploy_preview.yml +++ b/.github/workflows/deploy_preview.yml @@ -12,7 +12,7 @@ env: VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} - VERCEL_DOMAIN_SUFFIX: ".pr.nextchat.dev" + VERCEL_PR_DOMAIN_SUFFIX: ${{ secrets.VERCEL_PR_DOMAIN_SUFFIX }} permissions: contents: read @@ -43,7 +43,7 @@ jobs: id: set_env if: github.event_name == 'pull_request' run: | - echo "VERCEL_ALIAS_DOMAIN=${{ github.event.pull_request.number }}-${{ github.workflow }}.${VERCEL_DOMAIN_SUFFIX}" >> $GITHUB_OUTPUT + echo "VERCEL_ALIAS_DOMAIN=${{ github.event.pull_request.number }}-${{ github.workflow }}.${VERCEL_PR_DOMAIN_SUFFIX}" >> $GITHUB_OUTPUT - name: Install Vercel CLI run: npm install --global vercel@latest