Add GitHub workflow for automatically updating index.js

This commit is contained in:
Marcono1234 2024-02-25 21:35:21 +01:00
parent 63d15e7a1e
commit f17024a51c
11 changed files with 96 additions and 67 deletions

View File

@ -1,3 +1,4 @@
dist/
action/
lib/
node_modules/
node_modules/

View File

@ -1,51 +0,0 @@
# `dist/index.js` is a special file in Actions.
# When you reference an action with `uses:` in a workflow,
# `index.js` is the code that will run.
# For our project, we generate this file through a build process from other source files.
# We need to make sure the checked-in `index.js` actually matches what we expect it to be.
name: Check dist directory
on:
push:
branches:
- main
- releases/**
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'
workflow_dispatch:
jobs:
check-dist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Build
run: |
npm -v
node -v
npm clean-install
npm run build
- name: Compare the expected and actual dist/ directories
run: |
if [ "$(git diff --ignore-space-at-eol dist/ | wc -l)" -gt "0" ]; then
echo "Detected uncommitted changes after build. See status below:"
git diff
exit 1
fi
id: diff
# If index.js was different than expected, upload the expected version as an artifact
- uses: actions/upload-artifact@v4
if: ${{ failure() && steps.diff.conclusion == 'failure' }}
with:
name: dist
path: dist/

View File

@ -0,0 +1,22 @@
# Pull request authors are not expected to update `action/index.js` themselves
name: Check no dist update
on:
pull_request:
# For simplicity determine if the file was changed using `paths` here (instead of running any diff command below)
# There are some limitations to this, but they most likely won't cause any issues here
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#git-diff-comparisons
paths:
- 'action/index.js'
jobs:
check-no-dist-update:
name: Check no dist update
runs-on: ubuntu-latest
steps:
- name: Check no dist update
# Fail unconditionally; this workflow is only run if the file has changed, see
# condition at the start of this file
run: |
echo "Pull requests should not update 'action/index.js'"
exit 1

View File

@ -36,13 +36,13 @@ jobs:
with:
node-version: 20
- name: Build action (pull request)
# Pull requests are not expected to update `dist/index.js` themselves; therefore build `dist/index.js`
# here before running integration test
if: github.event_name == 'pull_request'
- name: Build action
# Pull requests are not expected to update `action/index.js`, and pushes to the `main` branch
# might not have updated the file either (and rely on CI to do that), therefore build the
# action here first
run: |
npm clean-install
npm run build
npm run github_ci_all
- name: Run wrapper-validation-action
id: action-test
@ -76,13 +76,13 @@ jobs:
with:
node-version: 20
- name: Build action (pull request)
# Pull requests are not expected to update `dist/index.js` themselves; therefore build `dist/index.js`
# here before running integration test
if: github.event_name == 'pull_request'
- name: Build action
# Pull requests are not expected to update `action/index.js`, and pushes to the `main` branch
# might not have updated the file either (and rely on CI to do that), therefore build the
# action here first
run: |
npm clean-install
npm run build
npm run github_ci_all
- name: Run wrapper-validation-action
id: action-test

50
.github/workflows/update-dist.yml vendored Normal file
View File

@ -0,0 +1,50 @@
name: Update `action/index.js`
on:
push:
branches:
- main
jobs:
update-dist:
runs-on: ubuntu-latest
permissions:
# Allow the job to push the changed file to the repository
contents: write
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Build and test
run: |
npm -v
node -v
npm clean-install
npm run github_ci_all
# To be safe, verify that either there are no changes or only `action/index.js` has changed
- name: Verify no unexpected changes
run: |
# Check for changes to any file other than `action/index.js`,
# see https://stackoverflow.com/a/29374503
# Note that this does not detect new untracked files
if ! git diff --exit-code --quiet -- . ':!action/index.js'; then
echo "Unexpected changes:"
git diff -- . ':!action/index.js'
exit 1
fi
# Commit and push changes; has no effect if the file did not change
# Important: The push event will not trigger any other workflows, see
# https://github.com/stefanzweifel/git-auto-commit-action?tab=readme-ov-file#commits-made-by-this-action-do-not-trigger-new-workflow-runs
- name: Commit & push changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: 'Update `action/index.js`'
file_pattern: 'action/index.js'

4
.gitignore vendored
View File

@ -100,3 +100,7 @@ lib/**/*
.idea/
*.iml
# Ignore `index.js` file created during build; it is copied by CI to a separate directory
# Otherwise authors of PRs would commit it, causing merge conflicts or erroneous merges
/dist

View File

@ -1,3 +1,4 @@
dist/
action/
lib/
node_modules/
node_modules/

View File

@ -1,9 +1,10 @@
# Release
* starting on `main`
* `npm install`
* `npm clean-install`
* `npm run all`
* Commit and push any changes to the `dist` directory. Wait for CI.
* verify that `dist/index.js` matches `action/index.js`
* if not, commit and push the changes, then wait for CI to finish
* `git tag v1.0.x && git push --tags` with the actual version number
* `git tag -f -a v1 && git push -f --tags`
* go to https://github.com/gradle/wrapper-validation-action/releases

View File

@ -22,7 +22,7 @@ outputs:
runs:
using: 'node20'
main: 'dist/index.js'
main: 'action/index.js'
branding:
icon: 'shield'

View File

@ -12,7 +12,8 @@
"compile": "ncc build",
"test": "jest",
"build": "npm run check && npm run compile",
"all": "npm run build && npm test"
"all": "npm run build && npm test",
"github_ci_all": "npm run all && cp dist/index.js action/index.js"
},
"repository": {
"type": "git",