From 27515f283c2e3aeb1e99e96c942d039dd5edd474 Mon Sep 17 00:00:00 2001 From: mickael Date: Thu, 17 Jul 2025 13:25:57 +0200 Subject: [PATCH] ci: comment preview URLs for PR deploy/undeploy events (+lint) --- .github/workflows/deploy-pr.yml | 50 ++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/.github/workflows/deploy-pr.yml b/.github/workflows/deploy-pr.yml index d1baa950..7deb0333 100644 --- a/.github/workflows/deploy-pr.yml +++ b/.github/workflows/deploy-pr.yml @@ -30,6 +30,7 @@ jobs: private-key: '${{ secrets.CR_APP_CI_PRIVATE_KEY }}' owner: '${{ env.FLUX_OWNER }}' repositories: '${{ env.FLUX_REPO }}' + - name: Checkout Flux repo uses: actions/checkout@v4 with: @@ -37,10 +38,13 @@ jobs: token: '${{ steps.app-token.outputs.token }}' path: flux-repo persist-credentials: false + - name: Export PR ID - run: 'echo "PR_ID=${{ github.event.pull_request.number }}" >> $GITHUB_ENV' shell: bash + run: 'echo "PR_ID=${{ github.event.pull_request.number }}" >> $GITHUB_ENV' + - name: Ensure template exists + shell: bash run: > T="flux-repo/pr-template/${APP_NAME}" @@ -48,8 +52,9 @@ jobs: [[ $(find "$T" -type f | wc -l) -gt 0 ]] || { echo "No files in $T"; exit 1; } - shell: bash + - name: Render & copy template + shell: bash run: | SRC="flux-repo/pr-template/${APP_NAME}" DST="flux-repo/deployments/prs-${APP_NAME}/${PR_ID}" @@ -57,8 +62,9 @@ jobs: cp -r "$SRC/." "$DST/" find "$DST" -type f -print0 \ | xargs -0 -n1 sed -i "s|@PR-ID@|${PR_ID}|g" - shell: bash + - name: Sanity‑check rendered output + shell: bash run: > E=$(find "flux-repo/pr-template/${APP_NAME}" -type f | wc -l) @@ -66,8 +72,9 @@ jobs: -l) (( G == E )) || { echo "Expected $E files, got $G"; exit 1; } - shell: bash + - name: Commit & push creation + shell: bash run: > cd flux-repo @@ -84,13 +91,22 @@ jobs: git remote set-url origin \ https://x-access-token:${{ steps.app-token.outputs.token }}@github.com/${{ env.FLUX_OWNER }}/${{ env.FLUX_REPO }}.git git push origin HEAD:main - shell: bash + + - name: Comment preview URL on PR + uses: thollander/actions-comment-pull-request@v3 + with: + github-token: '${{ secrets.GITHUB_TOKEN }}' + pr-number: '${{ github.event.pull_request.number }}' + comment-tag: 'pr-preview' + create-if-not-exists: 'true' + message: | + ⚙️ Preview environment for PR #${{ env.PR_ID }} is available at: + https://pr-${{ env.PR_ID }}.${{ env.APP_NAME }}.coderamp.dev/ remove-pr-env: if: >- (github.event.action == 'unlabeled' && github.event.label.name == - 'deploy-pr-temp-env') || (github.event.action == 'closed' && - github.event.pull_request.merged == true) + 'deploy-pr-temp-env') || (github.event.action == 'closed') runs-on: ubuntu-latest steps: - name: Create GitHub App token @@ -101,6 +117,7 @@ jobs: private-key: '${{ secrets.CR_APP_CI_PRIVATE_KEY }}' owner: '${{ env.FLUX_OWNER }}' repositories: '${{ env.FLUX_REPO }}' + - name: Checkout Flux repo uses: actions/checkout@v4 with: @@ -108,10 +125,13 @@ jobs: token: '${{ steps.app-token.outputs.token }}' path: flux-repo persist-credentials: false + - name: Export PR ID - run: 'echo "PR_ID=${{ github.event.pull_request.number }}" >> $GITHUB_ENV' shell: bash + run: 'echo "PR_ID=${{ github.event.pull_request.number }}" >> $GITHUB_ENV' + - name: Remove deployed directory + shell: bash run: | DST="flux-repo/deployments/prs-${APP_NAME}/${PR_ID}" if [[ -d "$DST" ]]; then @@ -120,8 +140,9 @@ jobs: else echo "⏭️ Nothing to delete at $DST" fi - shell: bash + - name: Commit & push deletion + shell: bash run: > cd flux-repo @@ -138,4 +159,13 @@ jobs: git remote set-url origin \ https://x-access-token:${{ steps.app-token.outputs.token }}@github.com/${{ env.FLUX_OWNER }}/${{ env.FLUX_REPO }}.git git push origin HEAD:main - shell: bash + + - name: Comment preview URL on PR + uses: thollander/actions-comment-pull-request@v3 + with: + github-token: '${{ secrets.GITHUB_TOKEN }}' + pr-number: '${{ github.event.pull_request.number }}' + comment-tag: 'pr-preview' + create-if-not-exists: 'true' + message: | + ⚙️ Preview environment was undeployed.