Skip to content

Commit

Permalink
improve publish script
Browse files Browse the repository at this point in the history
  • Loading branch information
thewh1teagle committed Aug 22, 2024
1 parent 4bfa360 commit 87380d8
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 99 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/landing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: Update downloads manifest
run: bun run scripts/landing_links.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}

- name: Setup Pages
uses: actions/configure-pages@v3
Expand All @@ -50,7 +50,7 @@ jobs:
run: bun run build
working-directory: landing
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}

- name: Upload Artifacts
uses: actions/upload-pages-artifact@v1
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linux_special.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ jobs:
- name: Build
uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
INPUT_CUDA_VERSION: ${{ matrix.cuda-version }}
Expand Down Expand Up @@ -170,4 +170,4 @@ jobs:
bun scripts/publish.js target/release/bundle/deb/*.deb
bun scripts/publish.js target/release/bundle/rpm/*.rpm
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
- name: Build
uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/windows_special.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:
- name: Build
uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
BUILD_DEBUG: ${{ github.event.inputs.debug }}
Expand Down Expand Up @@ -173,5 +173,5 @@ jobs:
bun scripts/publish.js target/release/bundle/nsis/$filePattern
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
DEBUG: ${{ github.event.inputs.debug }}
94 changes: 2 additions & 92 deletions scripts/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,111 +3,21 @@ import fs from 'fs/promises'
import path from 'path'
import { Glob } from 'bun'

// FYI: it doesn't work well with pre releases or draft release that already exists

// Tired of non functional workflows extensions
// Upload specifid file to latest release script

// Github options
const OWNER = 'thewh1teagle'
const REPO = 'vibe'
const TOKEN = process.env.GITHUB_TOKEN
const TOKEN = process.env.GH_TOKEN
const tauriConfPath = path.join(__dirname, '../desktop/src-tauri/tauri.conf.json')
const TAG = 'v' + (await JSON.parse(await fs.readFile(tauriConfPath)).version)
const DST = process.argv[2]
const glob = new Glob(DST)
const DEBUG = process.env.DEBUG === '1'

async function publish(dst, token, tag) {
if (!(await fs.exists(dst))) {
throw new Error(`${dst} Not exists`)
}
try {
const res = await fetch(`https://api.github.com/repos/${OWNER}/${REPO}/releases`, {
headers: {
Authorization: `Bearer ${token}`,
'X-GitHub-Api-tag': '2022-11-28',
Accept: 'application/vnd.github+json',
},
method: 'POST',
body: JSON.stringify({
tag_name: tag,
target_commitish: 'main',
name: tag,
body: 'See assets for download',
draft: false,
prerelease: false,
generate_release_notes: false,
}),
})
checkResponse(res)
const data = await res.json()
if (!data?.errors?.code === 'already_exists') {
await checkResponse(res)
}

console.log(`Created Release ${tag}`)
} catch (e) {
console.error(`Failed to create release ${tag}: ${e}`)
}

// Get release ID
const res = await fetch(`https://api.github.com/repos/${OWNER}/${REPO}/releases/tags/${tag}`, {
headers: {
Authorization: `Bearer ${token}`,
'X-GitHub-Api-tag': '2022-11-28',
Accept: 'application/vnd.github+json',
},
})
checkResponse(res)
const releaseData = await res.json()
const releaseID = releaseData.id
const prev = releaseData.assets.find((a) => a.name.toLowerCase() === path.basename(dst).toLowerCase())
if (DEBUG) {
console.log('[DEBUG] releaseData.assets', releaseData.assets)
console.log('[DEBUG] path.basename(DST)', path.basename(dst))
}
if (prev) {
// Delete previous asset
console.info('Deleting previous release', prev)
const res = await fetch(`https://api.github.com/repos/${OWNER}/${REPO}/releases/assets/${prev.id}`, {
headers: {
Authorization: `Bearer ${token}`,
'X-GitHub-Api-tag': '2022-11-28',
Accept: 'application/vnd.github+json',
},
method: 'DELETE',
})
await checkResponse(res)
}

// Upload asset
try {
const name = path.basename(dst)
console.info('Upload', name)
const res = await fetch(`https://uploads.github.com/repos/${OWNER}/${REPO}/releases/${releaseID}/assets?name=${name}`, {
headers: {
Authorization: `Bearer ${token}`,
'X-GitHub-Api-tag': '2022-11-28',
Accept: 'application/vnd.github+json',
'Content-Type': 'application/octet-stream',
},
method: 'POST',
body: await fs.readFile(dst),
})
await checkResponse(res)
console.log(`Upload asset ${name} from ${dst} successfuly for https://github.com/${OWNER}/${REPO}/releases/${TAG}`)
} catch (e) {
console.error(`Failed to upload asset ${dst}: ${e}`)
}
}

async function checkResponse(response) {
if (![200, 201, 204, 422].includes(response.status)) {
const reason = await response.text()
console.error(`status ${response.status} for ${response.url}: `, reason)
process.exit(1)
}
await $`gh release upload ${tag} ${dst} --clobber`
}

for await (const file of glob.scan()) {
Expand Down

0 comments on commit 87380d8

Please sign in to comment.