Skip to content

Commit c568ba5

Browse files
authored
Merge pull request #960 from joshunrau/ci-update
2 parents 8b9636c + a1e596b commit c568ba5

4 files changed

Lines changed: 116 additions & 60 deletions

File tree

.github/scripts/lib.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
11
// @ts-check
22

3-
/** @param {import('github-script').AsyncFunctionArguments} args */
4-
export async function getLatestVersion({ github }) {
5-
const result = await github.rest.packages.getAllPackageVersionsForPackageOwnedByAuthenticatedUser();
6-
console.log(result);
3+
const PACKAGE_NAMES = ['open-data-capture-api', 'open-data-capture-gateway', 'open-data-capture-web'];
4+
const VERSION_TAG_REGEX = /v([0-9]+.[0-9]+.[0-9]+)/;
5+
6+
/**
7+
* Get the latest published container version from GitHub. If the latest version is not the same
8+
* for all packages, throws an exception.
9+
*
10+
* @param {Pick<import('github-script').AsyncFunctionArguments, "github">} args
11+
* @returns {Promise<string>}
12+
*/
13+
export async function getLatestPublishedVersion({ github }) {
14+
/** @type {string} */
15+
let packageVersion;
16+
for (const packageName of PACKAGE_NAMES) {
17+
const packages = await github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg({
18+
org: 'DouglasNeuroinformatics',
19+
package_name: 'open-data-capture-api',
20+
package_type: 'container'
21+
});
22+
const latestPackage = packages.data.find((item) => item.metadata.container.tags.includes('latest'));
23+
if (!latestPackage) {
24+
console.error(JSON.stringify({ packages }, null, 2));
25+
throw new Error(`Failed to find release with 'latest' tag for package '${packageName}'`);
26+
}
27+
const tags = latestPackage.metadata.container.tags;
28+
const latestTag = tags.find((tag) => VERSION_TAG_REGEX.test(tag));
29+
if (!latestTag) {
30+
throw new Error(`Failed to find valid version tag in array: ${tags}`);
31+
}
32+
const latestVersion = VERSION_TAG_REGEX.exec(latestTag)[1];
33+
if (!packageVersion) {
34+
packageVersion = latestVersion;
35+
} else if (packageVersion !== latestVersion) {
36+
throw new Error(
37+
`Unexpected version for package '${packageName}': expected '${packageVersion}', got '${latestVersion}'`
38+
);
39+
}
40+
}
41+
return packageVersion;
742
}

.github/workflows/build.yaml

Lines changed: 0 additions & 53 deletions
This file was deleted.

.github/workflows/ci.yaml

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,86 @@ permissions:
1313
jobs:
1414
configure:
1515
runs-on: ubuntu-latest
16+
outputs:
17+
current_version: ${{ steps.current_version.outputs.result }}
18+
latest_published_version: ${{ steps.latest_published_version.outputs.result }}
19+
is_release: ${{ steps.current_version.outputs.result != steps.latest_published_version.outputs.result }}
1620
steps:
1721
- name: Checkout Repository
1822
uses: actions/checkout@v4
23+
- name: Get Package Version
24+
id: current_version
25+
run: echo "result=$(cat package.json | jq -r .version)" | sudo tee -a "$GITHUB_OUTPUT"
1926
- name: Run Script
2027
uses: actions/github-script@v7
28+
id: latest_published_version
2129
with:
30+
result-encoding: string
2231
script: |
23-
const { getLatestVersion } = await import('${{ github.workspace }}/.github/scripts/lib.js');
24-
await getLatestVersion();
32+
const { getLatestPublishedVersion } = await import('${{ github.workspace }}/.github/scripts/lib.js');
33+
return await getLatestPublishedVersion({ context, exec, github });
34+
build:
35+
runs-on: ubuntu-latest
36+
needs: [configure]
37+
env:
38+
CURRENT_VERSION: ${{ needs.configure.outputs.current_version }}
39+
strategy:
40+
fail-fast: true
41+
matrix:
42+
include:
43+
- dockerfile: ./apps/api/Dockerfile
44+
image: ghcr.io/douglasneuroinformatics/open-data-capture-api
45+
- dockerfile: ./apps/gateway/Dockerfile
46+
image: ghcr.io/douglasneuroinformatics/open-data-capture-gateway
47+
- dockerfile: ./apps/web/Dockerfile
48+
image: ghcr.io/douglasneuroinformatics/open-data-capture-web
49+
if: ${{ needs.configure.is_release }}
50+
steps:
51+
- name: Checkout Repository
52+
uses: actions/checkout@v4
53+
- name: Setup Environment
54+
run: ./scripts/generate-env.sh
55+
- name: Set Up QEMU
56+
uses: docker/setup-qemu-action@v3
57+
- name: Set Up Docker Buildx
58+
uses: docker/setup-buildx-action@v3
59+
- name: Login to Container Registry
60+
uses: docker/login-action@v3
61+
with:
62+
registry: ghcr.io
63+
username: ${{ github.repository_owner }}
64+
password: ${{ secrets.GITHUB_TOKEN }}
65+
- name: Extract Metadata for Docker
66+
id: meta
67+
uses: docker/metadata-action@v5
68+
with:
69+
images: ${{ matrix.image }}
70+
tags: |
71+
type=raw,value=latest
72+
type=raw,value=${{ needs.configure.outputs.current_version }}
73+
- name: Build and Push Docker Images
74+
uses: docker/build-push-action@v6
75+
with:
76+
context: .
77+
file: ${{ matrix.dockerfile }}
78+
push: true
79+
platforms: linux/amd64,linux/arm64
80+
tags: ${{ steps.meta.outputs.tags }}
81+
labels: ${{ steps.meta.outputs.labels }}
82+
release:
83+
runs-on: ubuntu-latest
84+
needs:
85+
- build
86+
- configure
87+
permissions:
88+
contents: write
89+
env:
90+
CURRENT_VERSION: ${{ needs.configure.outputs.current_version }}
91+
if: ${{ needs.configure.is_release }}
92+
steps:
93+
- name: Checkout Repository
94+
uses: actions/checkout@v4
95+
- name: Create Release
96+
uses: softprops/action-gh-release@v2
97+
with:
98+
tag_name: v${{ env.CURRENT_VERSION }}

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)