Verified Commit 17c0af14 authored by Daniel Sonck's avatar Daniel Sonck
Browse files

Combine infra into code repository

parent 6e294c83
Pipeline #267 waiting for manual action with stages
in 4 minutes and 27 seconds
stages:
- build
- deploy
- build-app
- deploy-prepare
- deploy-validate
- deploy-plan
- deploy-execute
- deploy-destroy
cache:
paths:
- .go
build:
stage: build
stage: build-app
script:
- buildah login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- ./build-docker.sh $CI_REGISTRY_IMAGE
......@@ -19,27 +23,5 @@ build:
reports:
junit: target/reports/tests.xml
cobertura: target/reports/cobertura.xml
deploy:
tags:
- release
before_script:
- eval $(ssh-agent -s | tee ssh-agent.env)
- chmod go-rwx $SSH_PRIVATE_KEY
- ssh-add $SSH_PRIVATE_KEY
after_script:
- . ssh-agent.env
- kill $SSH_AGENT_PID
stage: deploy
rules:
- if: '$CI_COMMIT_TAG =~ /^release\//'
script:
- git config --global user.email "$GITLAB_USER_EMAIL"
- git config --global user.name "$GITLAB_USER_NAME"
- git clone ssh://gitlab@git.sonck.nl:7222/touhou.fm/radio-go/infra/radio.git
- cd radio
- git checkout ci/update-version
- cp ../playlist.version.auto.tfvars ./
- git commit -a -m "Update version"
- git push -u origin ci/update-version -f
include: /infra/.gitlab-ci.yml
\ No newline at end of file
.terraform
backend.tf
details.auto.tfvars
playlist.version.auto.tfvars
\ No newline at end of file
.deploy_template: &deploy_tpl
image: registry.gitlab.com/gitlab-org/terraform-images/releases/0.13:v0.2.4
variables:
TF_ROOT: ${CI_PROJECT_DIR}/infra
TF_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${CI_ENVIRONMENT_NAME}
cache:
key: production
paths:
- ${TF_ROOT}/.terraform
before_script:
- cd ${TF_ROOT}
- ./terraform-gitlab
init:
<<: *deploy_tpl
stage: deploy-prepare
script:
- gitlab-terraform init
validate:
<<: *deploy_tpl
needs:
- init
stage: deploy-validate
script:
- gitlab-terraform validate
plan-dev:
<<: *deploy_tpl
needs:
- build
- validate
stage: deploy-plan
environment:
name: development
action: prepare
auto_stop_in: 2 days
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
when: on_success
script:
- cp ../playlist.version.auto.tfvars .
- gitlab-terraform plan
- gitlab-terraform plan-json
artifacts:
name: plan-dev
paths:
- ${TF_ROOT}/plan.cache
apply-dev:
<<: *deploy_tpl
needs:
- plan-dev
- build
stage: deploy-execute
environment:
name: development
on_stop: stop-dev
script:
- cp ../playlist.version.auto.tfvars .
- gitlab-terraform apply
dependencies:
- plan-dev
- build
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
when: manual
stop-dev:
<<: *deploy_tpl
needs:
- apply-dev
stage: deploy-destroy
script:
- gitlab-terraform init
- gitlab-terraform destroy -auto-approve
environment:
name: development
action: stop
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
when: manual
data "terraform_remote_state" "player" {
backend = "http"
config = {
address = var.player_state_address
}
}
locals {
labels = {
environment = var.environment
app = "playlist"
}
annotations = {
"app.gitlab.com/app" = var.gitlab_app
"app.gitlab.com/env" = var.environment
}
name = "${var.environment}-playlist"
player = data.terraform_remote_state.player.outputs.service
}
resource "kubernetes_service_account" "playlist" {
metadata {
name = local.name
namespace = var.namespace
labels = local.labels
}
}
resource "kubernetes_deployment" "playlist" {
metadata {
name = local.name
namespace = var.namespace
labels = merge(local.labels,{
"redmonitor.cofano.io/application" = "TouHou.FM-Radio"
})
annotations = local.annotations
}
spec {
revision_history_limit = 1
selector {
match_labels = local.labels
}
template {
metadata {
labels = merge(local.labels,{
"redmonitor.cofano.io/application" = "TouHou.FM-Radio"
})
annotations = local.annotations
}
spec {
service_account_name = kubernetes_service_account.playlist.metadata[0].name
container {
image_pull_policy = "Always"
name = "main"
image = "registry.git.sonck.nl/touhou.fm/radio-go/playlist:${var.playlist_version}"
volume_mount {
mount_path = "/config"
name = "config"
}
port {
container_port = 2001
name = "playlist"
}
port {
container_port = 4000
name = "graphql"
}
resources {
requests {
cpu = "1m"
memory = "5Mi"
}
limits {
cpu = "2m"
memory = "18Mi"
}
}
}
volume {
name = "config"
config_map {
name = kubernetes_config_map.playlist.metadata[0].name
optional = false
}
}
}
}
}
}
resource "kubernetes_config_map" "playlist" {
metadata {
name = local.name
namespace = var.namespace
labels = local.labels
}
data = {
"playlist.yml" = <<-EOC
graphql:
address: 0.0.0.0
port: 4000
player:
control:
address: ${local.player.address}
port: ${local.player.port}
playlist:
control:
address: 0.0.0.0
port: 2001
EOC
}
}
resource "kubernetes_service" "playlist" {
metadata {
name = local.name
namespace = var.namespace
labels = merge(local.labels,{
"redmonitor.cofano.io/application" = "TouHou.FM-Radio"
})
}
spec {
selector = local.labels
port {
port = 2001
target_port = "playlist"
}
}
}
resource "kubernetes_service" "playlist-gql" {
metadata {
name = "${local.name}-graphql"
namespace = var.namespace
labels = merge(local.labels,{
"redmonitor.cofano.io/application" = "TouHou.FM-Radio"
})
}
spec {
selector = local.labels
port {
port = 4000
target_port = "graphql"
}
}
}
output "service" {
value = {
address = "${kubernetes_service.playlist.metadata[0].name}.${var.namespace}"
port = 2001
}
}
output "graphql-service" {
value = {
address = "${kubernetes_service.playlist-gql.metadata[0].name}.${var.namespace}"
port = 4000
}
}
\ No newline at end of file
#!/bin/sh
ENV=${CI_ENVIRONMENT_SLUG:-unknown}
APP=${CI_PROJECT_PATH_SLUG:-unknown}
NS=${KUBE_NAMESPACE:-default}
echo "Environment: ${ENV}"
echo "Namespace : ${NS}"
cat <<EOB > backend.tf # language=terraform
terraform {
backend "http" {
}
}
EOB
cat << EOC > details.auto.tfvars # language=terraform
//player = {
// address = "${ENV}-player.${NS}"
// port = "2000"
//}
player_state_address = "${CI_API_V4_URL:-}/projects/${player_project_id:-131}/terraform/state/${ENV}"
environment = "${ENV}"
gitlab_app = "${APP}"
namespace = "${NS}"
EOC
\ No newline at end of file
variable "namespace" {
type = string
}
variable "playlist_version" {
type = string
}
variable "environment" {
type = string
}
variable "gitlab_app" {
type = string
}
variable "player_state_address" {
type = string
}
\ No newline at end of file
terraform {
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 1.13.2"
}
vault = {
source = "hashicorp/vault"
version = "~> 2.12.0"
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment