added terraform and other CI/CD config

pull/1/head
Thomas Martin 2024-11-22 17:47:21 +01:00
parent 920634d11b
commit e1b0a5668e
14 changed files with 206 additions and 22 deletions

View File

@ -0,0 +1,26 @@
FROM gcr.io/google.com/cloudsdktool/google-cloud-cli:latest
RUN apt-get update && apt-get -y upgrade
RUN apt-get install -y gnupg software-properties-common wget
#install kubectl
RUN apt-get install google-cloud-cli-app-engine-go kubectl google-cloud-cli
#install go
RUN curl -O https://dl.google.com/go/go1.23.1.linux-amd64.tar.gz
RUN rm -rf /usr/local/go && tar -C /usr/local -xzf go1.23.1.linux-amd64.tar.gz
ENV PATH=$PATH:/usr/local/go/bin
RUN rm go1.23.1.linux-amd64.tar.gz
RUN go install -v github.com/cweill/gotests/gotests@v1.6.0
RUN go install -v github.com/go-delve/delve/cmd/dlv@latest
#install terraform
RUN wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
RUN gpg --no-default-keyring --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg --fingerprint
RUN echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/hashicorp.list
RUN apt update
RUN apt-get install -y terraform
RUN touch ~/.bashrc
RUN terraform -install-autocomplete
#install docker
RUN apt install -y docker.io

View File

@ -0,0 +1,15 @@
{
"name": "Cloud Infra Dev Container",
"dockerFile": "Dockerfile",
"forwardPorts": [3000],
"postCreateCommand": "",
"mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
"customizations" : { "vscode" : { "extensions": [
"hashicorp.terraform",
"ms-kubernetes-tools.vscode-kubernetes-tools",
"googlecloudtools.cloudcode",
"golang.go",
"42Crunch.vscode-openapi"
]
}}
}

View File

16
.gitignore vendored 100644
View File

@ -0,0 +1,16 @@
**/.terraform/*
*.tfstate
*.tfstate.*
# ignore .DS_Store files for mac users
.DS_Store
# ignore executables
*.exe
# ignore consumer-cli specific config file
.userlocation
# ignore IDE specific temp files
.vscode/
.idea/

View File

@ -0,0 +1,8 @@
#!/bin/bash
# Find changes between the latest commit and main
CHANGED_FILES=$(git diff --name-only origin/main..HEAD)
# Extract directories under 'services' that were changed
CHANGED_DIRS=$(echo "$CHANGED_FILES" | grep '^services/' | cut -d'/' -f2 | sort | uniq)
echo $CHANGED_DIRS

27
cloudbuild.yaml 100644
View File

@ -0,0 +1,27 @@
steps:
# Step 1: Clone the repo and identify changed directories
- name: "gcr.io/cloud-builders/git"
entrypoint: "bash"
args:
- "-c"
- |
echo "Identifying changed directories..."
CHANGED_DIRS=$(./changed-directories.sh)
echo "Changed services: $CHANGED_DIRS"
# Step 2: Build, Test, and Containerize for each changed directory
- name: "gcr.io/cloud-builders/docker"
entrypoint: "bash"
args:
- "-c"
- |
for dir in $CHANGED_DIRS; do
echo "Processing $dir"
make -C $dir build test containerize
done
# Step 3: Push container images
- name: "gcr.io/cloud-builders/docker"
args: ["push", "gcr.io/$PROJECT_ID/$IMAGE_NAME"]
timeout: "1200s" # 20 minutes

View File

@ -2,20 +2,21 @@
# Manual edits may be lost in future updates. # Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/google" { provider "registry.terraform.io/hashicorp/google" {
version = "6.2.0" version = "6.12.0"
constraints = "6.12.0"
hashes = [ hashes = [
"h1:7JIgzQKRW0AT6UliiYSjYUKxDr03baZpQmt5XVkrujs=", "h1:rvZHMkoxkHrBYQXb/waoZiD2oo3FS1AF8HoWHlb6SN8=",
"zh:08a7dc0b53d2b63baab928e66086bf3e09107516af078ce011d2667456e64834", "zh:14701aa307a832d99f567b8056a4c5e4ee5a403d984c98f024deee7507a3f29c",
"zh:1cf9a1373e516844b43fdcea36e73f5a68f19ad07afcf6093788eb235c710163", "zh:344eca00ffb2643c2fa7f52f069b659d50bb4c9369df4cad96ea0fadb54282c8",
"zh:2d4a7cb26c3f0d036d51db219a09013d3d779e44d584e0fc631df0f2cd5e5550", "zh:5fb57c0acfd4d30a39941900040d5518a909d8c975af0c4366a7bfd0d0bb09a8",
"zh:47e1fc68e455f99f1875deaed9aa5434a852e2a70a3cb5a5e9b5a2d8c25d7b74", "zh:617a77048a5b9aa568e8bc706cc84307a237b2dd0e49709028b283f8bbe42475",
"zh:78531a8624ddcd45277e1b465e773ac92001ea0e200e9dc1147ebeb24d56359e", "zh:677837a05fefe0342cf4d4bdc494e8fd4d62331cac947820e73df37e8f512688",
"zh:a76751723c034d44764df22925178f78d8b4852e3e6ac6c5d86f51666c9e666c", "zh:7b79f6e02474eef4a1480fc6589afb63ed16b25bf019b6056f9838e2845e2ef8",
"zh:a83a59a7e667cfffb0d501a501e9b3d2d4fcc83deb07a318c9690d537cbdc4b6", "zh:7d891fceb5b15e81240d829f42e1a36e4c812bfc1abe7856756e59101932205f",
"zh:b16473b7e59e01690d8234a0044c304505688f5518b205e9ed06fc63ddc82977", "zh:97f1e0ac799faf382426e070e888fac36b0867597b460dc95b0e7f657de21ba9",
"zh:b957648ad0383e17149bf3a02def81ebc6bd55ca0cffb6ec1c368a1b4f33c4fd", "zh:9855f2f2f5919ff6a6a2c982439c910d28c8978ad18cd8f549a5d1ba9b4dc4c3",
"zh:e2f3f4a27b41a20bdbb7a80fbcde1a4c36bbd1c83edb9256bc1724754f8d370f", "zh:ac551367180eb396af2a50244e80243d333d600a76002e29935262d76a02290b",
"zh:ecfce738f85a81603aa51162d5237d6faaa2ffc0f0e52694f8b420ad761a8957", "zh:c354f34e6579933d21a98ce7f31f4ef8aeaceb04cfaedaff6d3f3c0be56b2c79",
"zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c",
] ]
} }

View File

@ -1,6 +0,0 @@
# Infrastructure
This folder is reserved for defining the cloud infrastructure using terraform.
> **NOTE**
> Run `gcloud auth application-default login` before running terraform to gain access to the project.

View File

@ -1,3 +0,0 @@
provider "google" {
project = "cloud-infra-demo"
}

View File

@ -0,0 +1,57 @@
provider "google" {
project = var.project_id
region = var.region
}
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "6.12.0"
}
}
required_version = ">= 1.3.0"
}
# Enable Google Cloud Build API
resource "google_project_service" "cloudbuild" {
project = var.project_id
service = "cloudbuild.googleapis.com"
}
# Enable Container Registry API
resource "google_project_service" "container_registry" {
project = var.project_id
service = "containerregistry.googleapis.com"
}
# IAM Role for Cloud Build service account
resource "google_project_iam_member" "cloudbuild_permissions" {
for_each = toset([
"roles/cloudbuild.builds.builder",
"roles/storage.admin"
])
project = var.project_id
role = each.key
member = "user:2121321@stud.hs-mannheim.de"
}
# Cloud Build Trigger
resource "google_cloudbuild_trigger" "cmg_trigger" {
name = "cmg-pipeline-trigger"
location = var.region
service_account = "projects/test-442316/serviceAccounts/test-442316@appspot.gserviceaccount.com"
source_to_build {
uri = "https://gitty.informatik.hs-mannheim.de/2121321/cmg-ws2024525-demo.git"
repo_type = "UNKNOWN"
ref = "refs/heads/main"
}
git_file_source {
path = "cloudbuild.yaml"
uri = "https://gitty.informatik.hs-mannheim.de/2121321/cmg-ws2024525-demo.git"
repo_type = "UNKNOWN"
revision = "refs/heads/main"
}
}

View File

@ -0,0 +1,4 @@
output "cloudbuild_trigger_url" {
description = "Cloud Build Trigger URL"
value = google_cloudbuild_trigger.cmg_trigger.id
}

View File

@ -0,0 +1,4 @@
project_id = "test-442316"
region = "europe-north1"
git_owner = "2121321"
git_repo = "cmg-ws2024525-demo"

View File

@ -0,0 +1,23 @@
variable "project_id" {
description = "test-442316"
type = string
default = "test-442316"
}
variable "region" {
description = "Google Cloud region"
type = string
default = "europe-north1"
}
variable "git_owner" {
description = "Gitea repository owner"
type = string
default = "2121321"
}
variable "git_repo" {
description = "GitHub or Gitea repository name"
type = string
default = "cmg-ws2024525-demo"
}

View File

@ -0,0 +1,12 @@
FROM golang:1.23
WORKDIR /usr/src/consumer-gateway
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
COPY go.mod go.sum ./
RUN go mod download && go mod verify
COPY . .
RUN go build -v -o /usr/local/bin ./...
CMD ["consumer-gateway"]