Skip to content
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
62 changes: 62 additions & 0 deletions .github/scripts/release.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const fs = require('node:fs');

const VERSION_TAG_REGEX = /^v(\d+\.\d+\.\d+(-(alpha|beta)\.\d+)?)$/;

const ORG = 'DouglasNeuroinformatics';
const PACKAGES = ['open-data-capture-api', 'open-data-capture-gateway', 'open-data-capture-web'];

/** @typedef {import('github-script').AsyncFunctionArguments} AsyncFunctionArguments */

/** @typedef {Awaited<ReturnType<AsyncFunctionArguments['github']['rest']['packages']['getAllPackageVersionsForPackageOwnedByOrg']>>['data'][number]} PackageInfo */

/**
* Extract the semver release tag (e.g. '1.2.3') for a published package version, or null
* @param {PackageInfo} packageInfo
* @returns {null | string}
*/
function extractPackageVersionTag(packageInfo) {
const tags = packageInfo.metadata?.container?.tags ?? [];
for (const tag of tags) {
const match = VERSION_TAG_REGEX.exec(tag)?.[1];
if (match) {
return match;
}
}
return null;
}

/**
* Determine whether the current package.json version differs from the latest published
* container release, and expose the version + decision as workflow outputs.
* @param {AsyncFunctionArguments} args
*/
module.exports = async function main({ core, github }) {
/** @type {string} */
const currentVersion = JSON.parse(fs.readFileSync('package.json', 'utf-8')).version;

/** @type {string | null | undefined} */
let resolvedReleaseVersionTag = undefined;

for (const packageName of PACKAGES) {
const response = await github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg({
org: ORG,
package_name: packageName,
package_type: 'container'
});
const latestPackage = response.data.find((item) => item.metadata?.container?.tags?.includes('latest'));
if (!latestPackage) {
throw new Error(`Failed to find package '${packageName}' with tag 'latest'`);
}
const versionTag = extractPackageVersionTag(latestPackage);
if (resolvedReleaseVersionTag === undefined) {
resolvedReleaseVersionTag = versionTag;
} else if (versionTag !== resolvedReleaseVersionTag) {
throw new Error(
`Unexpected version for package '${packageName}': expected '${resolvedReleaseVersionTag}', got '${versionTag}'`
);
}
}

core.setOutput('version', currentVersion);
core.setOutput('should_release', String(currentVersion !== resolvedReleaseVersionTag));
};
104 changes: 96 additions & 8 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,101 @@ on:
push:
branches: ['main']
workflow_dispatch:
permissions:
contents: write
packages: write
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
defaults:
run:
shell: bash
jobs:
configure:
runs-on: ubuntu-latest
outputs:
should_release: ${{ steps.version.outputs.should_release }}
version: ${{ steps.version.outputs.version }}
matrix: ${{ steps.matrix.outputs.matrix }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Derive Build Matrix
id: matrix
run: |
matrix=$(docker compose config --no-interpolate --format json \
| jq -c '{include:[.services|to_entries[]|select(.value.build and .value.image)|{image:(.value.image|split(":")[0]),dockerfile:.value.build.dockerfile}]}')
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
- name: Determine Release Version
id: version
uses: actions/github-script@v7
with:
script: |
await require('./.github/scripts/release.cjs')({ context, core, github, exec, glob, io })
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup PNPM
uses: pnpm/action-setup@v3
- name: Setup Node
uses: actions/setup-node@v4
with:
cache: pnpm
node-version-file: '.nvmrc'
- name: Install Dependencies
run: pnpm install --frozen-lockfile
- name: Validate
run: GATEWAY_DATABASE_URL=file:${TMPDIR}tmp.db pnpm lint
build:
runs-on: ubuntu-latest
needs: [configure, validate]
if: ${{ needs.configure.outputs.should_release == 'true' }}
strategy:
fail-fast: true
matrix: ${{ fromJSON(needs.configure.outputs.matrix) }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Set Up QEMU
uses: docker/setup-qemu-action@v3
- name: Set Up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ matrix.image }}
tags: |
type=raw,value=latest
type=raw,value=${{ needs.configure.outputs.version }}
- name: Build and Push Docker Images
uses: docker/build-push-action@v6
with:
build-args: |
RELEASE_VERSION=${{ needs.configure.outputs.version }}
context: .
file: ${{ matrix.dockerfile }}
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
release:
permissions:
contents: write
packages: write
uses: DouglasNeuroinformatics/.github/.github/workflows/app-release.yaml@main
with:
org: DouglasNeuroinformatics
packages: open-data-capture-api, open-data-capture-gateway, open-data-capture-web
validate_command: GATEWAY_DATABASE_URL=file:${TMPDIR}tmp.db pnpm lint
runs-on: ubuntu-latest
needs:
- build
- configure
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Create Release
uses: softprops/action-gh-release@v2
with:
tag_name: v${{ needs.configure.outputs.version }}
Loading
Loading