Skip to content

Commit

Permalink
feat: add terraform infra as code and docker build GHA
Browse files Browse the repository at this point in the history
  • Loading branch information
jimbrig committed Jun 22, 2024
1 parent e4c0925 commit eaacdfd
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 4 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Docker Build

on:
push:
branches: [main]
tags: [ '*.*.*' ]

env:
GCP_PROJECT: ${{ secrets.GCP_PROJECT }}
GCP_IMAGE: ${{ secrets.GCP_IMAGE }}

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Prepare
id: prep
run: |
DOCKER_IMAGE=gcr.io/${GCP_PROJECT}/${GCP_IMAGE}
VERSION=$(echo $GITHUB_REF | sed 's/refs\/tags\///')
DOCKER_TAG=${DOCKER_IMAGE}:${VERSION}
if [[ $VERSION =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
MINOR=${VERSION%.*}
MAJOR=${MINOR%.*}
TAGS="$TAGS,${DOCKER_IMAGE}:${MINOR},${DOCKER_IMAGE}:${MAJOR},${DOCKER_IMAGE}:latest"
elif [ "${{ github.event_name }}" = "push" ]; then
TAGS="$TAGS,${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}"
fi
echo ::set-output name=version::${VERSION}
echo ::set-output name=tags::${TAGS}
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to GCR
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with:
registry: gcr.io
username: _json_key
password: ${{ secrets.GCR_JSON_KEY }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
platforms: linux/amd64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.prep.outputs.tags }}
labels: |
org.opencontainers.image.title=${{ github.event.repository.name }}
org.opencontainers.image.description=${{ github.event.repository.description }}
org.opencontainers.image.url=${{ github.event.repository.html_url }}
org.opencontainers.image.source=${{ github.event.repository.clone_url }}
org.opencontainers.image.version=${{ steps.prep.outputs.version }}
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.licenses=${{ github.event.repository.license.spdx_id }}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 0 additions & 4 deletions inst/.gitignore

This file was deleted.

3 changes: 3 additions & 0 deletions inst/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore
!README.md
Empty file added inst/infra/README.md
Empty file.
63 changes: 63 additions & 0 deletions inst/infra/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
resource "google_project_service" "run" {
service = "run.googleapis.com"
}

resource "google_cloud_run_service" "rshinycloudrun" {
name = "rshinycloudrun"
location = var.region
project = var.project

template {
spec {
containers {
image = "gcr.io/rshinycloudrun/rshinycloudrun"
resources {
limits {
cpu = "1000m"
memory = "1024Mi"
}
requests {
cpu = "500m"
memory = "500Mi"
}
}
ports {
container_port = 5000
}
}

container_concurrency = 80
timeout_seconds = 300
}
metadata {
annotations = {
"autoscaling.knative.dev/minScale" = 0
"autoscaling.knative.dev/maxScale" = 10
}
}
}

traffic {
percent = 100
latest_revision = true
}

depends_on = [google_project_service.run]
}

resource "google_cloud_run_service_iam_member" "allUsers" {
service = google_cloud_run_service.rshinycloudrun.name
location = var.region
project = var.project
role = "roles/run.invoker"
member = "allUsers"
}

resource "google_cloud_run_domain_mapping" "rshinycloudrun" {
name = "rshinycloudrun"
location = var.region
project = var.project
spec {
route_name = google_cloud_run_service.rshinycloudrun.name
}
}
3 changes: 3 additions & 0 deletions inst/infra/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "url" {
value = google_cloud_run_service.rshinycloudrun.status[0].url
}
14 changes: 14 additions & 0 deletions inst/infra/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = ">= 3.56"
}
}
}

provider "google" {
credentials = file(var.credentials_file)
project = var.project
region = var.region
}
17 changes: 17 additions & 0 deletions inst/infra/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
variable project {
type = string
default = ""
description = "Google Cloud Project ID"
}

variable region {
type = string
default = "us-east1"
description = "Google Cloud Region"
}

variable credentials_file {
type = string
default = ""
description = "Path to Google Cloud credentials file"
}
3 changes: 3 additions & 0 deletions inst/infra/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.13"
}

0 comments on commit eaacdfd

Please sign in to comment.