Skip to content

Commit

Permalink
ci/cd-fix (#33)
Browse files Browse the repository at this point in the history
* ci: fix typo

* ci: cd terraform
  • Loading branch information
Alxandr authored Oct 10, 2024
1 parent 5916aab commit 45b370d
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 38 deletions.
64 changes: 48 additions & 16 deletions .github/scripts/_meta.mts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,38 @@ const enqueue = <T extends unknown>(fn: () => Promise<T>): Promise<T> => {
return task;
};

const dotnetImageSchema = z.object({
type: z.literal("dotnet"),
source: z.string().optional(),
name: z.string().optional(),
});

const dockerImageSchema = z.object({
type: z.literal("docker"),
source: z.string().optional(),
name: z.string().min(5),
});

const imageSchema = z
.object({
type: z.enum(["dotnet", "docker"]).default("dotnet"),
})
.passthrough()
.pipe(z.discriminatedUnion("type", [dotnetImageSchema, dockerImageSchema]));

const terraformSchema = z.object({
stateFile: z.string().min(10),
});

const infraSchema = z.object({
terraform: terraformSchema.optional(),
});

const configSchema = z.object({
name: z.string().optional(),
shortName: z.string().optional(),
image: z
.object({
name: z.string().optional(),
type: z.enum(["dotnet", "docker"]).default("dotnet"),
source: z.string().optional(),
})
.refine((v) => {
if (v.type === "docker" && !v.name) {
return { message: "Image name is required for docker images" };
}
})
.optional(),
image: imageSchema.optional(),
infra: infraSchema.optional(),
});

export type VerticalType = "app" | "lib" | "pkg";
Expand All @@ -40,13 +57,22 @@ export type ImageInfo = {
readonly source: string;
};

export type TerraformInfo = {
readonly stateFile: string;
};

export type InfraInfo = {
readonly terraform?: TerraformInfo;
};

export type Vertical = {
readonly type: VerticalType;
readonly name: string;
readonly shortName: string;
readonly path: string;
readonly relPath: string;
readonly image?: ImageInfo;
readonly infra?: InfraInfo;
};

const vertialDirs = {
Expand All @@ -68,7 +94,7 @@ const readVertical = async (
let parsed: any = {};
try {
const json = await fs.readFile(configPath, { encoding: "utf-8" });
const parsed = JSON.parse(json);
parsed = JSON.parse(json);
} catch (e) {}

const result = await configSchema.safeParseAsync(parsed);
Expand All @@ -90,9 +116,9 @@ const readVertical = async (
shortName = config.shortName;
}

let image: ImageInfo | undefined;
let image: ImageInfo | undefined = void 0;
let infra: InfraInfo | undefined = void 0;
if (type === "app") {
$.cwd = verticalPath;
const confImage = config.image ?? { type: "dotnet" };

switch (confImage.type) {
Expand Down Expand Up @@ -120,10 +146,15 @@ const readVertical = async (
}

default: {
throw new Error(`Unsupported image type: ${confImage.type}`);
throw new Error(`Unsupported image type: ${(confImage as any).type}`);
}
}

const confInfra = config.infra;
if (confInfra) {
infra = confInfra as InfraInfo;
}

image = confImage as ImageInfo;
}

Expand All @@ -134,6 +165,7 @@ const readVertical = async (
path: verticalPath,
relPath: dirPath,
image,
infra,
};
};

Expand Down
3 changes: 2 additions & 1 deletion .github/scripts/get-metadata.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { verticals } from "./_meta.mts";
import { inspect } from "node:util";

console.log(verticals);
console.log(inspect(verticals, { depth: 10, colors: true }));
9 changes: 9 additions & 0 deletions .github/scripts/list-verticals.mts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ var matrix = {
ret.imageName = v.image.name;
}

if (v.infra) {
ret.infra = "true";

if (v.infra.terraform) {
ret.terraform = "true";
ret.terraformStateFile = v.infra.terraform.stateFile;
}
}

return ret;
}),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ on:
type: string
description: GitHub environment
required: true

working_dir:
type: string
description: Name of the working directory
required: true

tf_state:
type: string
description: Name of the Terraform state file
required: true

tf_args:
type: string
description: Additional TF args
Expand Down
49 changes: 39 additions & 10 deletions .github/workflows/_deploy-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ on:
description: Path to the app
type: string

infra:
description: Whether to deploy infra
type: boolean
default: false

terraform:
description: Whether to deploy terraform
type: boolean
default: false

terraformStateFile:
description: Path to the terraform state file
type: string

jobs:
build-push:
name: Build and Push
Expand All @@ -22,6 +36,10 @@ jobs:
contents: read
packages: write

outputs:
image: ${{ steps.img.outputs.image }}
tag: ${{ steps.img.outputs.tag }}

steps:
- uses: actions/checkout@v4
name: Checkout repository
Expand All @@ -40,19 +58,10 @@ jobs:
- uses: docker/login-action@v3
name: Login to ghcr
with:
registry: ghrc.io
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# - uses: docker/build-push-action@v6
# id: build
# name: Build and Push
# with:
# file: ${{ inputs.path }}/Dockerfile
# context: ./
# push: true
# tags: ghrc.io/${{ inputs.repository }}/${{ inputs.imageName }}:${{ env.short_sha }}

- name: Build and Push ${{ inputs.name }}
id: img
run: tsx ./.github/scripts/build-push-app.mts "${{ inputs.name }}"
Expand All @@ -64,3 +73,23 @@ jobs:
run: |
echo "Image: ${IMAGE}"
echo "Tag: ${TAG}"
deploy-terraform:
name: Deploy infra

if: inputs.terraform

strategy:
fail-fast: false
matrix:
environment: [at21]

needs:
- build-push

uses: ./.github/workflows/_deploy-app-terraform.yml
with:
environment: ${{ matrix.environment }}
working_dir: ${{ inputs.path }}/deploy
tf_state: ${{ inputs.terraformStateFile }}
tf_args: "-var image=${{ needs.build-push.outputs.image }}"
5 changes: 0 additions & 5 deletions .github/workflows/cd-apps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ on:
push:
branches:
- main
- ci/cd-fix

env:
registry: ghcr.io
image_repository: altinn/altinn-authorization-tmp

jobs:
find-verticals:
Expand Down
7 changes: 5 additions & 2 deletions src/apps/Altinn.Authorization.AccessPackages/conf.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"image": {
"name": "altinn-authorization-access-packages"
"image": {},
"infra": {
"terraform": {
"stateFile": "Altinn.Authorization.AccessPackages.tfstate"
}
}
}
7 changes: 5 additions & 2 deletions src/apps/Altinn.Authorization.DeployApi/conf.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"image": {
"name": "altinn-authorization-deployapi"
"image": {},
"infra": {
"terraform": {
"stateFile": "Altinn.Authorization.DeployApi.tfstate"
}
}
}
7 changes: 5 additions & 2 deletions src/apps/Altinn.Authorization.Index/conf.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"image": {
"name": "altinn-authorization-index"
"image": {},
"infra": {
"terraform": {
"stateFile": "Altinn.Authorization.Index.tfstate"
}
}
}

0 comments on commit 45b370d

Please sign in to comment.