Skip to content

Commit

Permalink
Fix artifact parsing for goreleaser (#14822)
Browse files Browse the repository at this point in the history
* Fix artifact parsing for goreleaser

* Add label trigger to PR

* Add additional logging

* Handle testing published images

* Reduce logging

* Add QEMU for merge cross arch testing
  • Loading branch information
HenryNguyen5 committed Oct 29, 2024
1 parent d194d21 commit 7f2c953
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .github/actions/goreleaser-build-sign-publish/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ inputs:
runs:
using: composite
steps:
- # We need QEMU to test the cross architecture builds after they're built.
name: Set up QEMU
uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0
- name: Setup docker buildx
uses: docker/setup-buildx-action@2b51285047da1547ffb1b2203d8be4c0af6b1f20 # v3.2.0
- name: Setup go
Expand Down
58 changes: 42 additions & 16 deletions .github/actions/goreleaser-build-sign-publish/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ function main() {
);
}

const artifactsJsonPath = findArtifactsJson();
const dockerImages = extractDockerImages(artifactsJsonPath);
const artifacts = getArtifacts();
const dockerImages = extractDockerImages(artifacts);
const repoSha = execSync("git rev-parse HEAD", { encoding: "utf-8" }).trim();

const results = dockerImages.map((image) => {
try {
console.log(`Checking version for image: ${image}, expected version: ${chainlinkVersion}, expected SHA: ${repoSha}`);
console.log(
`Checking version for image: ${image}, expected version: ${chainlinkVersion}, expected SHA: ${repoSha}`
);
const versionOutput = execSync(`docker run --rm ${image} --version`, {
encoding: "utf-8",
});
console.log(`Output from image ${image}: ${versionOutput}`);

const cleanedOutput = versionOutput.replace("chainlink version ", "").trim();
const cleanedOutput = versionOutput
.replace("chainlink version ", "")
.trim();
const [version, sha] = cleanedOutput.split("@");
if (!version || !sha) {
throw new Error("Version or SHA not found in output.");
Expand Down Expand Up @@ -94,7 +98,7 @@ function printSummary(results) {
}
}

function findArtifactsJson() {
function getArtifacts() {
const distDir = path.resolve(process.cwd(), "dist");
const files = [];

Expand All @@ -103,8 +107,17 @@ function findArtifactsJson() {
for (const item of items) {
const fullPath = path.join(dir, item.name);
if (item.isDirectory()) {
findJsonFiles(fullPath);
// Skip child directories if an artifacts.json exists in the current directory
const parentArtifacts = path.join(dir, "artifacts.json");
if (fs.existsSync(parentArtifacts)) {
console.log(
`Skipping child directory: ${fullPath} because a parent artifacts.json exists at: ${parentArtifacts}`
);
} else {
findJsonFiles(fullPath);
}
} else if (item.isFile() && item.name === "artifacts.json") {
console.log(`Found artifacts.json at: ${fullPath}`);
files.push(fullPath);
}
}
Expand All @@ -115,20 +128,33 @@ function findArtifactsJson() {
if (files.length === 0) {
console.error("Error: No artifacts.json found in /dist.");
process.exit(1);
} else if (files.length > 1) {
console.error("Error: Multiple artifacts.json files found.");
process.exit(1);
}

return files[0];
}
// Merge all artifacts.json files into one
let mergedArtifacts = [];

function extractDockerImages(artifactsJsonPath) {
console.log(`Reading artifacts.json from: ${artifactsJsonPath}`);
const artifactsJson = JSON.parse(fs.readFileSync(artifactsJsonPath, "utf-8"));
for (const file of files) {
const artifactsJson = JSON.parse(fs.readFileSync(file, "utf-8"));
mergedArtifacts = mergedArtifacts.concat(artifactsJson);
}

// Remove duplicate Docker images based on the artifact name
const uniqueArtifacts = Array.from(
new Map(
mergedArtifacts.map((artifact) => [artifact.name, artifact])
).values()
);

return uniqueArtifacts;
}

const dockerImages = artifactsJson
.filter((artifact) => artifact.type === "Docker Image")
function extractDockerImages(artifacts) {
const dockerImages = artifacts
.filter(
(artifact) =>
artifact.type === "Docker Image" ||
artifact.type === "Published Docker Image"
)
.map((artifact) => artifact.name);

if (dockerImages.length === 0) {
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/build-publish-develop-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ name: "Build and Publish GoReleaser"

on:
pull_request:
# The default types are opened, synchronize, and reopened
# See https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request
# We add a label trigger too, since when the build-publish label is added to a PR, we want to build and publish
types:
- opened
- synchronize
- reopened
- labeled
push:
branches:
- develop
Expand Down

0 comments on commit 7f2c953

Please sign in to comment.