Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Create action #1

Merged
merged 1 commit into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/auto-tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Auto-tag
on:
push:
tags:
- '*.*.*'
jobs:
auto-tag:
name: Auto-tag
runs-on: ubuntu-latest
steps:
- name: Auto-tag
uses: silverstripe/gha-auto-tag@main
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# gha-update-js
# GitHub Actions - Update JS

Update JS dependencies in core Silverstripe modules, create new bundles and create pull-requests

## Usage:

GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
This action has no inputs. It is intended to be [run on a schedule](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#scheduled-events). You can copy the below example verbatim, and just modify the schedule to suit your use case.

**.github/workflows/update-js.yml**
```yml
name: Update JS
on:
# Run on a schedule of 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: silverstripe/gha-update-js@main
```
151 changes: 151 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
name: Update JS
description: Updates JS dependencies in Silverstripe core modules

runs:
using: composite
steps:

- name: Checkout code
uses: actions/checkout@7884fcad6b5d53d10323aee724dc68d8b9096a2e # v2.4.2

- name: Read .nvmrc
id: read-nvm
shell: bash
run: |
echo ::set-output name=version::$(cat .nvmrc)

- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.3.0
with:
node-version: ${{ steps.read-nvm.outputs.version }}

- 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 ..
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
git clone https://github.com/silverstripe/silverstripe-admin.git admin
cd admin
git checkout 1
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
# yarn comes preinstalled with actions/setup-node
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="false"
TEST="false"
if [[ "$(jq .scripts.lint? package.json)" != "null" ]]; then LINT="true"; fi
if [[ "$(jq .scripts.test? package.json)" != "null" ]]; then TEST="true"; 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 == 'true'
shell: bash
run: |
yarn lint

- name: Yarn test
if: steps.package-json.outputs.test == 'true'
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
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved

- name: Remove any old pull-requests
if: always()
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
# https://docs.github.com/en/rest/pulls/pulls#list-pull-requests
RESP_CODE=$(curl -w %{http_code} -s -o __response.json \
-X GET https://api.github.com/repos/$GITHUB_REPOSITORY/pulls \
-H "Accept: application/vnd.github.v3+json")
if [[ $RESP_CODE != "200" ]]; then
echo "Unable to list pull-requests - HTTP response code was $RESP_CODE"
exit 1
fi
JSON=$(cat __response.json)
NUMBERS=$(echo $JSON | jq '.[] | select(.title=="DEP Update JS dependencies" and .user.login=="github-actions[bot]") | .number')
for NUMBER in $NUMBERS; do
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
# https://docs.github.com/en/rest/pulls/pulls#update-a-pull-request
RESP_CODE=$(curl -w %{http_code} -s -o /dev/null \
-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
)
if [[ $RESP_CODE != "200" ]]; then
echo "Unable to close pull-request $NUMBER - HTTP response code was $RESP_CODE"
exit 1
fi
echo "Closed old pull-request $NUMBER"
done

- name: Remove any old branches
if: always()
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
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
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
if: always()
id: generate-branch-name
shell: bash
env:
GITHUB_REF: ${{ github.ref }}
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()
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
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