Skip to content

Commit 710c715

Browse files
authored
Optimize workflow with outputs (#8)
* Add workflow with outputs * Optimize workflow
1 parent 8575c30 commit 710c715

File tree

7 files changed

+107
-49
lines changed

7 files changed

+107
-49
lines changed

.github/actions/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const OWNER = 'Gitification-App'
2+
export const REPO = 'gitification'
3+
export const token = process.env.GITHUB_TOKEN
4+
export const secret = process.env.CLIENT_SECRET
5+
export const id = process.env.CLIENT_ID

.github/actions/env.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
declare global {
2+
namespace NodeJS {
3+
interface ProcessEnv {
4+
CLIENT_SECRET: string
5+
CLIENT_ID: string
6+
GITHUB_TOKEN: string
7+
}
8+
}
9+
}
10+
11+
export {}

.github/actions/release.ts

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,23 @@ import { fileURLToPath } from 'url'
33
import fs from 'node:fs/promises'
44
import * as github from '@actions/github'
55
import { execaCommand } from 'execa'
6-
7-
declare global {
8-
namespace NodeJS {
9-
interface ProcessEnv {
10-
CLIENT_SECRET: string
11-
CLIENT_ID: string
12-
GITHUB_TOKEN: string
13-
}
14-
}
15-
}
6+
import { OWNER, REPO, id, secret, token } from './constants'
167

178
const dirname = path.dirname(fileURLToPath(import.meta.url))
189
const envPath = path.join(dirname, '..', '..', '.env')
1910
const tauriConfPath = path.join(dirname, '..', '..', 'src-tauri', 'tauri.conf.json')
20-
const packageJsonPath = path.join(dirname, '..', '..', 'package.json')
21-
const token = process.env.GITHUB_TOKEN
22-
const secret = process.env.CLIENT_SECRET
23-
const id = process.env.CLIENT_ID
2411

2512
const envFileContent = `\
2613
VITE_CLIENT_SECRET=${secret}
2714
VITE_CLIENT_ID=${id}
2815
`
2916

3017
async function run() {
31-
const tauriConf = JSON.parse(
18+
const { package: { productName, version } } = JSON.parse(
3219
await fs.readFile(tauriConfPath, 'utf-8'),
3320
) as typeof import('../../src-tauri/tauri.conf.json')
3421

35-
const packageJSON = JSON.parse(
36-
await fs.readFile(packageJsonPath, 'utf-8'),
37-
) as typeof import('../../package.json')
38-
39-
if (packageJSON.version !== tauriConf.package.version)
40-
throw new Error('Tauri config and Package JSON versions are not the same, update both of them!')
41-
42-
const dmgFileName = `Gitification_${packageJSON.version}_universal.dmg`
22+
const dmgFileName = `${productName}_${version}_universal.dmg`
4323

4424
const dmgPath = path.join(
4525
dirname,
@@ -56,33 +36,20 @@ async function run() {
5636

5737
const octokit = github.getOctokit(token)
5838

59-
try {
60-
const remoteRelease = await octokit.rest.repos.getLatestRelease({
61-
owner: 'Gitification-App',
62-
repo: 'gitification',
63-
})
64-
65-
if (remoteRelease.data.name === packageJSON.version.toString()) {
66-
console.log('No version change detected, skipping build.')
67-
return
68-
}
69-
}
70-
catch { /* If endpoint throws, it means no release yet */ }
71-
7239
await fs.writeFile(envPath, envFileContent, 'utf-8')
7340
await execaCommand('pnpm tauri build --target universal-apple-darwin', { stdio: 'inherit' })
7441

7542
const release = await octokit.rest.repos.createRelease({
76-
owner: 'Gitification-App',
77-
repo: 'gitification',
78-
tag_name: packageJSON.version.toString(),
79-
name: packageJSON.version.toString(),
80-
body: 'Click to view [CHANGELOG](https://github.com/Gitification-App/gitification/blob/main/CHANGELOG.md).',
43+
owner: OWNER,
44+
repo: REPO,
45+
tag_name: version,
46+
name: version,
47+
body: `Click to view [CHANGELOG](https://github.com/${OWNER}/${REPO}/blob/main/CHANGELOG.md).`,
8148
})
8249

8350
octokit.rest.repos.uploadReleaseAsset({
84-
owner: 'Gitification-App',
85-
repo: 'gitification',
51+
owner: OWNER,
52+
repo: REPO,
8653
name: dmgFileName,
8754
release_id: release.data.id,
8855
// @ts-expect-error type

.github/actions/versionCheck.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import path from 'path'
2+
import { fileURLToPath } from 'url'
3+
import fs from 'node:fs/promises'
4+
import * as github from '@actions/github'
5+
import actions from '@actions/core'
6+
import { compare } from 'compare-versions'
7+
import { OWNER, REPO, token } from './constants'
8+
9+
const dirname = path.dirname(fileURLToPath(import.meta.url))
10+
const tauriConfPath = path.join(dirname, '..', '..', 'src-tauri', 'tauri.conf.json')
11+
const packageJsonPath = path.join(dirname, '..', '..', 'package.json')
12+
13+
async function run() {
14+
const tauriConf = JSON.parse(
15+
await fs.readFile(tauriConfPath, 'utf-8'),
16+
) as typeof import('../../src-tauri/tauri.conf.json')
17+
18+
const packageJSON = JSON.parse(
19+
await fs.readFile(packageJsonPath, 'utf-8'),
20+
) as typeof import('../../package.json')
21+
22+
if (packageJSON.version !== tauriConf.package.version)
23+
throw new Error('Tauri config and Package JSON versions are not the same, update both of them!')
24+
25+
const octokit = github.getOctokit(token)
26+
const version = packageJSON.version
27+
28+
try {
29+
const { data: remoteReleases } = await octokit.rest.repos.listReleases({
30+
owner: OWNER,
31+
repo: REPO,
32+
})
33+
34+
const isNewRelease = remoteReleases
35+
.filter(release => !release.draft)
36+
.every(release => (
37+
compare(version, release.tag_name, '>')
38+
))
39+
40+
if (isNewRelease) {
41+
actions.setOutput('isNewRelease', 'true')
42+
return
43+
}
44+
}
45+
catch { /* If endpoint throws, it means no release yet */ }
46+
47+
actions.setOutput('isNewRelease', 'false')
48+
}
49+
50+
run()

.github/workflows/release.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,23 @@ jobs:
4040
4141
- name: Install dependencies
4242
run: pnpm install
43+
44+
- name: Check if new release
45+
id: releaseCheck
46+
run: pnpx tsx .github/actions/versionCheck.ts
47+
env:
48+
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
49+
CLIENT_ID: ${{ secrets.CLIENT_ID }}
50+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4351

4452
- name: install Rust stable
53+
if: steps.releaseCheck.outputs.isNewRelease == 'true'
4554
uses: dtolnay/rust-toolchain@stable
4655
with:
4756
targets: 'aarch64-apple-darwin,x86_64-apple-darwin'
4857

4958
- name: Run Release
59+
if: steps.releaseCheck.outputs.isNewRelease == 'true'
5060
run: pnpx tsx .github/actions/release.ts
5161
env:
5262
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"package.json"
2828
],
2929
"devDependencies": {
30+
"@actions/core": "^1.10.0",
3031
"@actions/github": "^5.1.1",
3132
"@antfu/eslint-config": "^0.34.1",
3233
"@iconify-json/octicon": "^1.1.30",

pnpm-lock.yaml

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)