Skip to content

Commit

Permalink
Merge branch 'main' into feat/markdown-editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredkiss3 committed Jan 31, 2024
2 parents 96cdb17 + ad8d457 commit e71fef2
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 117 deletions.
2 changes: 1 addition & 1 deletion dc-build-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ while read -r line; do
build_args="$build_args --build-arg $line"
done < .env.docker.local
echo args="'$build_args'"
docker buildx build --push -t dcr.fredkiss.dev/gh-next:dev -f docker/Dockerfile.dev $build_args .
docker buildx build -t dcr.fredkiss.dev/gh-next:dev -f docker/Dockerfile.dev $build_args .
4 changes: 1 addition & 3 deletions docker/docker-stack.prod.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
version: "3.4"

services:
app:
gh-next-prod:
image: dcr.fredkiss.dev/gh-next:latest
ports:
- "8988:3000"
deploy:
replicas: 2
update_config:
Expand Down
50 changes: 4 additions & 46 deletions pr-preview-workflow/add-caddyfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ export async function addCaddyfile(
PR_ID: number,
PR_BRANCH: string,
CADDY_CONFIG_FOLDER_PATH: string,
shouldReloadCaddy: boolean,
MAX_CONFIG_NUMBER: number,
STARTING_PORT_RANGE: number
shouldReloadCaddy: boolean
) {
await $`echo '[🔄 Caddy] adding preview environment config...'`;
const caddyfileToAdd = file(
Expand All @@ -18,58 +16,18 @@ export async function addCaddyfile(
return;
}

const files =
$`ls ${CADDY_CONFIG_FOLDER_PATH} | grep -E '^pull-request\-[0-9]+\.caddy$'`.lines();

let caddyPRFiles = await Array.fromAsync(files).then((arr) =>
arr
.filter(Boolean)
// sort from the lowest pr number to the highest
.sort((a, b) => {
// @ts-expect-error
const numberA = parseInt(a.match(/pull-request-(\d+)\.caddy/)[1], 10);
// @ts-expect-error
const numberB = parseInt(b.match(/pull-request-(\d+)\.caddy/)[1], 10);
return numberA - numberB;
})
);

// don't go over 100 config max
if (caddyPRFiles.length === MAX_CONFIG_NUMBER) {
const [leastRecentPullRequestFile, ...rest] = caddyPRFiles;

const leastRecentPullRequestID = parseInt(
// @ts-expect-error
leastRecentPullRequestFile.match(/pull-request-(\d+)\.caddy/)[1],
10
);

await $`echo '[🔄 Caddy] preview environment config for pull request #${leastRecentPullRequestID} is too old, deleting it...'`;
await $`echo '[ℹ️ Caddy] you can still redeploy this env by deploying the associated pull request'`;

// delete the least recent Pull Request
await $`rm '${pathWithoutSlash(
CADDY_CONFIG_FOLDER_PATH
)}/${leastRecentPullRequestFile}'`;

await $`echo '[✅ Caddy] preview environment config for pull request #${leastRecentPullRequestID} deleted'`;

caddyPRFiles = rest;
}

const portNumber = STARTING_PORT_RANGE + caddyPRFiles.length;
const CADDY_TEMPLATE_CONTENT = `gh-${PR_ID}.gh.fredkiss.dev, gh-${PR_BRANCH}.gh.fredkiss.dev {
route {
sablier http://localhost:10000 {
sablier {
group gh-next-${PR_ID}
session_duration 30m
dynamic {
theme ghost
display_name preview environment ${PR_BRANCH} (pull request: ${PR_ID})
display_name preview environment ${PR_BRANCH} (pull request ID: ${PR_ID})
refresh_frequency 5s
}
}
reverse_proxy 127.0.0.1:${portNumber} {
reverse_proxy http://gh-next-${PR_ID}:3000 {
header_up Host {http.request.host}
# disables buffering
flush_interval -1
Expand Down
56 changes: 1 addition & 55 deletions pr-preview-workflow/add-docker-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { $, file, write } from "bun";

export async function addDockerApp(
PR_ID: number,
shouldReloadDockerStack: boolean,
MAX_OPEN_PORTS: number,
STARTING_PORT_RANGE: number
shouldReloadDockerStack: boolean
) {
await $`echo '[🔄 Docker] adding docker stack config...'`;
const COMPOSE_FILE_PATH = `./docker/docker-stack.pr-${PR_ID}.yaml`;
Expand All @@ -27,65 +25,13 @@ export async function addDockerApp(
return;
}

const files =
$`ls ./docker | grep -E '^docker-stack\.pr\-[0-9]+\.yaml$'`.lines();

let stackPRFiles = await Array.fromAsync(files).then((arr) =>
arr
.filter(Boolean)
// sort from the lowest pr number to the highest
.sort((a, b) => {
const numberA = parseInt(
// @ts-expect-error
a.match(/^docker-stack\.pr\-(\d+)\.yaml$/)[1],
10
);
const numberB = parseInt(
// @ts-expect-error
b.match(/^docker-stack\.pr\-(\d+)\.yaml$/)[1],
10
);
return numberA - numberB;
})
);

// don't go over 100 config max
if (stackPRFiles.length === MAX_OPEN_PORTS) {
const [leastRecentPullRequestFile, ...rest] = stackPRFiles;

const leastRecentPullRequestID = parseInt(
// @ts-expect-error
leastRecentPullRequestFile.match(/^docker-stack\.pr\-(\d+)\.yaml$/)[1],
10
);

await $`echo '[🔄 Docker] docker stack config for pull request #${leastRecentPullRequestID} is too old, deleting it...'`;
await $`echo '[ℹ️ Docker] you can still recreate it by deploying the associated pull request'`;

await $`echo '[🔄 Docker] Removing associated docker stack services...'`;
await $`docker stack rm gh-stack-pr-${PR_ID}`;
await $`echo '[✅ Docker] associated docker stack services removed succesfully'`;

// delete the least recent Pull Request
await $`rm './docker/${leastRecentPullRequestFile}'`;

// delete the least recent Pull Request
await $`echo '[✅ Docker] docker stack config for pull request #${leastRecentPullRequestID} deleted'`;

stackPRFiles = rest;
}

const portNumber = STARTING_PORT_RANGE + stackPRFiles.length;

// Placeholder text in the Docker Compose file
const DOCKER_STACK_TEMPLATE = `# service configuration for pull request #132
version: "3.4"
services:
gh-next-${PR_ID}:
image: dcr.fredkiss.dev/gh-next:pr-${PR_ID}
ports:
- "${portNumber}:3000"
deploy:
replicas: 0
update_config:
Expand Down
14 changes: 2 additions & 12 deletions pr-preview-workflow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,10 @@ const {
"reload-docker": shouldReloadDockerStack
} = argSchema.parse(values);

const MAX_OPEN_PORTS = 100,
STARTING_PORT_RANGE = 9100;

await addDockerApp(
PR_ID,
shouldReloadDockerStack,
MAX_OPEN_PORTS,
STARTING_PORT_RANGE
);
await addDockerApp(PR_ID, shouldReloadDockerStack);
await addCaddyfile(
PR_ID,
PR_BRANCH,
CADDY_CONFIG_FOLDER_PATH,
shouldReloadCaddy,
MAX_OPEN_PORTS,
STARTING_PORT_RANGE
shouldReloadCaddy
);

0 comments on commit e71fef2

Please sign in to comment.