Skip to content

Commit

Permalink
Merge pull request #2549 from newrelic/andrew/NR-268466-main-validation
Browse files Browse the repository at this point in the history
feat: Validation and release on main push
  • Loading branch information
Andrew Anguiano authored Sep 11, 2024
2 parents 75df800 + b8df1d8 commit 77df649
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 28 deletions.
12 changes: 9 additions & 3 deletions .github/actions/build-validate-artifact/action.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
name: Build and Validate Artifact
description: Reusable action to build and validate the component artifact
inputs:
output-artifact:
description: 'If provided, the action will output the artifact to the build directory'
required: false
runs:
using: composite
steps:
- name: Setup workspace
uses: "./.github/actions/bootstrap"
- name: Build & Validate Artifact
shell: bash
id: build-validate-artifact
run: |
cd utils && yarn build-validate-quickstart-artifact
cd utils && yarn build-validate-quickstart-artifact ${{ inputs.output-artifact && '--output-artifact' || '' }}
- name: Debugging output
shell: bash
if: ${{ runner.debug }}
run: ls -la utils/build
16 changes: 16 additions & 0 deletions .github/actions/bump-version-tag/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Bump version tag
description: Bumps the version tag based on the latest version
outputs:
tag:
description: The new tag prefixed with `v`
value: ${{ steps.versions.outputs.v-version }}
runs:
using: composite
steps:
- name: Get next version
uses: reecetech/version-increment@a5d65984a6c6e8bbc30ece69d53d96e40736f24c
id: versions
with:
scheme: semver
increment: minor
use_api: true
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ name: Validate PR Artifact

on:
pull_request:
push:
branches:
- main

jobs:
validate-pr-artifact:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
with:
ref: ${{ github.sha }}
- name: Setup workspace
uses: "./.github/actions/bootstrap"
- name: Validate PR Artifact
uses: "./.github/actions/build-validate-artifact"
# add another job for creating/releasing artifact if it's a push to main here
50 changes: 50 additions & 0 deletions .github/workflows/release-artifact.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# This workflow runs when a tag is created with the `v` prefix or a push
# to main occurs. It runs our validation script, outputs the artifact,
# creates a new tag, Github release, and uploads/associates the artifact with the release.

name: Release artifact
on:
push:
branches:
- main
tags:
- 'v*'

# Permissions for attestation
permissions:
id-token: write
contents: write
attestations: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
with:
ref: ${{ github.ref }}
- name: Setup workspace
uses: "./.github/actions/bootstrap"
- name: Validate and generate artifact
uses: "./.github/actions/build-validate-artifact"
with:
output-artifact: 'true'
- name: Attest artifact
uses: actions/attest-build-provenance@1c608d11d69870c2092266b3f9a6f3abbf17002c
with:
subject-path: utils/build/*
- name: Create new release tag
# Only run if this is a branch push (we'll need a new tag)
if: ${{ startsWith(github.ref, 'refs/heads') }}
id: version
uses: "./.github/actions/bump-version-tag"
- name: Create release
uses: softprops/action-gh-release@c062e08bd532815e2082a85e87e3ef29c3e6d191
with:
files: utils/build/*
fail_on_unmatched_files: true
make_latest: true
# If we have a tag output from the previous step, use that as the tag
# name, otherwise use the current ref which should be the tag.
tag_name: ${{ steps.version.outputs.tag || github.ref }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
utils/node_modules
utils/newrelic_agent.log
utils/build

# IDE files
.DS_Store
Expand All @@ -17,4 +18,4 @@ snapshots/
yarn.lock
.yarn-integrity
yarn-error.log
utils/yarn-error.log
utils/yarn-error.log
59 changes: 40 additions & 19 deletions utils/build-validate-quickstart-artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Dashboard, { DashboardConfig } from "./lib/Dashboard";
import Ajv, { type ErrorObject } from 'ajv';
import { QuickstartConfig, QuickstartConfigAlert } from './types/QuickstartConfig';
import { DataSourceConfig } from './types/DataSourceConfig';
import { passedProcessArguments } from './lib/helpers';

type ArtifactSchema = Record<string, unknown>;

Expand Down Expand Up @@ -74,7 +75,7 @@ export const validateArtifact = (schema: ArtifactSchema, artifact: Artifact): Er
return ajv.errors ?? [];
}

const main = () => {
const main = (shouldOutputArtifact: boolean = false) => {
const schema = getSchema('./schema/artifact.json');
const components = getArtifactComponents();
const dataSourceIds = getDataSourceIds('./schema/core-datasource-ids.json', components.dataSources);
Expand All @@ -88,6 +89,25 @@ const main = () => {
}

console.log('[*] Validation succeeded');

if (shouldOutputArtifact) {
outputArtifact(artifact);
}
}

const outputArtifact = (artifact: Artifact) => {
console.log('[*] Outputting the artifact');
try {
fs.mkdirSync('./build', { recursive: true });

// Dump the artifact to artifact.json
fs.writeFileSync('./build/artifact.json', JSON.stringify(artifact));
// Copy the schema to schema.json
fs.copyFileSync('./schema/artifact.json', './build/schema.json');
} catch (e) {
console.error('Error writing artifact to file:', e);
process.exit(1);
}
}

const getInvalidItems = (errors: ErrorObject[], artifact: ArtifactSchema): InvalidItem[] => {
Expand All @@ -108,25 +128,26 @@ const getInvalidItems = (errors: ErrorObject[], artifact: ArtifactSchema): Inval
}

const printErrors = (invalidItems: InvalidItem[]): void => {
console.error('*** Validation failed. See errors below. ***');
console.error('--------------------------------------------');

invalidItems.forEach(({ value, component, error }, idx) => {
console.error(`Error #${idx + 1}:`, error);
console.error('');
console.error('Received value:', value);

console.error('');
if (component !== value) {
console.error('Invalid component:', component);
}

if (idx < invalidItems.length - 1) {
console.error('************************************');
}
});
console.error('*** Validation failed. See errors below. ***');
console.error('--------------------------------------------');

invalidItems.forEach(({ value, component, error }, idx) => {
console.error(`Error #${idx + 1}:`, error);
console.error('');
console.error('Received value:', value);

console.error('');
if (component !== value) {
console.error('Invalid component:', component);
}

if (idx < invalidItems.length - 1) {
console.error('************************************');
}
});
}

if (require.main === module) {
main();
const shouldOutputArtifact = passedProcessArguments().includes('--output-artifact');
main(shouldOutputArtifact);
}

0 comments on commit 77df649

Please sign in to comment.