Compare commits
10 Commits
28d2303524
...
0c784ab68e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c784ab68e | ||
|
|
450fd7a0cd | ||
|
|
736040d9f8 | ||
|
|
05a08b9a38 | ||
|
|
27a82b1fd5 | ||
|
|
5977cb1da6 | ||
|
|
7d92f3db45 | ||
|
|
832a7c5927 | ||
|
|
a29dfb59aa | ||
|
|
e48991935c |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,4 @@
|
||||
/data
|
||||
push-event.txt
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
FROM node:16-alpine
|
||||
|
||||
RUN apk add --no-cache docker-cli docker-compose jq yq
|
||||
|
||||
WORKDIR /app
|
||||
ENV NODE_ENV=production
|
||||
|
||||
@@ -7,5 +9,7 @@ COPY ./dist /app/dist
|
||||
COPY ./package.json /app/package.json
|
||||
COPY ./package-lock.json /app/package-lock.json
|
||||
|
||||
RUN npm install
|
||||
|
||||
ENTRYPOINT npm start
|
||||
EXPOSE 3000
|
||||
|
||||
2
Jenkinsfile
vendored
2
Jenkinsfile
vendored
@@ -55,7 +55,7 @@ node {
|
||||
}
|
||||
}
|
||||
|
||||
stage('Cleanup'){
|
||||
stage('Cleanup') {
|
||||
echo 'prune and cleanup'
|
||||
sh 'npm prune'
|
||||
sh 'rm node_modules -rf'
|
||||
|
||||
47
res/push-event.txt
Normal file
47
res/push-event.txt
Normal file
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"path": "/",
|
||||
"headers": {
|
||||
"host": "deployment:8888",
|
||||
"user-agent": "Go-http-client/1.1",
|
||||
"content-length": "450",
|
||||
"authorization": "ThisIsAHeader",
|
||||
"content-type": "application/json",
|
||||
"accept-encoding": "gzip"
|
||||
},
|
||||
"method": "POST",
|
||||
"body": "{\"type\":\"PUSH_ARTIFACT\",\"occur_at\":1642979840,\"operator\":\"admin\",\"event_data\":{\"resources\":[{\"digest\":\"sha256:4c2ce204be06227e55a2580d9be0b0281324ebecd2e5110dc9a708720adb4ab1\",\"tag\":\"latest\",\"resource_url\":\"docker.sebse.de/web-development/node-wannistesvorbei:latest\"}],\"repository\":{\"date_created\":1618826088,\"name\":\"node-wannistesvorbei\",\"namespace\":\"web-development\",\"repo_full_name\":\"web-development/node-wannistesvorbei\",\"repo_type\":\"private\"}}}",
|
||||
"fresh": false,
|
||||
"hostname": "deployment",
|
||||
"ip": "::ffff:192.168.80.3",
|
||||
"ips": [],
|
||||
"protocol": "http",
|
||||
"query": {},
|
||||
"subdomains": [],
|
||||
"xhr": false,
|
||||
"os": {
|
||||
"hostname": "63361c412549"
|
||||
},
|
||||
"connection": {},
|
||||
"json": {
|
||||
"type": "PUSH_ARTIFACT",
|
||||
"occur_at": 1642979840,
|
||||
"operator": "admin",
|
||||
"event_data": {
|
||||
"resources": [
|
||||
{
|
||||
"digest": "sha256:4c2ce204be06227e55a2580d9be0b0281324ebecd2e5110dc9a708720adb4ab1",
|
||||
"tag": "latest",
|
||||
"resource_url": "docker.sebse.de/web-development/node-wannistesvorbei:latest"
|
||||
}
|
||||
],
|
||||
"repository": {
|
||||
"date_created": 1618826088,
|
||||
"name": "node-wannistesvorbei",
|
||||
"namespace": "web-development",
|
||||
"repo_full_name": "web-development/node-wannistesvorbei",
|
||||
"repo_type": "private"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
::ffff:192.168.80.3 - - [23/Jan/2022:23:17:23 +0000] "POST / HTTP/1.1" 200 1632 "-" "Go-http-client/1.1"
|
||||
29
src/index.ts
29
src/index.ts
@@ -1,8 +1,8 @@
|
||||
import express, {Application, ErrorRequestHandler, Request, Response} from 'express'
|
||||
import 'dotenv/config'
|
||||
import * as env from 'env-var'
|
||||
import {PushWebhook} from "./PushWebhook";
|
||||
import {spawn} from "child_process";
|
||||
import { PushWebhook } from "./PushWebhook";
|
||||
import { exec } from "child_process";
|
||||
|
||||
const app: Application = express()
|
||||
|
||||
@@ -16,26 +16,27 @@ const timeouts: Record<string, NodeJS.Timeout> = {}
|
||||
|
||||
async function parsePushHook(webhook: PushWebhook) {
|
||||
const repository: string|undefined = webhook.event_data?.repository?.repo_full_name
|
||||
const tag = webhook.event_data?.resources?.[0]?.tag
|
||||
if (!repository || !tag) {
|
||||
const {tag, resource_url} = webhook.event_data?.resources?.[0] ?? { }
|
||||
if (!repository || !tag || !resource_url) {
|
||||
return
|
||||
}
|
||||
clearTimeout(timeouts[repository])
|
||||
timeouts[repository] = setTimeout(async () => {
|
||||
delete timeouts[repository]
|
||||
const filename = repository
|
||||
const safeFilename = repository
|
||||
.replace(/[^\w\s/]/gi, '-')
|
||||
.replace(/\//gi, '_')
|
||||
const safeTag = tag
|
||||
.replace(/[^a-z0-9-]/gi, '')
|
||||
const safeResourceUri = resource_url
|
||||
.replace(/[^a-z0-9-./:]/gi, '')
|
||||
try {
|
||||
const cp = spawn(`./${filename}.sh`, [], {
|
||||
cwd: rootDir,
|
||||
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
|
||||
});
|
||||
cp.on('message', data => console.log(data))
|
||||
await new Promise((resolve, reject) => {
|
||||
cp.on('close', resolve)
|
||||
cp.on('error', reject)
|
||||
})
|
||||
const cmd = `${rootDir}/${safeFilename}.sh ${safeTag} ${safeResourceUri}`
|
||||
await new Promise(((resolve, reject) => {
|
||||
exec(cmd, (err, stdout, stderr) =>
|
||||
(err || stderr) ? reject(err || stderr) : resolve(stdout)
|
||||
);
|
||||
}))
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user