Skip to content

Instantly share code, notes, and snippets.

@mrbuk
Created July 16, 2022 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrbuk/ea843bbbf1cce86333dd245d20ed7360 to your computer and use it in GitHub Desktop.
Save mrbuk/ea843bbbf1cce86333dd245d20ed7360 to your computer and use it in GitHub Desktop.
Simple Cloud Build run Terraform init/plan/apply with custom git credentials
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
steps:
- id: 'main'
name: 'alpine'
entrypoint: 'sh'
args:
- '-c'
- |
echo "***********************"
echo "$TRIGGER_NAME"
echo "***********************"
- name: 'gcr.io/cloud-builders/git'
secretEnv: ['SSH_KEY']
entrypoint: 'bash'
args:
- -c
- |
echo "$$SSH_KEY" >> /root/.ssh/id_rsa
chmod 400 /root/.ssh/id_rsa
ssh-keyscan -t rsa github.com > /root/.ssh/known_hosts
volumes:
- name: 'ssh'
path: /root/.ssh
- id: 'tf init'
name: 'hashicorp/terraform:1.0.0'
entrypoint: 'sh'
args:
- '-c'
- |
terraform init
volumes:
- name: 'ssh'
path: /root/.ssh
# [START tf-plan]
- id: 'tf plan'
name: 'hashicorp/terraform:1.0.0'
entrypoint: 'sh'
args:
- '-c'
- |
terraform plan
# [END tf-plan]
# [START tf-apply]
- id: 'tf apply'
name: 'hashicorp/terraform:1.0.0'
entrypoint: 'sh'
args:
- '-c'
- |
terraform apply -auto-approve
# [END tf-apply]
availableSecrets:
secretManager:
- versionName: projects/MY-PROJECT-ID/secrets/github-id/versions/latest
env: 'SSH_KEY'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment