Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: willie-yao <[email protected]>
  • Loading branch information
willie-yao committed Jul 23, 2021
0 parents commit 57b80fc
Show file tree
Hide file tree
Showing 28 changed files with 1,514 additions and 0 deletions.
149 changes: 149 additions & 0 deletions .brigade/brigade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { events, Event, Job, ConcurrentGroup, SerialGroup } from "@brigadecore/brigadier"

const releaseTagRegex = /^refs\/tags\/(v[0-9]+(?:\.[0-9]+)*(?:\-.+)?)$/

const goImg = "brigadecore/go-tools:v0.1.0"
const kanikoImg = "brigadecore/kaniko:v0.2.0"
const helmImg = "brigadecore/helm-tools:v0.1.0"
const localPath = "/workspaces/brigade-noisy-neighbor"

// MakeTargetJob is just a job wrapper around a make target.
class MakeTargetJob extends Job {
constructor(target: string, img: string, event: Event, env?: {[key: string]: string}) {
super(target, img, event)
this.primaryContainer.sourceMountPath = localPath
this.primaryContainer.workingDirectory = localPath
this.primaryContainer.environment = env || {}
this.primaryContainer.environment["SKIP_DOCKER"] = "true"
if (event.worker?.git?.ref) {
const matchStr = event.worker.git.ref.match(releaseTagRegex)
if (matchStr) {
this.primaryContainer.environment["VERSION"] = Array.from(matchStr)[1] as string
}
}
this.primaryContainer.command = [ "make" ]
this.primaryContainer.arguments = [ target ]
}
}

// PushImageJob is a specialized job type for publishing Docker images.
class PushImageJob extends MakeTargetJob {
constructor(target: string, event: Event) {
super(target, kanikoImg, event, {
"DOCKER_ORG": event.project.secrets.dockerhubOrg,
"DOCKER_USERNAME": event.project.secrets.dockerhubUsername,
"DOCKER_PASSWORD": event.project.secrets.dockerhubPassword
})
}
}

// A map of all jobs. When a check_run:rerequested event wants to re-run a
// single job, this allows us to easily find that job by name.
const jobs: {[key: string]: (event: Event) => Job } = {}

// Basic tests:

const testUnitJobName = "test-unit"
const testUnitJob = (event: Event) => {
return new MakeTargetJob(testUnitJobName, goImg, event)
}
jobs[testUnitJobName] = testUnitJob

const lintJobName = "lint"
const lintJob = (event: Event) => {
return new MakeTargetJob(lintJobName, goImg, event)
}
jobs[lintJobName] = lintJob

const lintChartJobName = "lint-chart"
const lintChartJob = (event: Event) => {
return new MakeTargetJob(lintChartJobName, helmImg, event)
}
jobs[lintChartJobName] = lintChartJob

// Build / publish stuff:

const buildJobName = "build"
const buildJob = (event: Event) => {
return new MakeTargetJob(buildJobName, kanikoImg, event)
}
jobs[buildJobName] = buildJob

const pushJobName = "push"
const pushJob = (event: Event) => {
return new PushImageJob(pushJobName, event)
}
jobs[pushJobName] = pushJob

const publishChartJobName = "publish-chart"
const publishChartJob = (event: Event) => {
return new MakeTargetJob(publishChartJobName, helmImg, event, {
"HELM_REGISTRY": event.project.secrets.helmRegistry || "ghcr.io",
"HELM_ORG": event.project.secrets.helmOrg,
"HELM_USERNAME": event.project.secrets.helmUsername,
"HELM_PASSWORD": event.project.secrets.helmPassword
})
}
jobs[publishChartJobName] = publishChartJob

// Run the entire suite of tests WITHOUT publishing anything initially. If
// EVERYTHING passes AND this was a push (merge, presumably) to the master
// branch, then publish an "edge" image.
async function runSuite(event: Event): Promise<void> {
await new SerialGroup(
new ConcurrentGroup( // Basic tests
testUnitJob(event),
lintJob(event),
lintChartJob(event)
),
buildJob(event)
).run()
if (event.worker?.git?.ref == "master") {
// Push "edge" images.
//
// npm packages MUST be semantically versioned, so we DON'T publish an
// edge brigadier package.
//
// To keep our github released page tidy, we're also not publishing "edge"
// CLI binaries.
await pushJob(event).run()
}
}

// Either of these events should initiate execution of the entire test suite.
events.on("brigade.sh/github", "check_suite:requested", runSuite)
events.on("brigade.sh/github", "check_suite:rerequested", runSuite)

// This event indicates a specific job is to be re-run.
events.on("brigade.sh/github", "check_run:rerequested", async event => {
// Check run names are of the form <project name>:<job name>, so we strip
// event.project.id.length + 1 characters off the start of the check run name
// to find the job name.
const jobName = JSON.parse(event.payload).check_run.name.slice(event.project.id.length + 1)
const job = jobs[jobName]
if (job) {
await job(event).run()
return
}
throw new Error(`No job found with name: ${jobName}`)
})

// Pushing new commits to any branch in github triggers a check suite. Such
// events are already handled above. Here we're only concerned with the case
// wherein a new TAG has been pushed-- and even then, we're only concerned with
// tags that look like a semantic version and indicate a formal release should
// be performed.
events.on("brigade.sh/github", "push", async event => {
const matchStr = event.worker.git.ref.match(releaseTagRegex)
if (matchStr) {
// This is an official release with a semantically versioned tag
await new SerialGroup(
pushJob(event),
publishChartJob(event)
).run()
} else {
console.log(`Ref ${event.worker.git.ref} does not match release tag regex (${releaseTagRegex}); not releasing.`)
}
})

events.process()
6 changes: 6 additions & 0 deletions .brigade/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "brigade-noisy-neighbor-ci-cd",
"dependencies": {
"@brigadecore/brigadier": "^2.0.0-beta.1"
}
}
19 changes: 19 additions & 0 deletions .brigade/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/brigadecore/brigade/v2/v2/apiserver/schemas/project.json
apiVersion: brigade.sh/v2-beta
kind: Project
metadata:
id: brigade-noisy-neighbor
description: Noisy neighbor for Brigade 2!
spec:
eventSubscriptions:
- source: brigade.sh/github
qualifiers:
repo: brigadecore/brigade-noisy-neighbor
types:
- check_run:rerequested
- check_suite:requested
- check_suite:rerequested
- push
workerTemplate:
git:
cloneURL: https://github.com/brigadecore/brigade-noisy-neighbor.git
15 changes: 15 additions & 0 deletions .brigade/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@brigadecore/brigadier@^2.0.0-beta.1":
version "2.0.0-beta.1"
resolved "https://registry.yarnpkg.com/@brigadecore/brigadier/-/brigadier-2.0.0-beta.1.tgz#8abd9461d445d1d13c83404cec22c16f3cbc2104"
integrity sha512-PoDfSAYMpqXyhTP+DBE7B1xHXtCxuW7SCLcH9/yQyr4kgeKxsAyYvy1baQx1G5ujpWcKX4ULmxf7dsJdHPX+3w==
dependencies:
"@types/node" "^14.14.11"

"@types/node@^14.14.11":
version "14.17.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.17.1.tgz#5e07e0cb2ff793aa7a1b41deae76221e6166049f"
integrity sha512-/tpUyFD7meeooTRwl3sYlihx2BrJE7q9XF71EguPFIySj9B7qgnRtHsHTho+0AUm4m1SvWGm6uSncrR94q6Vtw==
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.git
.gocache

12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.gocache/
coverage.txt
charts/brigade/charts
.DS_Store
.brigade/node_modules/
.brigade/secrets.yaml

# IDEs
.vscode/
.devcontainer/
.idea/
.swp
4 changes: 4 additions & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file is described here: https://help.github.com/en/articles/about-code-owners

# Global Owners: These members are Core Maintainers of Brigade
* @brigadecore/maintainers
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Contributing Guide

See our Brigade project [Contributing Guide](https://github.com/brigadecore/community/blob/master/contributing.md).
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM brigadecore/go-tools:v0.1.0

ARG VERSION
ARG COMMIT
ENV CGO_ENABLED=0

WORKDIR /
COPY . /
COPY go.mod go.mod
COPY go.sum go.sum

RUN go build \
-o bin/noisy-neighbor \
-ldflags "-w -X github.com/brigadecore/brigade-noisy-neighbor/internal/version.version=$VERSION -X github.com/brigadecore/brigade-noisy-neighbor/internal/version.commit=$COMMIT" \
.

FROM scratch
COPY --from=0 /bin/ /brigade-noisy-neighbor/bin/
ENTRYPOINT ["/brigade-noisy-neighbor/bin/noisy-neighbor"]
Loading

0 comments on commit 57b80fc

Please sign in to comment.