Package update #14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Package update | ||
on: | ||
workflow_dispatch: | ||
inputs: | ||
package: | ||
description: The package to check for updates for | ||
type: choice | ||
required: true | ||
options: | ||
- mithril | ||
permissions: | ||
contents: write | ||
jobs: | ||
update: | ||
name: Update ${{ inputs.package }} | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20 | ||
- name: Get current package version | ||
id: view | ||
run: echo "version=$(npm view "$PKG"@latest version)" | tee $GITHUB_OUTPUT | ||
env: | ||
PKG: ${{ inputs.package }} | ||
- name: Update package.json | ||
id: update | ||
run: node -e "$SCRIPT" | ||
env: | ||
PKG: ${{ inputs.package }} | ||
VERSION: ${{ steps.view.outputs.version }} | ||
SCRIPT: | | ||
const {PKG, VERSION, GITHUB_OUTPUT} = process.env | ||
if (!PKG) throw new Error("Missing $PKG") | ||
if (!VERSION) throw new Error("Missing $VERSION") | ||
const pkg = JSON.parse(fs.readFileSync("package.json") | ||
console.log(`old version=${pkg.dependencies[PKG]}`) | ||
if (pkg.dependencies[PKG] === VERSION) { | ||
fs.writeFileSync(GITHUB_OUTPUT, "commit=n") | ||
console.log("Package version not changed. Skipping push.") | ||
} else { | ||
pkg.dependencies[PKG] = VERSION | ||
fs.writeFileSync("package.json", JSON.stringify(pkg, null, 2)+"\n") | ||
fs.writeFileSync(GITHUB_OUTPUT, "commit=y") | ||
console.log("Package version updated.") | ||
} | ||
- name: Update package-lock.json | ||
if: ${{ steps.update.outputs.commit == "y" }} | ||
Check failure on line 52 in .github/workflows/package-update.yml
|
||
run: npm update "$PKG" --lock-file-only | ||
env: | ||
PKG: ${{ inputs.package }} | ||
- name: Commit changes | ||
if: ${{ steps.update.outputs.commit == "y" }} | ||
id: commit | ||
run: | | ||
set -euo pipefail | ||
if git add package.json package-lock.json; then | ||
git config --global user.name "$ACTOR (on behalf of $TRIGGERING_ACTOR)" | ||
git config --global user.email "$ACTOR@users.noreply.github.com" | ||
git commit --message "Update $PKG to $VERSION" | ||
else | ||
echo '::warning::No changes detected. Skipping push.' | ||
fi | ||
env: | ||
PKG: ${{ inputs.package }} | ||
VERSION: ${{ steps.view.outputs.version }} | ||
ACTOR: ${{ github.actor }} | ||
TRIGGERING_ACTOR: ${{ github.triggering_actor }} | ||
- name: Push to branch | ||
if: ${{ steps.update.outputs.commit == "y" }} | ||
uses: MithrilJS/push-protected@main | ||
with: | ||
token: ${{ secrets.PACKAGE_UPDATE_TOKEN }} |