-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
150 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
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, | ||
}); | ||
} |