| # Copyright 2021 The Go Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style | |
| # license that can be found in the LICENSE file. | |
| # Run a migrate command on a database | |
| # Required substitutions | |
| # _ENV environment (dev, staging, or prod) | |
| # _CMD command to migrate (e.g., up, down N, force V). IMPORTANT: Do not enclose the command and its arguments in quotes. Enter it as you would on the command line. | |
| # Example for force: force 157 | |
| # Example for up: up | |
| timeout: 1800s | |
| steps: | |
| - id: Prepare | |
| name: gcr.io/cloud-builders/gcloud | |
| entrypoint: deploy/prepare.sh | |
| args: | |
| - $PROJECT_ID | |
| - id: Migrate | |
| name: golang:$_GO_VERSION | |
| entrypoint: bash | |
| dir: private | |
| args: | |
| - -c | |
| - | | |
| source devtools/lib.sh | |
| go install -tags postgres github.com/golang-migrate/migrate/v4/cmd/migrate@v4.15.1 | |
| go install github.com/GoogleCloudPlatform/cloudsql-proxy/cmd/cloud_sql_proxy@v1.27.0 | |
| name=$(database_name $_ENV) | |
| port=$(database_port $_ENV) | |
| cloud_sql_proxy -instances=$PROJECT_ID:us-central1:${name}=tcp:${port} & sleep 1 | |
| migrate \ | |
| -source file:../migrations \ | |
| -database "postgres://cloudbuild@127.0.0.1:${port}/${name}?sslmode=disable&password=$$PASSWORD" \ | |
| $_CMD | |
| secretEnv: ['PASSWORD'] | |
| availableSecrets: | |
| secretManager: | |
| - versionName: projects/$PROJECT_ID/secrets/cloudbuild-database-password/versions/latest | |
| env: 'PASSWORD' | |
| substitutions: | |
| _GO_VERSION: 1.27rc1 | |