Skip to content

Commit ad0cc23

Browse files
committed
871: Add update k8s verstion tag CI function
prettier
1 parent 927a8ed commit ad0cc23

File tree

10 files changed

+347
-92
lines changed

10 files changed

+347
-92
lines changed

.github/composite/build-image/action.yml

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,23 @@ name: "Build & Upload Docker Image"
22
description: "Build & (optionally) upload Docker Image to Docker Registry"
33

44
inputs:
5-
GPG_PRIVATE_KEY:
6-
description: "GPG Private Key"
7-
required: true
8-
GPG_PASSPHRASE:
9-
description: "GPG Passphrase"
10-
required: true
115
DOCKER_UPLOAD:
126
description: "Boolean indicating whether the image should be uploaded to Docker registry or not."
137
required: false
14-
default: true
15-
TAG_PREFIX:
16-
description: "Docker tags prefix"
17-
required: false
18-
SERVER_PROFILES:
19-
description: "Profile(s) to apply to Codebloom instance."
20-
required: false
21-
default: prod
8+
default: "true"
9+
ENVIRONMENT:
10+
description: "'staging' or 'production'"
11+
required: true
12+
13+
outputs:
14+
tag:
15+
description: "Built Docker image tag (git SHA, with optional prefix)"
16+
value: ${{ steps.build-image.outputs.tag }}
2217

2318
runs:
2419
using: "composite"
2520
steps:
26-
- name: Setup CI
27-
uses: ./.github/composite/setup-ci
28-
with:
29-
GPG_PRIVATE_KEY: ${{ inputs.GPG_PRIVATE_KEY }}
30-
GPG_PASSPHRASE: ${{ inputs.GPG_PASSPHRASE }}
31-
32-
- name: Set up pnpm
33-
uses: pnpm/action-setup@master
34-
with:
35-
version: 10.24.0
36-
cache: true
37-
cache_dependency_path: js/pnpm-lock.yaml
38-
package_json_file: js/package.json
39-
40-
- name: Set up OpenJDK 25
41-
uses: actions/setup-java@v4
42-
with:
43-
distribution: "temurin"
44-
java-version: "25"
45-
46-
- name: Cache Maven packages
47-
uses: actions/cache@v5
48-
with:
49-
path: |
50-
~/.m2
51-
~/repository
52-
key: ${{ github.job }}-${{ hashFiles('**/pom.xml') }}
53-
54-
- name: Expose GitHub Runtime
55-
uses: crazy-max/ghaction-github-runtime@v3
56-
5721
- name: Run script
22+
id: build-image
5823
shell: bash
59-
run: bun run .github/scripts/build-image --tag-prefix=${{ inputs.TAG_PREFIX }} --docker-upload=${{ inputs.DOCKER_UPLOAD }} --server-profiles=${{ inputs.SERVER_PROFILES }}
24+
run: bun run .github/scripts/build-image --environment "${{ inputs.ENVIRONMENT }}" --docker-upload=${{ inputs.DOCKER_UPLOAD }} --getGhaOutput=true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Deploy Web Service
2+
description: Builds web image and deploys to k8s manifests
3+
4+
inputs:
5+
ENVIRONMENT:
6+
description: "'staging' or 'production'"
7+
required: true
8+
9+
runs:
10+
using: composite
11+
steps:
12+
- name: Build and push web image
13+
id: build-image
14+
shell: bash
15+
run: bun run .github/scripts/build-image --environment "${{ inputs.ENVIRONMENT }}" --docker-upload=true --getGhaOutput=true
16+
17+
- name: Deploy web image tag
18+
shell: bash
19+
run: bun run .github/scripts/deploy --environment "${{ inputs.ENVIRONMENT }}" --newTagVersion "${{ steps.build-image.outputs.tag }}"

.github/scripts/build-image/index.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Environment } from "types";
2+
13
import { $ } from "bun";
24
import { getEnvVariables } from "load-secrets/env/load";
35
import { backend } from "utils/run-backend-instance";
@@ -7,25 +9,33 @@ import { hideBin } from "yargs/helpers";
79

810
process.env.TZ = "America/New_York";
911

10-
const { tagPrefix, dockerUpload, serverProfiles } = await yargs(
11-
hideBin(process.argv),
12-
)
13-
.option("tagPrefix", {
14-
type: "string",
15-
demandOption: true,
16-
})
17-
.option("dockerUpload", {
18-
type: "boolean",
19-
default: false,
20-
demandOption: true,
21-
})
22-
.option("serverProfiles", {
23-
type: "string",
24-
default: "prod",
25-
demandOption: true,
26-
})
27-
.strict()
28-
.parse();
12+
const { environment, dockerUpload, getGhaOutput, githubOutputFile } =
13+
await yargs(hideBin(process.argv))
14+
.option("environment", {
15+
choices: ["staging", "production"] satisfies Environment[],
16+
describe: "Deployment environment (staging or production)",
17+
demandOption: true,
18+
})
19+
.option("dockerUpload", {
20+
type: "boolean",
21+
default: false,
22+
demandOption: true,
23+
})
24+
.option("getGhaOutput", {
25+
type: "boolean",
26+
describe: "Enable GitHub Actions output to receive latest built tag version",
27+
default: false,
28+
})
29+
.option("githubOutputFile", {
30+
type: "string",
31+
describe: "Path to GITHUB_OUTPUT (passed in automatically in CI)",
32+
default: process.env.GITHUB_OUTPUT,
33+
})
34+
.strict()
35+
.parse();
36+
37+
const tagPrefix = environment === "staging" ? "staging-" : "";
38+
const serverProfiles = environment === "staging" ? "stg" : "prod";
2939

3040
async function main() {
3141
try {
@@ -103,6 +113,14 @@ async function main() {
103113
.`;
104114

105115
console.log("Image pushed successfully.");
116+
117+
if (getGhaOutput && githubOutputFile) {
118+
console.log("Outputting image tag...");
119+
const w = Bun.file(githubOutputFile).writer();
120+
await w.write(`tag<<EOF\n${tagPrefix}${gitSha}\nEOF\n`);
121+
await w.flush();
122+
await w.end();
123+
}
106124
} finally {
107125
await backend.end();
108126
await db.end();

.github/scripts/bun.lock

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/scripts/deploy/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { Environment } from "types";
2+
3+
import { getEnvVariables } from "load-secrets/env/load";
4+
import { updateK8sTagWithPR } from "utils/create-k8s-pr";
5+
import yargs from "yargs";
6+
import { hideBin } from "yargs/helpers";
7+
8+
const { environment, newTagVersion } = await yargs(hideBin(process.argv))
9+
.option("newTagVersion", {
10+
type: "string",
11+
demandOption: true,
12+
})
13+
.option("environment", {
14+
choices: ["staging", "production"] satisfies Environment[],
15+
describe: "Deployment environment (staging or production)",
16+
demandOption: true,
17+
})
18+
.strict()
19+
.parse();
20+
21+
async function main() {
22+
const ciEnv = await getEnvVariables(["ci"]);
23+
const { githubPat } = parseCiEnv(ciEnv);
24+
25+
await updateK8sTagWithPR({
26+
githubPat,
27+
kustomizationFilePath: `apps/${environment}/codebloom/kustomization.yaml`,
28+
imageName: "docker.io/tahminator/codebloom",
29+
newTag: newTagVersion,
30+
environment,
31+
});
32+
}
33+
34+
function parseCiEnv(ciEnv: Record<string, string>) {
35+
const githubPat = (() => {
36+
const v = ciEnv["GITHUB_PAT"];
37+
if (!v) {
38+
throw new Error("Missing GITHUB_PAT from .env.ci");
39+
}
40+
return v;
41+
})();
42+
43+
return { githubPat };
44+
}
45+
46+
main()
47+
.then(() => {
48+
process.exit(0);
49+
})
50+
.catch((e) => {
51+
console.error(e);
52+
process.exit(1);
53+
});

.github/scripts/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"coolify": "https://github.com/tahminator/Coolify-TypeScript-SDK#8e19e18635ea2b095a9c39c574d22eefd938441f",
2727
"discord.js": "^14.25.1",
2828
"octokit": "^5.0.5",
29-
"yargs": "^18.0.0"
29+
"yaml": "^2.8.2",
30+
"yargs": "^18.0.0",
31+
"zod": "^4.3.6"
3032
},
3133
"devDependencies": {
3234
"typescript": "^5.9.0",

.github/scripts/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export type Environment = "staging" | "production";
22

33
export type Location = "backend" | "frontend";
4+
5+
export type Type = "web" | "standup-bot";

0 commit comments

Comments
 (0)