From 36bbb1a6afceec96617499a469269aac2e3b0dc1 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Tue, 31 May 2022 11:48:52 +1200 Subject: [PATCH] NEW Create action --- .github/workflows/auto-tag.yml | 14 ++++ README.md | 23 +++++- action.yml | 131 +++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/auto-tag.yml create mode 100644 action.yml diff --git a/.github/workflows/auto-tag.yml b/.github/workflows/auto-tag.yml new file mode 100644 index 0000000..982c17a --- /dev/null +++ b/.github/workflows/auto-tag.yml @@ -0,0 +1,14 @@ +on: + push: + tags: + - '*.*.*' +jobs: + auto-tag: + name: Auto-tag + runs-on: ubuntu-latest + steps: + - name: Auto-tag + uses: silverstripe/gha-auto-tag@main + with: + ref: ${{ github.ref }} + sha: ${{ github.sha }} diff --git a/README.md b/README.md index 8341a90..899792b 100644 --- a/README.md +++ b/README.md @@ -1 +1,22 @@ -# gha-update-js \ No newline at end of file +# GitHub Actions - Update JS + +Update JS dependencies in core Silverstripe modules, create new bundles and create pull-requests + +## Usage: + +**.github/workflows/update-js.yml** +```yml +name: Update JS +on: + # Run on a schedule once per quarter + schedule: + - cron: '0 0 1 */3 *' + workflow_dispatch: +jobs: + update-js: + name: Update JS + runs-on: ubuntu-latest + steps: + - name: Update JS + uses: emteknetnz/gha-update-js@main +``` diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..30be810 --- /dev/null +++ b/action.yml @@ -0,0 +1,131 @@ +name: Update JS +description: Updates JS dependencies in Silverstripe core modules +runs: + using: composite + steps: + - name: Checkout code + uses: actions/checkout@7884fcad6b5d53d10323aee724dc68d8b9096a2e # v2 + + - name: Read .nvmrc + id: read-nvm + shell: bash + run: | + echo ::set-output name=version::$(cat .nvmrc) + + - name: Setup node + uses: actions/setup-node@f1f314fca9dfce2769ece7d933488f076716723e #v1 + with: + node-version: ${{ steps.read-nvm.outputs.version }} + + - name: Install yarn + shell: bash + run: | + npm install --global yarn + + - name: Install admin JS + if: github.event.repository.name != 'silverstripe-admin' + shell: bash + run: | + # Install admin js in sibling directory so shared components are available + DIR=$(pwd) + cd .. + git clone https://github.com/silverstripe/silverstripe-admin.git admin + cd admin + git checkout 1 + yarn install + cd $DIR + + # Use `yarn install` rather than `yarn upgrade` to prevent the following error: + # "error Outdated lockfile. Please run `yarn install` and try again." + - name: Update yarn.lock + shell: bash + run: | + if [ -f yarn.lock ]; then rm yarn.lock; fi + yarn install + + - name: Read package.json + id: package-json + shell: bash + run: | + # Read package.json to see if lint and test are runnable scripts + LINT=0 + TEST=0 + if [ "$(jq .scripts.lint? package.json)" != "null" ]; then LINT=1; fi + if [ "$(jq .scripts.test? package.json)" != "null" ]; then TEST=1; fi + echo "::set-output name=lint::$LINT" + echo "::set-output name=test::$TEST" + echo "LINT is $LINT" + echo "TEST is $TEST" + + # The following 3 steps make up `yarn build` + # Splitting apart to make it easier to see where any failures originate from + - name: Yarn lint + if: steps.package-json.outputs.lint == 1 + shell: bash + run: | + yarn lint + + - name: Yarn test + if: steps.package-json.outputs.test == 1 + shell: bash + run: | + yarn test + + - name: Build JS with webpack + # always run this and subsequent steps even if yarn.lint/yarn.test fails so that pull-request is + # created which will result in a red pull-request build so it's easy to see where things are at + if: always() + shell: bash + run: | + NODE_ENV=production node_modules/webpack/bin/webpack.js -p --bail --progress + + - name: Remove any old pull-requests + if: always() + shell: bash + run: | + JSON=$(curl https://api.github.com/repos/${{ github.repository }}/pulls) + NUMBERS=$(echo $JSON | jq '.[] | select(.title=="DEP Update JS dependencies" and .user.login=="github-actions[bot]") | .number') + for NUMBER in $NUMBERS; do + curl -s \ + -X PATCH https://api.github.com/repos/${{ github.repository }}/pulls/$NUMBER \ + -H "Accept: application/vnd.github.v3+json" \ + -H "Authorization: token ${{ github.token }}" \ + -d @- << EOF + { + "state": "closed" + } + EOF + echo "Closed old pull-request $NUMBER" + done + + - name: Remove any old branches + if: always() + shell: bash + run: | + JSON=$(curl https://api.github.com/repos/${{ github.repository }}/branches) + BRANCHES=$(echo $JSON | jq -r '.[] | .name | select(.|test("^pulls\/[0-9]\/update-js-[0-9]{10}$"))') + for BRANCH in $BRANCHES; do + if [[ "$BRANCH" =~ ^pulls/[0-9\.]+/update\-js\-[0-9]+$ ]]; then + git push origin --delete "$BRANCH" + echo "Deleted old branch $BRANCH" + fi + done + + - name: Generate branch name + if: always() + id: generate-branch-name + shell: bash + run: | + # Convert refs/heads/mybranch to mybranch + CUT=$(echo ${{ github.ref }} | cut -c 12-) + # e.g. pulls/1/update-js-1647810133 + BRANCH=pulls/$CUT/update-js-$(date +%s) + echo ::set-output name=branch::$BRANCH + + - name: Git + if: always() + uses: silverstripe/gha-pull-request@main + with: + branch: ${{ steps.generate-branch-name.outputs.branch }} + title: DEP Update JS dependencies + description: Automated yarn upgrade and yarn build