Publish Release on Version Change #1
This file contains 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: Publish Release on Version Change | |
on: | |
push: | |
paths: | |
- package.json | |
workflow_dispatch: | |
jobs: | |
check-version: | |
runs-on: ubuntu-latest | |
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::$(cat package.json | jq -r '.version')" | |
- name: Get previous version | |
id: previous_version | |
run: echo "::set-output name=previous_version::$(git show HEAD~1:package.json | jq -r '.version')" | |
- 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: 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: 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: 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: 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: Create Release and Tag | |
uses: actions/github-script@v6 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const version = '${{ needs.check-version.outputs.current_version }}'; | |
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, | |
}); | |
} |