Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build and push container image to GCS #160

Merged
merged 4 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Deploy
on:
push:
branches:
- main
jobs:
build-and-push-image:
name: Build and push image to Google Cloud
permissions:
id-token: write
contents: read
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: google-github-actions/auth@v1
with:
token_format: "access_token"
workload_identity_provider: ${{ secrets.GCS_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCS_SERVICE_ACCOUNT_EMAIL }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
tags: gcr.io/${{ secrets.GCS_PROJECT_ID }}/compass:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
18 changes: 12 additions & 6 deletions terraform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

## Initializing a new Google Cloud project

To initialize the Terraform backend (must be done once manually per Google Cloud project):
To initialize the Terraform backend and necessary services (must be done once manually per Google Cloud project):

1. `cd terraform/gcs-backend`
2. `terraform init`
3. `terraform apply`
3. `terraform apply -var="project=<project-id>"`

If this is for a local development environment, copy `backend.hcl` to `backend.dev.hcl` and update `bucket` with what is output from the `terraform apply` command.
### For a local development environment

Otherwise, update `backend.hcl` directly.
Copy `backend.hcl` to `backend.dev.hcl` and update `bucket` with what is output from the `terraform apply` command.

## Deploying
### For a production deployment

Update `bucket` in `backend.hcl` with what is output from the `terraform apply` command and commit your changes.

Similarly, set or update the GitHub secrets `GCS_WORKLOAD_IDENTITY_PROVIDER` and `GCS_SERVICE_ACCOUNT_EMAIL` from the output of the `terraform apply` command.

## Manual deployment

1. `cd terraform/gcs`
2. `terraform init -backend-config=../backend.dev.hcl` (you only need to run this once)
3. `terraform apply`
3. `terraform apply -var="project=<project-id>" -var="image=<image-ref>`
36 changes: 35 additions & 1 deletion terraform/gcs-backend/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ terraform {
}

provider "google" {
project = var.project
project = var.project_id
}

# Enable Cloud Storage API
Expand Down Expand Up @@ -42,3 +42,37 @@ resource "google_storage_bucket" "default" {
output "state_bucket_name" {
value = google_storage_bucket.default.name
}

# Create necessary resources for authenticating to GCP via GitHub Actions tokens
resource "google_service_account" "github_ci" {
account_id = "github-ci"
display_name = "GitHub CI"
}

# Allow GitHub CI to write to Artifact Registry
resource "google_project_iam_member" "github_ci_artifacts" {
project = var.project_id
role = "roles/artifactregistry.writer"
member = "serviceAccount:${google_service_account.github_ci.email}"
}

module "gh_oidc" {
source = "terraform-google-modules/github-actions-runners/google//modules/gh-oidc"
project_id = var.project_id
pool_id = "compass-ci-pool"
provider_id = "gh-provider"
sa_mapping = {
(google_service_account.github_ci.account_id) = {
sa_name = google_service_account.github_ci.name
attribute = "attribute.repository/user/repo"
}
}
}

output "github_ci_workload_identity_provider" {
value = module.gh_oidc.provider_name
}

output "github_ci_sevice_account_email" {
value = google_service_account.github_ci.email
}
2 changes: 1 addition & 1 deletion terraform/gcs-backend/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Google Cloud project slug
variable "project" {
variable "project_id" {
type = string
}
2 changes: 1 addition & 1 deletion terraform/gcs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ terraform {
}

provider "google" {
project = var.project
project = var.project_id
}

data "google_project" "project" {}
2 changes: 1 addition & 1 deletion terraform/gcs/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Google Cloud project slug
variable "project" {
variable "project_id" {
type = string
}

Expand Down
Loading