Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
anybodys committed Feb 18, 2024
1 parent ab9df58 commit 49302c5
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.infra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ jobs:
terraform_version: 1.4
terraform_wrapper: false
- name: Run infra-lint
run: |
echo "If this fails, run 'make infra-format'"
run: |-
echo "If this fails, run 'make infra-format'" \
make infra-lint
58 changes: 20 additions & 38 deletions .github/workflows/infra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,36 @@ name: Terraform CD

on:
push:
paths:
- infra/**
- .github/workflows/infra.yaml

env:
GOOGLE_CREDENTIALS: ${{ secrets.GCP_CD }}
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GCP_CD }}
PROJECT: "artist-2d"
DEPLOY_ENV: "dev"

jobs:
tf_bootstrap:
name: "Terraform Bootstrap"
runs-on: "ubuntu-latest"
defaults:
run:
working-directory: infra/bootstrap

env:
STAGE: "bootstrap"
defaults:
run:
working-directory: infra

steps:
- name: Checkout Repo
uses: actions/checkout@v1

- uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.4
jobs:
auth:
runs-on: ubuntu-latest

- run: ../setbucket.sh
- run: terraform init
- run: terraform fmt -check
- run: terraform validate
- run: terraform plan -no-color
steps:
- uses: 'actions/checkout@v3'

- run: terraform apply -no-color -auto-approve
if: github.ref_name == 'main'
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.GCP_CD }}'

tf_app:
name: "Terraform App"
runs-on: "ubuntu-latest"
defaults:
run:
working-directory: infra/app

env:
STAGE: "app"

steps:
- name: Checkout Repo
Expand All @@ -53,11 +41,5 @@ jobs:
with:
terraform_version: 1.4

- run: cd .. && ./setbucket.sh
- run: terraform init
- run: terraform fmt -check
- run: terraform validate
- run: terraform plan -no-color

- run: terraform apply -no-color -auto-approve
if: github.ref_name == 'main'
#- run: deploy.sh github.ref_name
- run: deploy.sh "voting-api:0.1.0"
49 changes: 35 additions & 14 deletions infra/app/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 41 additions & 1 deletion infra/app/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,48 @@
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "3.0.2"
}
}
}


provider "google" {
project = var.project_name
region = var.region
}

data "google_client_config" "current" {
}

#provider "docker" {
# host = "tcp://${var.region}-docker.pkg.dev"

# registry_auth {
# address = "${var.region}-docker.pkg.dev"
# #config_file_content = var.plain_content_of_config_file
# username = "oauth2accesstoken"
# password = "${data.google_client_config.current.access_token}"
# }
#}


resource "docker_image" "voting" {
name = "${var.region}-docker.pkg.dev/artist-2d/artist-2d/voting-api:${var.app_versions["voting"]}"
build {
context = "../voting-api"
}
triggers = {
dir_sha1 = sha1(join("", [for f in fileset(path.module, "../voting-api/*") : filesha1(f)]))
}
}

resource "docker_registry_image" "voting" {
name = docker_image.voting.name
keep_remotely = true
}


# Create the Cloud Run service
resource "google_cloud_run_service" "voting_service" {
Expand All @@ -13,7 +53,7 @@ resource "google_cloud_run_service" "voting_service" {
spec {
containers {
#us-west2-docker.pkg.dev/artist-2d/cloud-run-source-deploy/painter-api:latest
image = "us-west2-docker.pkg.dev/artist-2d/cloud-run-source-deploy/voting-api:${var.app_versions["voting"]}"
image = resource.docker_image.voting.name
#gcr.io/google-samples/hello-app:1.0"
}
}
Expand Down
4 changes: 2 additions & 2 deletions infra/app/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ variable "region" {
variable "app_versions" {
type = map(string)
default = {
voting : "v0.1.0",
painter : "v0.1.0",
voting : "0.1.0",
painter : "0.1.0",
}
}
7 changes: 0 additions & 7 deletions infra/app/versions.tf

This file was deleted.

7 changes: 1 addition & 6 deletions infra/bootstrap/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ resource "google_artifact_registry_repository" "image_repo" {
}

resource "google_storage_bucket" "terraform_state" {
name = "${PROJECT}-bucket-tfstate-${DEPLOY_ENV}"
name = "artist-2d-bucket-tfstate-dev"
force_destroy = false
location = "US"
storage_class = "STANDARD"
Expand All @@ -27,9 +27,4 @@ terraform {
required_providers {
google = "~> 4.66"
}

backend "gcs" {
bucket = "${PROJECT}-bucket-tfstate-${DEPLOY_ENV}"
prefix = "terraform/state"
}
}
35 changes: 35 additions & 0 deletions infra/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash


GIT_RELEASE_TAG=$1

IFS=':' read -ra GIT_RELEASE <<< "$GIT_RELEASE_TAG"
APP="${GIT_RELEASE[0]}"
VERSION="${GIT_RELEASE[1]}"
echo $APP
echo $VERSION


IMAGE=us-west1-docker.pkg.dev/artist-2d/artist-2d/${GIT_RELEASE_TAG}

echo $IMAGE

gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS

#echo $GOOGLE_APPLICATION_CREDENTIALS | docker login -u oauth2accesstoken --password-stdin us-west1-docker.pkg.dev
docker build ../{APP}/ -t $IMAGE
docker push $IMAGE

#

run_terraform () {
pushd $2
../setbucket.sh
terraform init
terraform fmt -check
terraform validate
terraform plan -no-color
terraform apply -no-color -auto-approve
}

run_terraform
Empty file added infra/main.tf
Empty file.
3 changes: 2 additions & 1 deletion infra/setbucket.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

set -x
tf_file="main.tf"
tmpfile=$(mktemp)
cp --attributes-only --preserve $tf_file $tmpfile
cat $originalfile | envsubst > $tmpfile && mv $tmpfile $tf_file
cat $tf_file | envsubst > $tmpfile && mv $tmpfile $tf_file
34 changes: 34 additions & 0 deletions test-simple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/0427b526-d632-41a1-b995-1b07a24befba.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/0a550378-3685-4671-997a-357f30635236.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/0ceaf0e6-fec8-41cd-ba18-8d9876df4d76.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/16970f81-ba7d-4675-a191-ad21f681af54.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/1eb3da73-9c6d-4cb5-a748-365d9b88d364.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/2462f909-c113-4c06-ae66-bbfa61bab95a.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/2d4ad9e4-29af-4552-ba8c-616315af5141.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/3436e4e3-2fbf-47ed-8db1-291d285f6829.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/3b9d46a1-9c99-42b2-a3c9-5f02622028be.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/3c23db31-0fd5-45c8-9446-44309db7c3de.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/3c6900ce-2e35-4a89-af5d-d574645b7aa0.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/4008762a-91f1-416f-9e56-c794c87039e0.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/4f560813-33c2-49d8-a106-78a606ff92cb.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/52417aaf-4d50-4893-ba42-3ff9977b30ce.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/60ac480e-4fbf-4de5-ac00-5f97d53cb055.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/669aa901-3915-4266-8152-010f552900d0.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/78263e3e-2819-4a20-8ba4-754c8ecec3fc.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/7928fa9d-eac7-4d48-8da9-32c160aec2fa.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/7c8f8955-6e66-4cb2-ae81-cf7b555d7655.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/7ca3efe6-9b49-4858-95be-03d384ad5428.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/7e86ac9f-ce5e-406f-b7f1-cadb00d5c343.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/8b7978be-b0d0-4990-b4de-617836b99612.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/9735c644-46bd-4108-853b-f7abe792673d.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/aff85e29-4f40-4c03-9e08-0397c5c9544b.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/d958c6c2-b07e-4a0f-8677-a351a32fbf06.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/demo.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/e035fd2d-fccc-453d-aa56-349baeb035f2.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/ea866707-3f48-4518-a901-150b6122383e.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/eb03d67a-6adc-4985-bbff-4c6c65a979b2.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/ef6a80aa-2b05-4fba-a785-283d0e7772ec.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/f2ddb245-0ce1-4a0e-b923-0ac0f8190990.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/fd3ba046-4f66-4240-bb12-9a8ebcdb894c.jpg">
<img src="https://storage.googleapis.com/artist-2d.appspot.com/gen-0/fdc2424c-b9b0-4671-a626-d6627efab556.jpg">

0 comments on commit 49302c5

Please sign in to comment.