Skip to content

Commit

Permalink
Add script to check Docker Compose files and GitHub workflow (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Oct 9, 2024
1 parent 4fb9de8 commit d1a53ff
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

name: Check

on: [push, pull_request]

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: ./src/check.ts
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ services/webhook-handler
!/.gitignore
!/README.md
!/SETUP.md

!.github
58 changes: 58 additions & 0 deletions src/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env -S COLI_CONC_BASE=. deno run --allow-env --allow-read --allow-sys --ext=ts

/**
* Checks docker-compose.yml files for issues:
*
* - Duplicate service names
*
* Ideas for other issues to check:
*
* - restart directive
* - Docker networks
* - volume paths
*/

Deno.env.set("FORCE_COLOR", "2")
import { existsSync } from "https://deno.land/std/fs/mod.ts"

// Determine available targets by reading docker-compose.yml files in service subfolders
import { parse as parseYaml } from "https://deno.land/[email protected]/yaml/mod.ts"
import { getEnv } from "../src/utils.ts"
const { servicePath } = getEnv("")

const serviceNamesToComposeFiles = {}
let ok = true

for await (const { name, isDirectory } of Deno.readDir(servicePath)) {
if (!isDirectory) {
continue
}
try {
// Try to read docker-compose.yml
const file = `${servicePath}/${name}/docker-compose.yml`
const fileShort = file.replace(servicePath, "")
const compose = parseYaml(await Deno.readTextFile(file))
const errors: String[] = []
for (const service of Object.keys(compose?.services || {})) {
if (serviceNamesToComposeFiles[service]) {
errors.push(`!!! Service "${service}" is already defined in ${serviceNamesToComposeFiles[service]}`)
ok = false
} else {
serviceNamesToComposeFiles[service] = fileShort
}
}
if (errors.length) {
console.error(`%c${fileShort}`, "color: red")
errors.forEach(error => console.error(`%c ${error}`, "color: red"))
} else {
console.log(`%c${fileShort}`, "color: green")
console.log("%c All good.", "color: green")
}
} catch (_) {
// Just ignore errors as we are expecting them
}
}

if (!ok) {
Deno.exit(1)
}

0 comments on commit d1a53ff

Please sign in to comment.