Deploy preview for PR #720
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy preview for PR | |
| on: | |
| workflow_run: | |
| workflows: | |
| - "Trigger preview for PR" | |
| types: | |
| - completed | |
| permissions: | |
| contents: read | |
| id-token: write | |
| pull-requests: write | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch }} | |
| cancel-in-progress: false | |
| env: | |
| PREVIEW_BASE: https://preview-developer.espressif.com/ | |
| jobs: | |
| deploy-preview: | |
| runs-on: ubuntu-latest | |
| if: > | |
| github.event.workflow_run.event == 'pull_request' && | |
| github.event.workflow_run.conclusion == 'success' | |
| steps: | |
| - name: Download PR number artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: pr-metadata | |
| path: ./ | |
| run-id: ${{ github.event.workflow_run.id }} | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Read PR number from file | |
| run: | | |
| PR_NUMBER="$(cat pr_num.txt)" | |
| PR_URL="$(cat pr_url.txt)" | |
| PR_REF="$(cat pr_head_ref.txt)" | |
| PR_SHA="$(cat pr_head_sha.txt)" | |
| echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV" | |
| echo "HUGO_BASEURL=${PREVIEW_BASE}pr${PR_NUMBER}/" >> "$GITHUB_ENV" | |
| echo "PR_URL=$PR_URL" >> "$GITHUB_ENV" | |
| echo "PR_LINK_TEXT=${PR_URL#https://}" >> "$GITHUB_ENV" | |
| echo "PR_REF=$PR_REF" >> "$GITHUB_ENV" | |
| echo "PR_SHA=$PR_SHA" >> "$GITHUB_ENV" | |
| - name: Check out PR commit with submodules | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ env.PR_SHA }} | |
| submodules: recursive | |
| fetch-depth: 0 | |
| - name: Install Hugo | |
| env: | |
| HUGO_VERSION: 0.152.2 | |
| run: | | |
| wget --progress=dot:giga -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_withdeploy_${HUGO_VERSION}_linux-amd64.deb \ | |
| && sudo dpkg -i ${{ runner.temp }}/hugo.deb | |
| - name: Rewrite and verify Hugo deployment URL for PR | |
| run: | | |
| # Rewrite the prefix in hugo.toml | |
| sed -i "s|prefix=subdir/|prefix=pr$PR_NUMBER/|g" config/_default/hugo.toml | |
| echo "The updated deployment URL for PR:" | |
| grep '\&prefix=' config/_default/hugo.toml | |
| - name: Add PR prefix to article links on main page | |
| run: | | |
| # Add pr$PR_NUMBER to all article links in index.md | |
| sed -i "s|\({{< article link=\"\)/|\1/pr$PR_NUMBER/|g" content/_index.md | |
| - name: Add preview banner to website | |
| run: | | |
| PREVIEW_HTML="<p style='color: rgba(var(--color-primary-600), 1); text-align: center;'> | |
| This site is a preview for <a href='$PR_URL' style='text-decoration: underline;'>$PR_LINK_TEXT</a> | |
| </p>" | |
| echo "$PREVIEW_HTML" >> layouts/partials/header/basic.html | |
| - name: Build website with Hugo | |
| env: | |
| # For maximum backward compatibility with Hugo modules | |
| HUGO_ENVIRONMENT: preview | |
| HUGO_ENV: preview | |
| run: | | |
| hugo \ | |
| --baseURL "$HUGO_BASEURL" \ | |
| --gc \ | |
| --minify | |
| - name: Deploy preview with Hugo | |
| env: | |
| # AWS_S3_BUCKET: ${{ secrets.PREVIEW_AWS_BUCKET_NAME }} | |
| AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| AWS_REGION: ${{ secrets.AWS_REGION }} | |
| run: | | |
| hugo deploy \ | |
| --target=preview | |
| - name: Post Preview Link to PR | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| try { | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| issue_number: process.env.PR_NUMBER, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo | |
| }); | |
| // Define the comment body | |
| const commentBody = `🎉 A preview for this PR is available at: ${process.env.HUGO_BASEURL}`; | |
| // Look for an existing comment containing the specific text | |
| const existingComment = comments.find(comment => | |
| comment.body.includes("🎉 A preview for this PR is available at:") | |
| ); | |
| if (existingComment) { | |
| await github.rest.issues.deleteComment({ | |
| comment_id: existingComment.id, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo | |
| }); | |
| } | |
| // Create a new comment | |
| await github.rest.issues.createComment({ | |
| issue_number: process.env.PR_NUMBER, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: commentBody | |
| }); | |
| } catch (error) { | |
| core.setFailed(`Failed to manage PR comment: ${error.message}`); | |
| } | |
| - name: Invalidate CloudFront cache for PR | |
| env: | |
| DISTRIBUTION_ID: ${{ secrets.PREVIEW_CLOUDFRONT_DISTRIBUTION }} | |
| AWS_REGION: ${{ secrets.AWS_REGION }} | |
| AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| run: | | |
| echo "Invalidating CloudFront paths for PR $PR_NUMBER..." | |
| aws cloudfront create-invalidation \ | |
| --distribution-id "$DISTRIBUTION_ID" \ | |
| --paths "/pr$PR_NUMBER/*" |