From f45b6526bae4e2b5d304e15d16b7b4244c538e68 Mon Sep 17 00:00:00 2001 From: Sergio Ribera <56278796+SergioRibera@users.noreply.github.com> Date: Thu, 16 May 2024 22:33:08 -0400 Subject: [PATCH] ci: replace github pages by cloudflare pages --- .github/workflows/mdbook.yml | 18 ++--- .github/workflows/pr-preview.yml | 127 +++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/pr-preview.yml diff --git a/.github/workflows/mdbook.yml b/.github/workflows/mdbook.yml index e4f11363..2fdb8b3f 100644 --- a/.github/workflows/mdbook.yml +++ b/.github/workflows/mdbook.yml @@ -29,16 +29,13 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Install mdbook run: | mkdir bin curl -sSL https://github.com/RustLangES/mdBook/releases/download/v0.4.36-localization-v0.6/mdbook-v0.4.36-localization-v0.6-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin curl -sSL https://github.com/catppuccin/mdBook/releases/download/v0.1.1/mdbook-catppuccin-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin echo "$(pwd)/bin" >> ${GITHUB_PATH} - - name: Setup Pages - id: pages - uses: actions/configure-pages@v3 - name: Build with mdBook run: mdbook build - name: Upload artifact @@ -48,12 +45,13 @@ jobs: # Deployment job deploy: - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} runs-on: ubuntu-latest needs: build steps: - - name: Deploy to GitHub Pages - id: deployment - uses: actions/deploy-pages@v2 + - uses: actions/download-artifact@v4 + - name: Deploy + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: pages deploy ./artifact --project-name=rust-book diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml new file mode 100644 index 00000000..e56bb5a0 --- /dev/null +++ b/.github/workflows/pr-preview.yml @@ -0,0 +1,127 @@ +#################### 🚧 WARNING: READ THIS BEFORE USING THIS FILE 🚧 #################### +# +# +# +# IF YOU DON'T KNOW WHAT YOU'RE DOING, YOU CAN EASILY LEAK SECRETS BY USING A +# `pull_request_target` WORKFLOW INSTEAD OF `pull_request`! SERIOUSLY, DO NOT +# BLINDLY COPY AND PASTE THIS FILE WITHOUT UNDERSTANDING THE FULL IMPLICATIONS +# OF WHAT YOU'RE DOING! WE HAVE TESTED THIS FOR OUR OWN USE CASES, WHICH ARE +# NOT NECESSARILY THE SAME AS YOURS! WHILE WE AREN'T EXPOSING ANY OF OUR SECRETS, +# ONE COULD EASILY DO SO BY MODIFYING OR ADDING A STEP TO THIS WORKFLOW! +# +# +# +#################### 🚧 WARNING: READ THIS BEFORE USING THIS FILE 🚧 #################### + +name: Docs - Preview Deployment +on: + pull_request_target: + types: + - opened + - edited + - closed + +# cancel in-progress runs on new commits to same PR (github.event.number) +concurrency: + group: ${{ github.workflow }}-${{ github.event.number || github.sha }} + cancel-in-progress: true + +jobs: + deploy-preview: + if: ${{ github.event.action != 'closed' }} + permissions: + contents: read + pull-requests: write + deployments: write + runs-on: ubuntu-latest + name: Deploy Preview to Cloudflare Pages + env: + BRANCH_NAME: preview-${{ github.head_ref }} + ACTION_RUN: ${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}} + steps: + - uses: actions/checkout@v3 + with: + submodules: "recursive" + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + - name: Declare some variables + shell: bash + run: | + echo "SHA_SHORT=$(git rev-parse --short "$GITHUB_SHA")" >> "$GITHUB_ENV" + + - name: Create comment + id: comment + uses: peter-evans/create-or-update-comment@v4 + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body: | + ## ⚡ Cloudflare Pages Deployment + | Name | Status | Preview | + | :--- | :----- | :------ | + | ${{env.BRANCH_NAME}} | 🔨 Building ([Logs](${env.ACTION_RUN})) | waiting... | + + # Build Rust Page + - name: Install mdbook + run: | + mkdir bin + curl -sSL https://github.com/RustLangES/mdBook/releases/download/v0.4.36-localization-v0.6/mdbook-v0.4.36-localization-v0.6-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin + curl -sSL https://github.com/catppuccin/mdBook/releases/download/v0.1.1/mdbook-catppuccin-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin + echo "$(pwd)/bin" >> ${GITHUB_PATH} + - name: Build with mdBook + run: mdbook build + + # Deploy + - name: Deploy + id: deploy + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: pages deploy ./book --project-name=rust-book --branch="${ env.BRANCH_NAME }" + + - name: Create comment + uses: peter-evans/create-or-update-comment@v4 + with: + issue-number: ${{ github.event.pull_request.number }} + comment-id: ${{ steps.comment.outputs.comment-id }} + edit-mode: replace + body: | + ## ⚡ Cloudflare Pages Deployment + | Name | Status | Preview | + | :--- | :----- | :------ | + | ${{env.BRANCH_NAME}} | ✅ Ready ([Logs](${env.ACTION_RUN})) | [${{env.SHA_SHORT}}](${{ steps.deploy.outputs.deployment-url }}) | + + # remove-preview: + # if: ${{ github.event.action == "closed" }} + # permissions: + # contents: read + # pull-requests: write + # deployments: write + # runs-on: ubuntu-latest + # name: Remove Preview of Cloudflare Pages + # steps: + # - uses: actions/checkout@v3 + # with: + # submodules: "recursive" + # ref: ${{ github.event.pull_request.head.ref }} + # repository: ${{ github.event.pull_request.head.repo.full_name }} + + # - name: Deploy + # id: deploy + # uses: cloudflare/wrangler-action@v3 + # with: + # apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + # accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + # command: pages --project-name=homepage --branch="${ env.BRANCH_NAME }" + + # - name: Create comment + # uses: peter-evans/create-or-update-comment@v4 + # with: + # issue-number: ${{ github.event.pull_request.number }} + # comment-author: 'github-actions[bot]' + # body: | + # ## ⚡ Removing Cloudflare Pages Preview + # | Name | Status | + # | :--- | :----- | + # | ${{env.BRANCH_NAME}} | ✅ Removed |