From 71dd289ba323f9d9c4c42311652faba965bd7c4f Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 20 Dec 2021 20:02:29 +0200 Subject: [PATCH] gh/actions: refactor out action to deploy branches (#3433) --- .eslintignore | 2 - .../actions/deploy-dir-as-branch/action.yml | 52 +++++++++++++++ .github/workflows/ci.yml | 14 ++-- .gitignore | 2 - .prettierignore | 2 - resources/gitpublish.sh | 66 ------------------- 6 files changed, 60 insertions(+), 78 deletions(-) create mode 100644 .github/actions/deploy-dir-as-branch/action.yml delete mode 100755 resources/gitpublish.sh diff --git a/.eslintignore b/.eslintignore index ec6a952fa7..a872ca6e25 100644 --- a/.eslintignore +++ b/.eslintignore @@ -4,8 +4,6 @@ /coverage /npmDist /denoDist -/npm -/deno # Ignore TS files inside integration test /integrationTests/ts/*.ts diff --git a/.github/actions/deploy-dir-as-branch/action.yml b/.github/actions/deploy-dir-as-branch/action.yml new file mode 100644 index 0000000000..0910ca753a --- /dev/null +++ b/.github/actions/deploy-dir-as-branch/action.yml @@ -0,0 +1,52 @@ +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`/cloned_${{ inputs.target_branch }}_XXXXXX") + echo "BRANCH_DIR=$BRANCH_DIR" >> $GITHUB_ENV + + - 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 '${{ github.workspace }}/${{ inputs.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 ${{ github.sha }} to '${{ inputs.target_branch }}' branch' + git push + echo 'Pushed' + fi + + - name: Remove cloned branch + if: ${{ always() }} + shell: bash + run: 'rm -rf $BRANCH_DIR' diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 029459e1a8..f3c355d1cf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -243,9 +243,10 @@ jobs: run: npm run build:npm - name: Deploy to `npm` branch - run: npm run gitpublish:npm - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + uses: ./.github/actions/deploy-dir-as-branch + with: + src_dir: npmDist + target_branch: npm deploy-to-deno-branch: name: Deploy to `deno` branch @@ -272,6 +273,7 @@ jobs: run: npm run build:deno - name: Deploy to `deno` branch - run: npm run gitpublish:deno - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + uses: ./.github/actions/deploy-dir-as-branch + with: + src_dir: denoDist + target_branch: deno diff --git a/.gitignore b/.gitignore index 0687d549b7..69b0985aad 100644 --- a/.gitignore +++ b/.gitignore @@ -12,5 +12,3 @@ /coverage /npmDist /denoDist -/npm -/deno diff --git a/.prettierignore b/.prettierignore index 384c2585fb..07b1fbef69 100644 --- a/.prettierignore +++ b/.prettierignore @@ -5,5 +5,3 @@ /coverage /npmDist /denoDist -/npm -/deno diff --git a/resources/gitpublish.sh b/resources/gitpublish.sh deleted file mode 100755 index bf27acdc1f..0000000000 --- a/resources/gitpublish.sh +++ /dev/null @@ -1,66 +0,0 @@ -#!/bin/bash - -# This script maintains a git branch which mirrors main but in a form that -# what will eventually be deployed to npm, allowing npm dependencies to use: -# -# "graphql": "git://github.com/graphql/graphql-js.git#npm" -# -# Additionally it use use to push Deno build to `deno` branch. - -BRANCH=$1 -DIST_DIR=$2 - -# Exit immediately if any subcommand terminated -set -e - -if [ -z "${BRANCH}" ]; then - echo 'Must provide BRANCH as first argument!' - exit 1; -fi; - -if [ -z "${DIST_DIR}" ]; then - echo 'Must provide DIST_DIR as second argument!' - exit 1; -fi; - -if [ -z "${GITHUB_TOKEN}" ]; then - echo 'Must provide GITHUB_TOKEN as environment variable!' - exit 1; -fi; - -if [ -z "${GITHUB_ACTOR}" ]; then - echo 'Must provide GITHUB_ACTOR as environment variable!' -fi; - -if [ ! -d $DIST_DIR ]; then - echo "Directory '${DIST_DIR}' does not exist!" - exit 1; -fi; - -# Create empty directory -rm -rf $BRANCH -git clone -b $BRANCH -- "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/graphql/graphql-js.git" $BRANCH - -# Remove existing files first -rm -rf $BRANCH/**/* -rm -rf $BRANCH/* - -# Copy over necessary files -cp -r $DIST_DIR/* $BRANCH/ - -# Reference current commit -HEAD_REV=`git rev-parse HEAD` -echo $HEAD_REV - -# Deploy -cd $BRANCH -git config user.name "GitHub Action Script" -git config user.email "please@open.issue" -git add -A . -if git diff --staged --quiet; then - echo "Nothing to publish" -else - git commit -a -m "Deploy $HEAD_REV to '$BRANCH' branch" - git push > /dev/null 2>&1 - echo "Pushed" -fi