Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Get Terraform out of the CI/CD business (#406)
Browse files Browse the repository at this point in the history
* Build containers separately

* Fail on errors requesting the secret

* Do not manage the lifecycle of Cloud Run services

* Remove unused secrets, more POLP

* Remove triggers

* Add ability to deploy single service

* Add ability to promote a single service

* Update docs + remote state

* Remove state.tf

* Fix GCR link
  • Loading branch information
sethvargo authored May 21, 2020
1 parent 03293fc commit e41bde2
Show file tree
Hide file tree
Showing 21 changed files with 411 additions and 488 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
*.tfstate*
*.terraform/
terraform.tfvars
terraform/state.tf

/local/
1 change: 0 additions & 1 deletion .ko.yaml

This file was deleted.

38 changes: 33 additions & 5 deletions setup_ko.sh → Dockerfile
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/bin/sh

# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,6 +12,36 @@
# See the License for the specific language governing permissions and
# limitations under the License.

export PROJECT_ID=$(gcloud config get-value core/project)
export KO_DOCKER_REPO="us.gcr.io/${PROJECT_ID}"
export DOCKER_REPO_OVERRIDE="us.gcr.io/${PROJECT_ID}"
FROM golang:1.14 AS builder

ARG SERVICE

RUN apt-get -qq update && apt-get -yqq install upx

ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64

WORKDIR /src
COPY . .

RUN go build \
-trimpath \
-ldflags "-s -w -extldflags '-static'" \
-installsuffix cgo \
-tags netgo \
-o /bin/service \
./cmd/${SERVICE}

RUN strip /bin/service
RUN upx -q -9 /bin/service


FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /bin/service /bin/service

ENV PORT 8080

ENTRYPOINT ["/bin/service"]
81 changes: 19 additions & 62 deletions builders/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,69 +13,26 @@
# limitations under the License.

#
# Builds container imges.
# Builds a container image.
#

options:
env:
- 'KO_DOCKER_REPO=us.gcr.io/${PROJECT_ID}'
- 'DOCKER_REPO_OVERRIDE=us.gcr.io/${PROJECT_ID}'
machineType: N1_HIGHCPU_8
substitutions:
_SERVICE:
_TAG:

steps:
# Tests
- id: test
name: 'mirror.gcr.io/library/golang'
env:
- GO111MODULE=on
args: ['go', 'test', './...']
waitFor: ['-']

# Build and publish containers`
- id: export
name: 'gcr.io/cloud-devrel-public-resources/exposure-notifications/ko:latest'
args:
- publish
- -P
- ./cmd/export
waitFor: ['test']

- id: federationin
name: 'gcr.io/cloud-devrel-public-resources/exposure-notifications/ko:latest'
args:
- publish
- -P
- ./cmd/federationin
waitFor: ['test']

- id: federationout
name: 'gcr.io/cloud-devrel-public-resources/exposure-notifications/ko:latest'
args:
- publish
- -P
- ./cmd/federationout
waitFor: ['test']

- id: exposure
name: 'gcr.io/cloud-devrel-public-resources/exposure-notifications/ko:latest'
args:
- publish
- -P
- ./cmd/exposure
waitFor: ['test']

- id: cleanup-export
name: 'gcr.io/cloud-devrel-public-resources/exposure-notifications/ko:latest'
args:
- publish
- -P
- ./cmd/cleanup-export
waitFor: ['test']

- id: cleanup-exposure
name: 'gcr.io/cloud-devrel-public-resources/exposure-notifications/ko:latest'
args:
- publish
- -P
- ./cmd/cleanup-exposure
waitFor: ['test']
- id: 'build'
name: 'registry.hub.docker.com/library/docker:18'
args: [
'build',
'--tag', 'gcr.io/${PROJECT_ID}/github.com/google/exposure-notifications-server/cmd/${_SERVICE}:${_TAG}',
'--build-arg', 'SERVICE=${_SERVICE}',
'.',
]

- id: 'publish'
name: 'registry.hub.docker.com/library/docker:18'
args: [
'push',
'gcr.io/${PROJECT_ID}/github.com/google/exposure-notifications-server/cmd/${_SERVICE}:${_TAG}',
]
89 changes: 8 additions & 81 deletions builders/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,100 +13,27 @@
# limitations under the License.

#
# Re-deploys existing Cloud Run services. The services must have been deployed
# with Terraform first, before using this script.
# Deploys a Cloud Run service.
#

substitutions:
_REGION:
_SERVICE:
_TAG:

steps:
- id: 'export'
- id: 'deploy'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-eEuo'
- 'pipefail'
- '-c'
- |-
gcloud run deploy export \
gcloud run deploy "${_SERVICE}" \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--image "us.gcr.io/$PROJECT_ID/github.com/google/exposure-notifications-server/cmd/export:latest" \
--image "gcr.io/${PROJECT_ID}/github.com/google/exposure-notifications-server/cmd/${_SERVICE}:${_TAG}" \
--no-traffic
waitFor: ['-']

- id: 'federationin'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-c'
- |-
gcloud run deploy federationin \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--image "us.gcr.io/$PROJECT_ID/github.com/google/exposure-notifications-server/cmd/federationin:latest" \
--no-traffic
waitFor: ['-']

- id: 'federationout'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-c'
- |-
gcloud run deploy federationout \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--image "us.gcr.io/$PROJECT_ID/github.com/google/exposure-notifications-server/cmd/federationout:latest" \
--no-traffic
waitFor: ['-']

- id: 'exposure'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-c'
- |-
gcloud run deploy exposure \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--image "us.gcr.io/$PROJECT_ID/github.com/google/exposure-notifications-server/cmd/exposure:latest" \
--no-traffic
waitFor: ['-']

- id: 'cleanup-export'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-c'
- |-
gcloud run deploy cleanup-export \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--image "us.gcr.io/$PROJECT_ID/github.com/google/exposure-notifications-server/cmd/cleanup-export:latest" \
--no-traffic
waitFor: ['-']

- id: 'cleanup-exposure'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-c'
- |-
gcloud run deploy cleanup-exposure \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--image "us.gcr.io/$PROJECT_ID/github.com/google/exposure-notifications-server/cmd/cleanup-exposure:latest" \
--no-traffic
waitFor: ['-']
2 changes: 2 additions & 0 deletions builders/migrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ steps:
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-eEuo'
- 'pipefail'
- '-c'
- |-
gcloud secrets versions access "${_DB_PASS_SECRET}" > ./dbpass
Expand Down
78 changes: 5 additions & 73 deletions builders/promote.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,91 +17,23 @@
#

substitutions:
_PERCENTAGE: '100'
_REGION:
_REVISION: 'LATEST'
_PERCENTAGE: '100'
_SERVICE:

steps:
- id: 'export'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-eEuo'
- 'pipefail'
- '-c'
- |-
gcloud run services update-traffic export \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--to-revisions "${_REVISION}=${_PERCENTAGE}"
waitFor: ['-']

- id: 'federationin'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-c'
- |-
gcloud run services update-traffic federationin \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--to-revisions "${_REVISION}=${_PERCENTAGE}"
waitFor: ['-']

- id: 'federationout'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-c'
- |-
gcloud run services update-traffic federationout \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--to-revisions "${_REVISION}=${_PERCENTAGE}"
waitFor: ['-']

- id: 'exposure'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-c'
- |-
gcloud run services update-traffic exposure \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--to-revisions "${_REVISION}=${_PERCENTAGE}"
waitFor: ['-']

- id: 'cleanup-export'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-c'
- |-
gcloud run services update-traffic cleanup-export \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--to-revisions "${_REVISION}=${_PERCENTAGE}"
waitFor: ['-']

- id: 'cleanup-exposure'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:293.0.0-alpine'
args:
- 'bash'
- '-c'
- |-
gcloud run services update-traffic cleanup-exposure \
gcloud run services update-traffic ${_SERVICE} \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--to-revisions "${_REVISION}=${_PERCENTAGE}"
waitFor: ['-']
Loading

0 comments on commit e41bde2

Please sign in to comment.