Skip to content

Commit d1a53ff

Browse files
committed
Add script to check Docker Compose files and GitHub workflow (#12)
1 parent 4fb9de8 commit d1a53ff

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

.github/workflows/check.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
name: Check
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
check:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: denoland/setup-deno@v1
12+
with:
13+
deno-version: v1.x
14+
- run: ./src/check.ts

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ services/webhook-handler
2929
!/.gitignore
3030
!/README.md
3131
!/SETUP.md
32+
33+
!.github

src/check.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env -S COLI_CONC_BASE=. deno run --allow-env --allow-read --allow-sys --ext=ts
2+
3+
/**
4+
* Checks docker-compose.yml files for issues:
5+
*
6+
* - Duplicate service names
7+
*
8+
* Ideas for other issues to check:
9+
*
10+
* - restart directive
11+
* - Docker networks
12+
* - volume paths
13+
*/
14+
15+
Deno.env.set("FORCE_COLOR", "2")
16+
import { existsSync } from "https://deno.land/std/fs/mod.ts"
17+
18+
// Determine available targets by reading docker-compose.yml files in service subfolders
19+
import { parse as parseYaml } from "https://deno.land/[email protected]/yaml/mod.ts"
20+
import { getEnv } from "../src/utils.ts"
21+
const { servicePath } = getEnv("")
22+
23+
const serviceNamesToComposeFiles = {}
24+
let ok = true
25+
26+
for await (const { name, isDirectory } of Deno.readDir(servicePath)) {
27+
if (!isDirectory) {
28+
continue
29+
}
30+
try {
31+
// Try to read docker-compose.yml
32+
const file = `${servicePath}/${name}/docker-compose.yml`
33+
const fileShort = file.replace(servicePath, "")
34+
const compose = parseYaml(await Deno.readTextFile(file))
35+
const errors: String[] = []
36+
for (const service of Object.keys(compose?.services || {})) {
37+
if (serviceNamesToComposeFiles[service]) {
38+
errors.push(`!!! Service "${service}" is already defined in ${serviceNamesToComposeFiles[service]}`)
39+
ok = false
40+
} else {
41+
serviceNamesToComposeFiles[service] = fileShort
42+
}
43+
}
44+
if (errors.length) {
45+
console.error(`%c${fileShort}`, "color: red")
46+
errors.forEach(error => console.error(`%c ${error}`, "color: red"))
47+
} else {
48+
console.log(`%c${fileShort}`, "color: green")
49+
console.log("%c All good.", "color: green")
50+
}
51+
} catch (_) {
52+
// Just ignore errors as we are expecting them
53+
}
54+
}
55+
56+
if (!ok) {
57+
Deno.exit(1)
58+
}

0 commit comments

Comments
 (0)