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
|
/data
|
||||||
push-event.txt
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
FROM node:16-alpine
|
FROM node:16-alpine
|
||||||
|
|
||||||
|
RUN apk add --no-cache docker-cli docker-compose jq yq
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
ENV NODE_ENV=production
|
ENV NODE_ENV=production
|
||||||
|
|
||||||
@@ -7,5 +9,7 @@ COPY ./dist /app/dist
|
|||||||
COPY ./package.json /app/package.json
|
COPY ./package.json /app/package.json
|
||||||
COPY ./package-lock.json /app/package-lock.json
|
COPY ./package-lock.json /app/package-lock.json
|
||||||
|
|
||||||
|
RUN npm install
|
||||||
|
|
||||||
ENTRYPOINT npm start
|
ENTRYPOINT npm start
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|||||||
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"
|
||||||
27
src/index.ts
27
src/index.ts
@@ -2,7 +2,7 @@ import express, {Application, ErrorRequestHandler, Request, Response} from 'expr
|
|||||||
import 'dotenv/config'
|
import 'dotenv/config'
|
||||||
import * as env from 'env-var'
|
import * as env from 'env-var'
|
||||||
import { PushWebhook } from "./PushWebhook";
|
import { PushWebhook } from "./PushWebhook";
|
||||||
import {spawn} from "child_process";
|
import { exec } from "child_process";
|
||||||
|
|
||||||
const app: Application = express()
|
const app: Application = express()
|
||||||
|
|
||||||
@@ -16,26 +16,27 @@ const timeouts: Record<string, NodeJS.Timeout> = {}
|
|||||||
|
|
||||||
async function parsePushHook(webhook: PushWebhook) {
|
async function parsePushHook(webhook: PushWebhook) {
|
||||||
const repository: string|undefined = webhook.event_data?.repository?.repo_full_name
|
const repository: string|undefined = webhook.event_data?.repository?.repo_full_name
|
||||||
const tag = webhook.event_data?.resources?.[0]?.tag
|
const {tag, resource_url} = webhook.event_data?.resources?.[0] ?? { }
|
||||||
if (!repository || !tag) {
|
if (!repository || !tag || !resource_url) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
clearTimeout(timeouts[repository])
|
clearTimeout(timeouts[repository])
|
||||||
timeouts[repository] = setTimeout(async () => {
|
timeouts[repository] = setTimeout(async () => {
|
||||||
delete timeouts[repository]
|
delete timeouts[repository]
|
||||||
const filename = repository
|
const safeFilename = repository
|
||||||
.replace(/[^\w\s/]/gi, '-')
|
.replace(/[^\w\s/]/gi, '-')
|
||||||
.replace(/\//gi, '_')
|
.replace(/\//gi, '_')
|
||||||
|
const safeTag = tag
|
||||||
|
.replace(/[^a-z0-9-]/gi, '')
|
||||||
|
const safeResourceUri = resource_url
|
||||||
|
.replace(/[^a-z0-9-./:]/gi, '')
|
||||||
try {
|
try {
|
||||||
const cp = spawn(`./${filename}.sh`, [], {
|
const cmd = `${rootDir}/${safeFilename}.sh ${safeTag} ${safeResourceUri}`
|
||||||
cwd: rootDir,
|
await new Promise(((resolve, reject) => {
|
||||||
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
|
exec(cmd, (err, stdout, stderr) =>
|
||||||
});
|
(err || stderr) ? reject(err || stderr) : resolve(stdout)
|
||||||
cp.on('message', data => console.log(data))
|
);
|
||||||
await new Promise((resolve, reject) => {
|
}))
|
||||||
cp.on('close', resolve)
|
|
||||||
cp.on('error', reject)
|
|
||||||
})
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user