From b0821d37e6f4f95b16583109320aa4cc26f0459b Mon Sep 17 00:00:00 2001 From: ESP-Kirill <105578077+f-hollow@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:10:55 +0800 Subject: [PATCH 1/4] Feat/add workflow preview prs (#348) * fix: improve comments and name for deploy workflow * feat: clean up and improve config files * feat: add workflow to preview prs * feat: add workflow to remove preview files when pr closed --- ...oy-hugo-site.yml => deploy-production.yml} | 7 +- .github/workflows/pr-deploy-preview.yml | 89 +++++++++++++++++++ .github/workflows/pr-remove-preview.yml | 31 +++++++ config/_default/hugo.toml | 12 +-- config/production/hugo.toml | 13 +++ hugo.toml | 9 -- 6 files changed, 142 insertions(+), 19 deletions(-) rename .github/workflows/{deploy-hugo-site.yml => deploy-production.yml} (92%) create mode 100644 .github/workflows/pr-deploy-preview.yml create mode 100644 .github/workflows/pr-remove-preview.yml create mode 100644 config/production/hugo.toml delete mode 100644 hugo.toml diff --git a/.github/workflows/deploy-hugo-site.yml b/.github/workflows/deploy-production.yml similarity index 92% rename from .github/workflows/deploy-hugo-site.yml rename to .github/workflows/deploy-production.yml index 4269d9e9..ad160aa8 100644 --- a/.github/workflows/deploy-hugo-site.yml +++ b/.github/workflows/deploy-production.yml @@ -1,4 +1,4 @@ -name: Deploy Hugo site +name: Deploy production on: # Run on pushes to the default branch @@ -8,10 +8,9 @@ on: # Allow manual runs from the Actions tab workflow_dispatch: -# Set permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages +# Set permissions of the GITHUB_TOKEN permissions: contents: read - pages: write id-token: write # Allow only one concurrent deployment @@ -68,7 +67,7 @@ jobs: runs-on: ubuntu-latest steps: - - name: Invalidate cloudfront cache + - name: Invalidate CloudFront cache uses: chetan/invalidate-cloudfront-action@v2 env: PATHS: "/*" diff --git a/.github/workflows/pr-deploy-preview.yml b/.github/workflows/pr-deploy-preview.yml new file mode 100644 index 00000000..d3f9f550 --- /dev/null +++ b/.github/workflows/pr-deploy-preview.yml @@ -0,0 +1,89 @@ +name: Deploy preview for PR + +on: + pull_request: + types: [opened, synchronize, reopened] + # Allow manual runs from the Actions tab + workflow_dispatch: + +# Set permissions of the GITHUB_TOKEN +permissions: + contents: read + id-token: write + pull-requests: write + +# Allow only one concurrent deployment +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +# Define environment variables +env: + HUGO_BASEURL: "https://preview-developer.espressif.com/pr${{ github.event.pull_request.number }}/" + +# Default to bash +defaults: + run: + shell: bash + +jobs: + build-and-deploy-preview: + runs-on: ubuntu-latest + + steps: + + - name: Install Hugo CLI + env: + HUGO_VERSION: 0.135.0 + run: | + wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \ + && sudo dpkg -i ${{ runner.temp }}/hugo.deb + + - name: Check out repo + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Build website with Hugo + env: + # For maximum backward compatibility with Hugo modules + HUGO_ENVIRONMENT: preview + HUGO_ENV: preview + run: | + hugo \ + --gc \ + --minify + + - name: Deploy to AWS S3 PR-specific subdirectory + uses: jakejarvis/s3-sync-action@master + with: + args: --follow-symlinks --delete --cache-control no-cache + env: + AWS_S3_BUCKET: ${{ secrets.PREVIEW_AWS_BUCKET_NAME }} + SOURCE_DIR: './public' + # Subdirectory for the PR + DEST_DIR: "pr${{ github.event.pull_request.number }}" + AWS_REGION: ${{ secrets.AWS_REGION }} + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + + - name: Post Preview Link to PR + uses: actions/github-script@v7 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `🎉 A preview of this PR is available at: ${{ env.HUGO_BASEURL }} ` + }) + + - name: Invalidate CloudFront cache for PR + uses: chetan/invalidate-cloudfront-action@v2 + env: + PATHS: "/pr-${{ github.event.pull_request.number }}/*" + DISTRIBUTION: ${{ 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 }} diff --git a/.github/workflows/pr-remove-preview.yml b/.github/workflows/pr-remove-preview.yml new file mode 100644 index 00000000..2b4e2f21 --- /dev/null +++ b/.github/workflows/pr-remove-preview.yml @@ -0,0 +1,31 @@ +name: Remove preview for PR + +on: + pull_request: + types: + - closed + +jobs: + remove-preview: + name: Remove preview for PR + runs-on: ubuntu-latest + + steps: + # Remove the PR-specific folder from S3 + - name: Remove PR-specific subdirectory from S3 + run: | + echo "Cleaning up preview folder for PR #${{ github.event.pull_request.number }}" + aws s3 rm "s3://${{ secrets.PREVIEW_AWS_BUCKET_NAME }}/pr${{ github.event.pull_request.number }}" --recursive + env: + AWS_REGION: ${{ secrets.AWS_REGION }} + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + + - name: Invalidate CloudFront cache for PR + uses: chetan/invalidate-cloudfront-action@v2 + env: + PATHS: "/pr-${{ github.event.pull_request.number }}/*" + DISTRIBUTION: ${{ 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 }} diff --git a/config/_default/hugo.toml b/config/_default/hugo.toml index 862a7596..9698da82 100644 --- a/config/_default/hugo.toml +++ b/config/_default/hugo.toml @@ -2,9 +2,11 @@ # Refer to the theme docs for more details about each of these parameters. # https://blowfish.page/docs/getting-started/ -theme = "blowfish" -baseURL = "https://developer.espressif.com" +baseURL = "https://preview-developer.espressif.com/" +languageCode = "en" defaultContentLanguage = "en" +title = "Espressif Developer Portal Preview" +theme = "blowfish" # Keep English on top level defaultContentLanguageInSubdir = false @@ -14,10 +16,8 @@ defaultContentLanguageInSubdir = false enableRobotsTXT = true summaryLength = 0 -buildDrafts = false -buildFuture = false - -googleAnalytics = "G-K9NTMXPGN3" +buildDrafts = true +buildFuture = true [pagination] pagerSize = 30 diff --git a/config/production/hugo.toml b/config/production/hugo.toml new file mode 100644 index 00000000..66193aed --- /dev/null +++ b/config/production/hugo.toml @@ -0,0 +1,13 @@ +# -- Site Configuration -- +# Refer to the theme docs for more details about each of these parameters. +# https://blowfish.page/docs/getting-started/ + +baseURL = "https://developer.espressif.com/" +title = "Espressif Developer Portal" + +buildDrafts = false +buildFuture = false + +[services] + [services.googleAnalytics] + id = "G-K9NTMXPGN3" diff --git a/hugo.toml b/hugo.toml deleted file mode 100644 index 69a075c5..00000000 --- a/hugo.toml +++ /dev/null @@ -1,9 +0,0 @@ -baseURL = "https://developer.espressif.com/" -# [en, zh-cn, fr, ...] determines default content language -defaultContentLanguage = "en" -# language code -languageCode = "en" -title = "Espressif Developer Portal" - -# Change the default theme to be use when building the site with Hugo -theme = "blowfish" From f853ed1a5669a9d895a6b116ae021e5b67156ebb Mon Sep 17 00:00:00 2001 From: ESP-Kirill <105578077+f-hollow@users.noreply.github.com> Date: Tue, 10 Dec 2024 17:05:10 +0800 Subject: [PATCH 2/4] feat: add separate workflows to build and deploy preview pr (#359) --- .github/workflows/pr-build-preview.yml | 41 +++++++++++++++++ .github/workflows/pr-deploy-preview.yml | 61 ++++++++----------------- 2 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 .github/workflows/pr-build-preview.yml diff --git a/.github/workflows/pr-build-preview.yml b/.github/workflows/pr-build-preview.yml new file mode 100644 index 00000000..3869ce5a --- /dev/null +++ b/.github/workflows/pr-build-preview.yml @@ -0,0 +1,41 @@ +name: Build preview for PR + +on: + pull_request: + types: [opened, synchronize, reopened] + +permissions: + contents: read + +jobs: + build-preview: + runs-on: ubuntu-latest + + steps: + - name: Install Hugo CLI + env: + HUGO_VERSION: 0.135.0 + run: | + wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \ + && sudo dpkg -i ${{ runner.temp }}/hugo.deb + + - name: Check out repo + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Build website with Hugo + env: + # For maximum backward compatibility with Hugo modules + HUGO_ENVIRONMENT: preview + HUGO_ENV: preview + run: | + hugo \ + --gc \ + --minify + + - name: Upload artifacts + uses: actions/upload-artifact@v3 + with: + name: public-folder + path: ./public diff --git a/.github/workflows/pr-deploy-preview.yml b/.github/workflows/pr-deploy-preview.yml index d3f9f550..793e95e0 100644 --- a/.github/workflows/pr-deploy-preview.yml +++ b/.github/workflows/pr-deploy-preview.yml @@ -1,58 +1,36 @@ name: Deploy preview for PR on: - pull_request: - types: [opened, synchronize, reopened] - # Allow manual runs from the Actions tab - workflow_dispatch: + workflow_run: + workflows: ["Build preview for PR"] + types: + - completed -# Set permissions of the GITHUB_TOKEN permissions: contents: read id-token: write pull-requests: write -# Allow only one concurrent deployment concurrency: - group: ${{ github.workflow }}-${{ github.ref }} + group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch }} cancel-in-progress: false -# Define environment variables env: - HUGO_BASEURL: "https://preview-developer.espressif.com/pr${{ github.event.pull_request.number }}/" - -# Default to bash -defaults: - run: - shell: bash + HUGO_BASEURL: "https://preview-developer.espressif.com/pr${{ github.event.workflow_run.pull_requests[0].number }}/" jobs: - build-and-deploy-preview: + deploy-preview: runs-on: ubuntu-latest + if: > + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion == 'success' steps: - - - name: Install Hugo CLI - env: - HUGO_VERSION: 0.135.0 - run: | - wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \ - && sudo dpkg -i ${{ runner.temp }}/hugo.deb - - - name: Check out repo - uses: actions/checkout@v4 + - name: Download artifacts + uses: actions/download-artifact@v3 with: - submodules: recursive - - - name: Build website with Hugo - env: - # For maximum backward compatibility with Hugo modules - HUGO_ENVIRONMENT: preview - HUGO_ENV: preview - run: | - hugo \ - --gc \ - --minify + name: public-folder + path: ./public - name: Deploy to AWS S3 PR-specific subdirectory uses: jakejarvis/s3-sync-action@master @@ -61,8 +39,7 @@ jobs: env: AWS_S3_BUCKET: ${{ secrets.PREVIEW_AWS_BUCKET_NAME }} SOURCE_DIR: './public' - # Subdirectory for the PR - DEST_DIR: "pr${{ github.event.pull_request.number }}" + DEST_DIR: "pr${{ github.event.workflow_run.pull_requests[0].number }}" AWS_REGION: ${{ secrets.AWS_REGION }} AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} @@ -70,19 +47,19 @@ jobs: - name: Post Preview Link to PR uses: actions/github-script@v7 with: - github-token: ${{secrets.GITHUB_TOKEN}} + github-token: ${{ secrets.GITHUB_TOKEN }} script: | github.rest.issues.createComment({ - issue_number: context.issue.number, + issue_number: context.event.workflow_run.pull_requests[0].number, owner: context.repo.owner, repo: context.repo.repo, - body: `🎉 A preview of this PR is available at: ${{ env.HUGO_BASEURL }} ` + body: `🎉 A preview of this PR is available at: ${{ env.HUGO_BASEURL }}` }) - name: Invalidate CloudFront cache for PR uses: chetan/invalidate-cloudfront-action@v2 env: - PATHS: "/pr-${{ github.event.pull_request.number }}/*" + PATHS: "/pr-${{ github.event.workflow_run.pull_requests[0].number }}/*" DISTRIBUTION: ${{ secrets.PREVIEW_CLOUDFRONT_DISTRIBUTION }} AWS_REGION: ${{ secrets.AWS_REGION }} AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} From 760021afbae87b4364a4db0a5b0c0b7937128ee9 Mon Sep 17 00:00:00 2001 From: ESP-Kirill <105578077+f-hollow@users.noreply.github.com> Date: Wed, 11 Dec 2024 14:58:40 +0800 Subject: [PATCH 3/4] feat: add final fixes to preview workflows (#347) --- .github/workflows/pr-build-preview.yml | 10 +++++++++- .github/workflows/pr-deploy-preview.yml | 13 ++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.github/workflows/pr-build-preview.yml b/.github/workflows/pr-build-preview.yml index 3869ce5a..61c18bcd 100644 --- a/.github/workflows/pr-build-preview.yml +++ b/.github/workflows/pr-build-preview.yml @@ -7,6 +7,9 @@ on: permissions: contents: read +env: + HUGO_BASEURL: "https://preview-developer.espressif.com/pr${{ github.event.pull_request.number }}/" + jobs: build-preview: runs-on: ubuntu-latest @@ -31,11 +34,16 @@ jobs: HUGO_ENV: preview run: | hugo \ + --baseURL ${{ env.HUGO_BASEURL }} \ --gc \ --minify + - name: Print preview link to PR + run: | + echo "Preview link ${{ env.HUGO_BASEURL }}" + - name: Upload artifacts - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: public-folder path: ./public diff --git a/.github/workflows/pr-deploy-preview.yml b/.github/workflows/pr-deploy-preview.yml index 793e95e0..0a611667 100644 --- a/.github/workflows/pr-deploy-preview.yml +++ b/.github/workflows/pr-deploy-preview.yml @@ -2,7 +2,8 @@ name: Deploy preview for PR on: workflow_run: - workflows: ["Build preview for PR"] + workflows: + - "Build preview for PR" types: - completed @@ -27,10 +28,12 @@ jobs: steps: - name: Download artifacts - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: public-folder path: ./public + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ secrets.GITHUB_TOKEN }} - name: Deploy to AWS S3 PR-specific subdirectory uses: jakejarvis/s3-sync-action@master @@ -50,11 +53,11 @@ jobs: github-token: ${{ secrets.GITHUB_TOKEN }} script: | github.rest.issues.createComment({ - issue_number: context.event.workflow_run.pull_requests[0].number, + issue_number: ${{ github.event.workflow_run.pull_requests[0].number }}, owner: context.repo.owner, repo: context.repo.repo, - body: `🎉 A preview of this PR is available at: ${{ env.HUGO_BASEURL }}` - }) + body: `🎉 A preview for this PR is available at: ${{ env.HUGO_BASEURL }}` + }); - name: Invalidate CloudFront cache for PR uses: chetan/invalidate-cloudfront-action@v2 From eb32ae057633b8bc7f4e4e65999ecac81e31a390 Mon Sep 17 00:00:00 2001 From: ESP-Kirill <105578077+f-hollow@users.noreply.github.com> Date: Wed, 11 Dec 2024 20:01:57 +0800 Subject: [PATCH 4/4] ci(preview): pass PR number to deploy-preview via artifacts (#361) --- .github/workflows/pr-build-preview.yml | 13 +++++++++---- .github/workflows/pr-deploy-preview.yml | 26 +++++++++++++++++++------ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/.github/workflows/pr-build-preview.yml b/.github/workflows/pr-build-preview.yml index 61c18bcd..9c634227 100644 --- a/.github/workflows/pr-build-preview.yml +++ b/.github/workflows/pr-build-preview.yml @@ -38,12 +38,17 @@ jobs: --gc \ --minify - - name: Print preview link to PR - run: | - echo "Preview link ${{ env.HUGO_BASEURL }}" - - name: Upload artifacts uses: actions/upload-artifact@v4 with: name: public-folder path: ./public + + - name: Create PR number file + run: echo "${{ github.event.pull_request.number }}" > pr-num.txt + + - name: Upload PR number artifact + uses: actions/upload-artifact@v4 + with: + name: pr-num + path: pr-num.txt diff --git a/.github/workflows/pr-deploy-preview.yml b/.github/workflows/pr-deploy-preview.yml index 0a611667..34bd9a98 100644 --- a/.github/workflows/pr-deploy-preview.yml +++ b/.github/workflows/pr-deploy-preview.yml @@ -17,7 +17,7 @@ concurrency: cancel-in-progress: false env: - HUGO_BASEURL: "https://preview-developer.espressif.com/pr${{ github.event.workflow_run.pull_requests[0].number }}/" + HUGO_BASEURL: "https://preview-developer.espressif.com/" jobs: deploy-preview: @@ -27,7 +27,21 @@ jobs: github.event.workflow_run.conclusion == 'success' steps: - - name: Download artifacts + - name: Download artifacts (PR number file) + uses: actions/download-artifact@v4 + with: + name: pr-num + path: ./ + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Read PR number from file + id: read-pr-num + run: | + echo "PR_NUMBER=$(cat pr-num.txt)" >> $GITHUB_ENV + echo ${{ env.PR_NUMBER }} + + - name: Download artifacts (Public folder) uses: actions/download-artifact@v4 with: name: public-folder @@ -42,7 +56,7 @@ jobs: env: AWS_S3_BUCKET: ${{ secrets.PREVIEW_AWS_BUCKET_NAME }} SOURCE_DIR: './public' - DEST_DIR: "pr${{ github.event.workflow_run.pull_requests[0].number }}" + DEST_DIR: "pr${{ env.PR_NUMBER }}" AWS_REGION: ${{ secrets.AWS_REGION }} AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} @@ -53,16 +67,16 @@ jobs: github-token: ${{ secrets.GITHUB_TOKEN }} script: | github.rest.issues.createComment({ - issue_number: ${{ github.event.workflow_run.pull_requests[0].number }}, + issue_number: ${{ env.PR_NUMBER }}, owner: context.repo.owner, repo: context.repo.repo, - body: `🎉 A preview for this PR is available at: ${{ env.HUGO_BASEURL }}` + body: `🎉 A preview for this PR is available at: ${{ env.HUGO_BASEURL }}pr${{ env.PR_NUMBER }}/` }); - name: Invalidate CloudFront cache for PR uses: chetan/invalidate-cloudfront-action@v2 env: - PATHS: "/pr-${{ github.event.workflow_run.pull_requests[0].number }}/*" + PATHS: "/pr${{ env.PR_NUMBER }}/*" DISTRIBUTION: ${{ secrets.PREVIEW_CLOUDFRONT_DISTRIBUTION }} AWS_REGION: ${{ secrets.AWS_REGION }} AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}