From 28828963f61708238d494a7b7f8e9e79dd74a489 Mon Sep 17 00:00:00 2001 From: Alok Gupta Date: Mon, 29 Jul 2024 17:36:57 +0530 Subject: [PATCH] [ github-actions ] : Add workflow to get preview from fork repository and deploy to cf pages (#785) * github-actions : Add workflow to get preview from fork repository * github-actions : Add workflow to deploy to production * removed id from the deployment steps * Update preview-deployment.yml secrets names --------- Co-authored-by: Benjamin Granados <40007659+benjagm@users.noreply.github.com> --- .github/workflows/preview-deployment.yml | 84 +++++++++++++++++++++ .github/workflows/production-deployment.yml | 64 ++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 .github/workflows/preview-deployment.yml create mode 100644 .github/workflows/production-deployment.yml diff --git a/.github/workflows/preview-deployment.yml b/.github/workflows/preview-deployment.yml new file mode 100644 index 000000000..a8eb57236 --- /dev/null +++ b/.github/workflows/preview-deployment.yml @@ -0,0 +1,84 @@ +#################### 🚧 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: Preview Deployment +on: + pull_request_target: + +# 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: + permissions: + contents: read + pull-requests: write + deployments: write + + runs-on: ubuntu-latest + name: Deploy Preview to Cloudflare Pages + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + submodules: 'recursive' + + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT + + - name: Cache Node dependencies + uses: actions/cache@v4 + id: yarn-cache + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- + + - name: Cache Next Build + uses: actions/cache@v4 + with: + path: | + ${{ steps.yarn-cache-dir-path.outputs.dir }} + ${{ github.workspace }}/.next/cache + key: ${{ runner.os }}-nextjs-${{ hashFiles('**/yarn.lock') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx') }} + restore-keys: | + ${{ runner.os }}-nextjs-${{ hashFiles('**/yarn.lock') }}- + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install dependencies + run: yarn install --frozen-lockfile + + - name: Build Site + run: yarn run build + + - name: Deploy to Cloudflare Pages + uses: AdrianGonz97/refined-cf-pages-action@v1 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + githubToken: ${{ secrets.GITHUB_TOKEN }} + projectName: ${{ vars.CF_PROJECT_NAME }} + directory: out + deploymentName: Preview diff --git a/.github/workflows/production-deployment.yml b/.github/workflows/production-deployment.yml new file mode 100644 index 000000000..b659b6231 --- /dev/null +++ b/.github/workflows/production-deployment.yml @@ -0,0 +1,64 @@ +name: Docs Production Deployment +on: + push: + branches: + - main + +concurrency: ${{ github.workflow }}-${{ github.ref }} + +jobs: + publish: + runs-on: ubuntu-latest + permissions: + contents: read + deployments: write + name: Publish to Cloudflare Pages + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + submodules: 'recursive' + + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT + + - name: Cache Node dependencies + uses: actions/cache@v4 + id: yarn-cache + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- + + - name: Cache Next Build + uses: actions/cache@v4 + with: + path: | + ${{ steps.yarn-cache-dir-path.outputs.dir }} + ${{ github.workspace }}/.next/cache + key: ${{ runner.os }}-nextjs-${{ hashFiles('**/yarn.lock') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx') }} + restore-keys: | + ${{ runner.os }}-nextjs-${{ hashFiles('**/yarn.lock') }}- + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install dependencies + run: yarn install --frozen-lockfile + + - name: Build Site + run: yarn run build + + - name: Deploy to Cloudflare Pages + uses: AdrianGonz97/refined-cf-pages-action@v1 + with: + apiToken: ${{ secrets.CF_API_TOKEN }} + accountId: ${{ secrets.CF_ACCOUNT_ID }} + githubToken: ${{ secrets.GITHUB_TOKEN }} + projectName: ${{ vars.CF_PROJECT_NAME }} + directory: out + deploymentName: Production \ No newline at end of file