Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish firmware release as FOTA bundles to nRF Cloud #154

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/attach_release_assets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Attach Release Assets

permissions:
contents: write
actions: write

on:
release:
Expand Down Expand Up @@ -33,3 +34,10 @@ jobs:
with:
fail_on_unmatched_files: true
files: hello.nrfcloud.com-*.*

- name: Trigger publish firmware workflow
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh workflow run publish-firmware-bundles.yml \
-F version=${{ env.VERSION }}
34 changes: 34 additions & 0 deletions .github/workflows/publish-firmware-bundles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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"

- name: Install dependencies
run: npm i @octokit/rest yazl

- run:
npx tsx .github/workflows/register-firmware-bundles.mjs ${{ inputs.version
}}
env:
NRF_CLOUD_API_KEY: ${{ secrets.NRF_CLOUD_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
125 changes: 125 additions & 0 deletions .github/workflows/register-firmware-bundles.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
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";

const owner = process.env.OWNER ?? "hello-nrfcloud";
const repo = process.env.REPO ?? "firmware";
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\.com-${version}-thingy91x-((?<configuration>.+)-)?app_update_signed\.bin$`
);

const assets = release.assets.filter(
({ name }) => nameRegEx.exec(name) !== null
);

if (assets.length === 0) {
console.error(`No assets found for release ${version}!`);
console.debug(`Got: ${release.assets.map(({ name }) => name).join(", ")}!`);
process.exit(1);
}

for (const asset of assets) {
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));
}
}
Loading