From 21b926aabea17c4eeb61d75fcc21c0b3e83e2283 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Wed, 9 Feb 2022 15:48:41 +0200 Subject: [PATCH] ci: Extract branch publishing into separate workflow (#3488) --- .../actions/deploy-dir-as-branch/action.yml | 58 ----------------- .github/workflows/canary.yaml | 12 ++-- .github/workflows/cd.yml | 29 +++++++++ .github/workflows/ci.yml | 64 +++++-------------- .../workflows/deploy-artifact-as-branch.yml | 51 +++++++++++++++ 5 files changed, 102 insertions(+), 112 deletions(-) delete mode 100644 .github/actions/deploy-dir-as-branch/action.yml create mode 100644 .github/workflows/cd.yml create mode 100644 .github/workflows/deploy-artifact-as-branch.yml diff --git a/.github/actions/deploy-dir-as-branch/action.yml b/.github/actions/deploy-dir-as-branch/action.yml deleted file mode 100644 index d37086a4e9..0000000000 --- a/.github/actions/deploy-dir-as-branch/action.yml +++ /dev/null @@ -1,58 +0,0 @@ -name: 'Deploy specified directory as a branch' -description: 'This action deploys directory as branch.' -inputs: - src_dir: - required: true - target_branch: - required: true -runs: - using: 'composite' - steps: - - name: Creating temporary directory to clone the branch - shell: bash - run: | - BRANCH_DIR=$(mktemp -d "`pwd`/$BRANCH_DIR_TEMPLATE") - echo "BRANCH_DIR=$BRANCH_DIR" >> $GITHUB_ENV - env: - BRANCH_DIR_TEMPLATE: 'cloned_${{ inputs.target_branch }}_XXXXXX' - - - name: Checkout `${{ inputs.target_branch }}` branch - uses: actions/checkout@v2 - with: - ref: ${{ inputs.target_branch }} - path: ${{ env.BRANCH_DIR }} - - - name: Publish `${{ inputs.target_branch }}` branch - working-directory: ${{ env.BRANCH_DIR }} - shell: bash - run: | - echo '::echo::on' - - echo '::group::Remove existing files first' - git rm -r . - echo '::endgroup::' - - echo '::group::Move necessary files' - cp -vnR "$SRC_DIR/." . - echo '::endgroup::' - - git add -A - if git diff --staged --quiet; then - echo 'Nothing to publish' - else - git config user.name 'GitHub Action Script' - git config user.email 'please@open.issue' - - git commit -a -m "Deploy $SHA to '$TARGET_BRANCH' branch" - git push - echo 'Pushed' - fi - env: - SRC_DIR: '${{ github.workspace }}/${{ inputs.src_dir }}' - SHA: ${{ github.sha }} - TARGET_BRANCH: ${{ inputs.target_branch }} - - - name: Remove cloned branch - if: ${{ always() }} - shell: bash - run: 'rm -rf "$BRANCH_DIR"' diff --git a/.github/workflows/canary.yaml b/.github/workflows/canary.yaml index 79b1e8ef41..982c68ecf5 100644 --- a/.github/workflows/canary.yaml +++ b/.github/workflows/canary.yaml @@ -7,12 +7,11 @@ on: - completed env: NODE_VERSION_USED_FOR_DEVELOPMENT: 17 - CI_WORKFLOW_ID: ${{github.event.workflow_run.id}} jobs: publish-canary: runs-on: ubuntu-latest name: Publish Canary - if: ${{ github.event.workflow_run.event == 'pull_request' }} + if: github.event.workflow_run.event == 'pull_request' steps: - name: Checkout repo uses: actions/checkout@v2 @@ -27,18 +26,17 @@ jobs: # 'registry-url' is required for 'npm publish' registry-url: 'https://registry.npmjs.org' - - name: Install Dependencies - run: npm ci --ignore-scripts - - name: Download event.json - run: gh run download "$CI_WORKFLOW_ID" -n event.json + run: gh run download "$WORKFLOW_ID" -n event.json env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + WORKFLOW_ID: ${{github.event.workflow_run.id}} - name: Download NPM package artifact - run: gh run download "$CI_WORKFLOW_ID" -n npmDist -D npmDist + run: gh run download "$WORKFLOW_ID" -n npmDist -D npmDist env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + WORKFLOW_ID: ${{github.event.workflow_run.id}} - name: Modify NPM package to be canary release uses: actions/github-script@v5 diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000000..a58a771b80 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,29 @@ +name: CD +on: + workflow_run: + workflows: + - CI + types: + - completed + branches: + - main +jobs: + deploy-to-npm-branch: + name: Deploy to `npm` branch + if: github.event.workflow_run.event == 'push' + uses: ./.github/workflows/deploy-artifact-as-branch.yml + with: + workflow_id: ${{github.event.workflow_run.id}} + artifact_name: npmDist + target_branch: npm + commit_message: "Deploy ${{github.event.workflow_run.head_sha}} to 'npm' branch" + + deploy-to-deno-branch: + name: Deploy to `deno` branch + if: github.event.workflow_run.event == 'push' + uses: ./.github/workflows/deploy-artifact-as-branch.yml + with: + workflow_id: ${{github.event.workflow_run.id}} + artifact_name: denoDist + target_branch: deno + commit_message: "Deploy ${{github.event.workflow_run.head_sha}} to 'deno' branch" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ea94d75f57..8dac97235c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -246,8 +246,8 @@ jobs: path: ./npm-dist-diff.html if-no-files-found: ignore - build-npm-package: - name: Build artifact with NPM package + build-npm-dist: + name: Build 'npmDist' artifact runs-on: ubuntu-latest needs: [test, fuzz, lint, integrationTests] steps: @@ -268,52 +268,16 @@ jobs: - name: Build NPM package run: npm run build:npm - - name: Upload NPM package + - name: Upload npmDist package uses: actions/upload-artifact@v2 with: name: npmDist path: ./npmDist - deploy-to-npm-branch: - name: Deploy to `npm` branch + build-deno-dist: + name: Build 'denoDist' artifact runs-on: ubuntu-latest - if: | - github.event_name == 'push' && - github.repository == 'graphql/graphql-js' && - github.ref == 'refs/heads/main' - needs: [test, fuzz, lint, checkForCommonlyIgnoredFiles, integrationTests] - steps: - - name: Checkout repo - uses: actions/checkout@v2 - with: - persist-credentials: false - - - name: Setup Node.js - uses: actions/setup-node@v2 - with: - cache: npm - node-version: ${{ env.NODE_VERSION_USED_FOR_DEVELOPMENT }} - - - name: Install Dependencies - run: npm ci --ignore-scripts - - - name: Build NPM package - run: npm run build:npm - - - name: Deploy to `npm` branch - uses: ./.github/actions/deploy-dir-as-branch - with: - src_dir: npmDist - target_branch: npm - - deploy-to-deno-branch: - name: Deploy to `deno` branch - runs-on: ubuntu-latest - if: | - github.event_name == 'push' && - github.repository == 'graphql/graphql-js' && - github.ref == 'refs/heads/main' - needs: [test, fuzz, lint, checkForCommonlyIgnoredFiles, integrationTests] + needs: [test, fuzz, lint, integrationTests] steps: - name: Checkout repo uses: actions/checkout@v2 @@ -332,13 +296,13 @@ jobs: - name: Build Deno package run: npm run build:deno - - name: Deploy to `deno` branch - uses: ./.github/actions/deploy-dir-as-branch + - name: Upload denoDist package + uses: actions/upload-artifact@v2 with: - src_dir: denoDist - target_branch: deno + name: denoDist + path: ./denoDist - build-website: + build-website-dist: name: Build website runs-on: ubuntu-latest steps: @@ -358,3 +322,9 @@ jobs: - name: Build Docs run: npm run build:website + + - name: Upload denoDist package + uses: actions/upload-artifact@v2 + with: + name: websiteDist + path: ./websiteDist diff --git a/.github/workflows/deploy-artifact-as-branch.yml b/.github/workflows/deploy-artifact-as-branch.yml new file mode 100644 index 0000000000..1e7f994233 --- /dev/null +++ b/.github/workflows/deploy-artifact-as-branch.yml @@ -0,0 +1,51 @@ +name: Deploy specified artifact as a branch +on: + workflow_call: + inputs: + workflow_id: + required: true + type: string + artifact_name: + required: true + type: string + target_branch: + required: true + type: string + commit_message: + required: true + type: string +jobs: + deploy-artifact-as-branch: + runs-on: ubuntu-latest + steps: + - name: Checkout `${{ inputs.target_branch }}` branch + uses: actions/checkout@v2 + with: + ref: ${{ inputs.target_branch }} + + - name: Remove existing files first + run: git rm -r . + + - name: Download artifact into cloned branch + run: gh run download "$WORKFLOW_ID" -n "$ARTIFACT_NAME" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + WORKFLOW_ID: ${{ inputs.workflow_id }} + ARTIFACT_NAME: ${{ inputs.artifact_name }} + + - name: Publish target branch + run: | + git add -A + if git diff --staged --quiet; then + echo 'Nothing to publish' + else + git config user.name 'GitHub Action Script' + git config user.email 'please@open.issue' + + git commit -a -m "$COMMIT_MESSAGE" + git push + echo 'Pushed' + fi + env: + TARGET_BRANCH: ${{ inputs.target_branch }} + COMMIT_MESSAGE: ${{ inputs.commit_message }}