Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: catch execution events #507

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = {
"no-process-exit": [0],
"node/no-unsupported-features/es-syntax": [0],
"node/shebang": [0],
"no-ex-assign": [0],
"import/no-commonjs": [0],
"import/no-dynamic-require": [0],
"import/no-extraneous-dependencies": noExtraneousRule,
Expand Down
4 changes: 4 additions & 0 deletions packages/common/config/load-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const os = require("os")
const path = require("path")
const { mkdtemp } = require("fs/promises")
const { v4: uuidv4 } = require("uuid")

const { satisfies } = require("compare-versions")
const fs = require("fs-extra")
Expand Down Expand Up @@ -241,6 +242,9 @@ const loadConfig = async (
env: "KS_FORCE_NEW_DEPLOY",
envParser: envParserYaml,
},
pipelineUUID: {
defaultFunction: () => uuidv4(),
},
pipelineId: {
defaultFunction: (config) => {
const {
Expand Down
1 change: 1 addition & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"slugify": "^1.6.5",
"sonic-boom": "^3.0.0",
"tiged": "^2.12.4",
"uuid": "^10.0.0",
"which": "^3.0.0",
"yaml": "^2.3.1",
"zx": "^7.1.1"
Expand Down
6 changes: 4 additions & 2 deletions packages/common/utils/flatten-aggregate-error.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const indent = require("./indent")

module.exports = (aggregateError) =>
new Error(
module.exports = (aggregateError) => {
console.log("aggregateError", aggregateError)
return new Error(
`${aggregateError.name} ${aggregateError.message}: \n${indent(
aggregateError.errors
.map((error) => `${error.stack.toString()}`)
.join("\n"),
2
)}`
)
}
9 changes: 7 additions & 2 deletions packages/kontinuous/src/build/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ const loadDependencies = require("./load-dependencies")

module.exports = async (_options = {}) => {
const config = ctx.require("config")
const logger = ctx.require("logger")

const { buildPath, buildProjectPath, workspaceKsPath } = config
logger.info("🌀 [LIFECYCLE]: pre-build")
// TODO:

const logger = ctx.require("logger")
const { buildPath, buildProjectPath, workspaceKsPath } = config

if (await fs.pathExists(workspaceKsPath)) {
await fs.copy(workspaceKsPath, buildProjectPath, {
Expand Down Expand Up @@ -77,6 +79,9 @@ module.exports = async (_options = {}) => {

await debugManifests(manifests, values)

logger.info("🌀 [LIFECYCLE]: post-build")
// TODO:

return {
manifestsFile,
manifests: manifestsDump,
Expand Down
13 changes: 12 additions & 1 deletion packages/kontinuous/src/build/validate-manifests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,16 @@ module.exports = async (manifests) => {
logger.info("🌀 [LIFECYCLE]: validators")
const context = createContext({ type: "validators", ValidationError })
const { buildProjectPath } = config
await pluginFunction(`${buildProjectPath}/validators`)(manifests, {}, context)
try {
await pluginFunction(`${buildProjectPath}/validators`)(
manifests,
{},
context
)
} catch (err) {
if (!(err instanceof ValidationError)) {
err = new ValidationError(err.message)
}
throw err
}
}
2 changes: 2 additions & 0 deletions plugins/fabrique/kontinuous.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ patches:
repositoryUrl: https://github.com/SocialGouv/infra-resources.git

dependencies:
kontinuous-ui:
import: socialgouv/kontinuous/plugins/kontinuous-ui
contrib:
import: socialgouv/kontinuous/plugins/contrib

Expand Down
1 change: 1 addition & 0 deletions plugins/kontinuous-ui/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
Binary file added plugins/kontinuous-ui/.yarn/install-state.gz
Binary file not shown.
109 changes: 109 additions & 0 deletions plugins/kontinuous-ui/deploy-sidecars/kontinuous-ui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const { setTimeout: sleep } = require("timers/promises")
const supabase = require("../lib/supabase")

module.exports = async (_options, { config, dryRun, ctx }) => {
if (dryRun) {
return
}

const {
gitSha,
projectName,
environment,
gitBranch,
repositoryName,
gitRepositoryUrl,
pipelineUUID,
} = config

const values = {
uuid: pipelineUUID,
commit_hash: gitSha,
project: projectName,
environment,
branch: gitBranch,
repository: repositoryName,
repository_url: gitRepositoryUrl,
}

const eventsBucket = ctx.require("eventsBucket")
// const abortController = ctx.require("abortController")

// resource:waiting
// resource:failed
// resource:ready
// resource:closed

async function insertValues(data) {
const { error } = await supabase.from("deployments_logs").insert([data])
if (error) throw error
}

const waitingFor = []
eventsBucket.on("resource:waiting", () => {
waitingFor.push(
new Promise(async (resolve, reject) => {
try {
const valuesToInsert = { ...values, status: "waiting" }
await insertValues(valuesToInsert)
resolve()
} catch (e) {
reject(e)
}
// await
})
)
})

eventsBucket.on("resource:ready", () => {
waitingFor.push(
new Promise(async (resolve, reject) => {
try {
const valuesToInsert = { ...values, status: "ready" }
await insertValues(valuesToInsert)
resolve()
} catch (e) {
reject(e)
}
// await
})
)
})

eventsBucket.on("resource:failed", () => {
waitingFor.push(
new Promise(async (resolve, reject) => {
try {
const valuesToInsert = { ...values, status: "failed" }
await insertValues(valuesToInsert)
resolve()
} catch (e) {
reject(e)
}
// await
})
)
})

let finished
eventsBucket.on("deploy-with:finish", () => {
finished = true
})

return new Promise(async (res, rej) => {
while (true) {

Check warning on line 94 in plugins/kontinuous-ui/deploy-sidecars/kontinuous-ui.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected constant condition
if (finished) {
console.log("WAITING FOR...")
try {
await Promise.all(waitingFor)
res()
} catch (err) {
console.log("ERROR", err)
rej(err)
}
return
}
await sleep(1)
}
})
}
Empty file.
7 changes: 7 additions & 0 deletions plugins/kontinuous-ui/lib/supabase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { createClient } = require("@supabase/supabase-js")

Check failure on line 1 in plugins/kontinuous-ui/lib/supabase.js

View workflow job for this annotation

GitHub Actions / Lint

Unable to resolve path to module '@supabase/supabase-js'

Check failure on line 1 in plugins/kontinuous-ui/lib/supabase.js

View workflow job for this annotation

GitHub Actions / Lint

"@supabase/supabase-js" is not found

const { SUPABASE_URL: supabaseUrl, SUPABASE_KEY: supabaseKey } = process.env

const supabase = createClient(supabaseUrl, supabaseKey)

module.exports = supabase
13 changes: 13 additions & 0 deletions plugins/kontinuous-ui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "~kontinuous-ui",
"dependencies": {
"@socialgouv/parse-manifests": "^1.16.4",
"@supabase/supabase-js": "2.45.4"
},
"license": "MIT",
"private": true,
"packageManager": "[email protected]",
"engines": {
"node": "^16.17 || ^18 || ^20"
}
}
30 changes: 30 additions & 0 deletions plugins/kontinuous-ui/pre-deploy/01-deployment_event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const supabase = require("../lib/supabase")

module.exports = async (_manifests, _options, { config, logger }) => {
const {
gitSha,
projectName,
environment,
gitBranch,
repositoryName,
gitRepositoryUrl,
pipelineUUID,
} = config

const values = {
uuid: pipelineUUID,
status: "pre-deploy",
commit_hash: gitSha,
project: projectName,
environment,
branch: gitBranch,
repository: repositoryName,
repository_url: gitRepositoryUrl,
}

const { error } = await supabase.from("deployments_logs").insert([values])

if (error) throw error

logger.info(values, "FINISHED")
}
Empty file.
Loading
Loading