Skip to content

Publish Release on Version Change #4

Publish Release on Version Change

Publish Release on Version Change #4

Workflow file for this run

name: Publish Release on Version Change
on:
push:
paths:
- package.json
workflow_dispatch:
inputs:
force_build:
description: 'Force a build and release'
required: false
default: 'false'
jobs:
check-version:
if: github.event_name == 'push' || github.event.inputs.force_build == 'false'
runs-on: ubuntu-latest
outputs:
version_changed: ${{ steps.version_compare.outputs.version_changed }}
current_version: ${{ steps.current_version.outputs.current_version }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Cache npm
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install dependencies
run: npm install
- name: Extract current version
id: current_version
run: echo "::set-output name=current_version::$(jq -r '.version' package.json)"
- name: Get previous version
id: previous_version
run: |
git show HEAD~1:package.json > previous_package.json || echo '{"version": "0.0.0"}' > previous_package.json
echo "::set-output name=previous_version::$(jq -r '.version' previous_package.json)"
- name: Compare versions
id: version_compare
run: |
if [ "${{ steps.current_version.outputs.current_version }}" != "${{ steps.previous_version.outputs.previous_version }}" ]; then
echo "::set-output name=version_changed::true"
else
echo "::set-output name=version_changed::false"
fi
build-linux:
if: github.event.inputs.force_build == 'true' || needs.check-version.outputs.version_changed == 'true'
runs-on: ubuntu-latest
needs: check-version
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Build Linux
run: npm run dist -- --linux
build-windows:
if: github.event.inputs.force_build == 'true' || needs.check-version.outputs.version_changed == 'true'
runs-on: windows-latest
needs: check-version
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Build Windows
run: npm run dist -- --win
build-mac:
if: github.event.inputs.force_build == 'true' || needs.check-version.outputs.version_changed == 'true'
runs-on: macos-latest
needs: check-version
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Build Mac
run: npm run dist -- --mac
create-release:
if: github.event.inputs.force_build == 'true' || needs.check-version.outputs.version_changed == 'true'
runs-on: ubuntu-latest
needs: [build-linux, build-windows, build-mac]
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set current version output for force build
if: github.event.inputs.force_build == 'true'
run: echo "::set-output name=current_version::$(jq -r '.version' package.json)"
- name: Create Release and Tag
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |

Check failure on line 129 in .github/workflows/publish.yml

View workflow run for this annotation

GitHub Actions / Publish Release on Version Change

Invalid workflow file

The workflow is not valid. .github/workflows/publish.yml (Line: 129, Col: 19): Unrecognized named-value: 'jq'. Located at position 36 within expression: github.event.inputs.force_build && jq -r ".version" "package.json"
const version = '${{ needs.check-version.outputs.current_version }}' || '${{ github.event.inputs.force_build && jq -r ".version" "package.json" }}';
const tag = `v${version}`;
await github.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/tags/${tag}`,
sha: context.sha,
});
const release = await github.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tag,
name: `Release ${version}`,
body: `Automated release for version ${version}`,
draft: false,
prerelease: false,
});
// Upload assets to release
const fs = require('fs');
const path = require('path');
const distDir = path.resolve(__dirname, 'dist');
const files = fs.readdirSync(distDir);
for (const file of files) {
const filePath = path.join(distDir, file);
const content = fs.createReadStream(filePath);
await github.repos.uploadReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release.data.id,
name: file,
data: content,
});
}