-
Notifications
You must be signed in to change notification settings - Fork 8
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
1 parent
b5e9773
commit 5f8665e
Showing
2 changed files
with
155 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,35 @@ | ||
name: Publish firmware bundles to nRF Cloud | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
version: | ||
required: true | ||
type: string | ||
description: Release version to publish, e.g. "v1.1.0" | ||
|
||
jobs: | ||
publish: | ||
environment: production | ||
|
||
name: Publish firmware bundles to nRF Cloud | ||
|
||
runs-on: ubuntu-22.04 | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20.x" | ||
cache: "npm" | ||
|
||
- name: Install dependencies | ||
run: npm ci --no-audit | ||
|
||
- run: | ||
node .github/workflows/register-firmware-bundles.js ${{ inputs.version | ||
}} | ||
env: | ||
NRF_CLOUD_API_KEY: ${{ secrets.NRF_CLOUD_API_KEY }} | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
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,120 @@ | ||
import { Octokit } from '@octokit/rest' | ||
import fs, { createWriteStream } from 'node:fs' | ||
import { mkdtemp, readFile } from 'node:fs/promises' | ||
import os from 'node:os' | ||
import path from 'node:path' | ||
import { Readable } from 'stream' | ||
import { finished } from 'stream/promises' | ||
import yazl from 'yazl' | ||
import pJSON from '../../package.json' assert { type: 'json' } | ||
|
||
const [, owner, repo] = new URL(pJSON.repository.url).pathname | ||
.replace('.git', '') | ||
.split('/') | ||
|
||
const version = process.argv[process.argv.length - 1] | ||
|
||
console.log(`Publishing release version`, version) | ||
|
||
const octokit = new Octokit({ | ||
auth: process.env.GITHUB_TOKEN, | ||
}) | ||
|
||
console.log(`Repository: ${owner}/${repo}`) | ||
|
||
const { data: release } = await octokit.rest.repos.getReleaseByTag({ | ||
repo, | ||
owner, | ||
tag: version, | ||
}) | ||
|
||
if (release === undefined) { | ||
console.error(`Release for ${version} not found!`) | ||
console.debug(`Got: ${releases.map(({ name }) => name).join(', ')}!`) | ||
process.exit(1) | ||
} | ||
|
||
const nameRegEx = new RegExp( | ||
`^hello-nrfcloud-thingy91-((?<configuration>.+)-)?${release.tag_name}-fwupd\.bin$`, | ||
) | ||
|
||
for (const asset of release.assets.filter(({ name }) => | ||
name.endsWith('-fwupd.bin'), | ||
)) { | ||
const { url, name, label, size } = asset | ||
const { | ||
groups: { configuration }, | ||
} = nameRegEx.exec(name) | ||
|
||
const fwversion = `${release.tag_name}${ | ||
configuration !== undefined ? `-${configuration}` : '' | ||
}` | ||
const fwName = `hello.nrfcloud.com ${fwversion}` | ||
console.log(fwName) | ||
const manifest = { | ||
name: fwName, | ||
description: label, | ||
fwversion, | ||
'format-version': 1, | ||
files: [ | ||
{ | ||
file: name, | ||
type: 'application', | ||
size, | ||
}, | ||
], | ||
} | ||
|
||
const res = await fetch(url, { | ||
headers: { | ||
Accept: 'application/octet-stream', | ||
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`, | ||
}, | ||
}) | ||
|
||
const zipfile = new yazl.ZipFile() | ||
zipfile.addBuffer( | ||
Buffer.from(JSON.stringify(manifest, null, 2), 'utf-8'), | ||
'manifest.json', | ||
) | ||
|
||
const tempDir = await mkdtemp(path.join(os.tmpdir(), name)) | ||
const fwFile = path.join(tempDir, 'firmware.bin') | ||
|
||
const body = Readable.fromWeb(res.body) | ||
const download_write_stream = fs.createWriteStream(fwFile) | ||
await finished(body.pipe(download_write_stream)) | ||
|
||
zipfile.addFile(fwFile, name) | ||
|
||
const zipFileName = path.join(tempDir, `${name}.zip`) | ||
|
||
await new Promise((resolve) => { | ||
zipfile.outputStream | ||
.pipe(createWriteStream(zipFileName)) | ||
.on('close', () => { | ||
resolve() | ||
}) | ||
zipfile.end() | ||
}) | ||
|
||
const createFirmwareRes = await fetch( | ||
'https://api.nrfcloud.com/v1/firmwares', | ||
{ | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${process.env.NRF_CLOUD_API_KEY}`, | ||
'Content-Type': 'application/zip', | ||
}, | ||
body: await readFile(zipFileName), | ||
}, | ||
) | ||
|
||
if (createFirmwareRes.status !== 200) { | ||
console.error( | ||
`Failed to upload firmware bundle: ${await createFirmwareRes.text()}`, | ||
) | ||
} else { | ||
console.log(JSON.stringify(await createFirmwareRes.json(), null, 2)) | ||
} | ||
} |