在我的开发中,我正在测试是否有一种方法可以将cloudbuild.yaml构建步骤(Terraform)复制到TerraForm Cloud Build Trigger Build> Step资源。我遵循了
resource "google_cloudbuild_trigger" "build-trigger" {
provider = google-beta
project = var.project
github {
owner = "github repo"
name = var.name
push {
branch = var.branch
}
}
build {
step {
id = "branch name"
name = "alpine"
entrypoint = "sh"
env = [
{
BRANCH_NAME = var.branch
}
args = [ "-c" , "echo ${BRANCH_NAME}" ]
}
}
description = "Push to any branch"
filename = "cloudbuild.yaml"
}
steps:
- id: 'branch name'
name: 'alpine'
entrypoint: 'sh'
args:
- '-c'
- |
echo "***********************"
echo "$BRANCH_NAME"
echo "***********************"
# # #[start tf-init]
- id: 'tf init'
name: 'hashicorp/terraform:0.13.0'
entrypoint: 'sh'
args:
- '-c'
- |
if [ -d "environments/$BRANCH_NAME/" ]; then
cd environments/$BRANCH_NAME
terraform init
else
for dir in environments/*/
do
cd ${dir}
env=${dir%*/}
env=${env*/}
echo ""
echo "*************** TERRAFORM INIT ******************"
echo "******* At environment: ${env} ********"
echo "*************************************************"
terraform init || exit 1
cd ../../
done
fi
# # [START tf-plan]
- id: 'tf plan'
name: 'hashicorp/terraform:0.13.0'
entrypoint: 'sh'
args:
- '-c'
- |
if [ -d "environments/$BRANCH_NAME/" ]; then
cd environments/$BRANCH_NAME
terraform plan
else
for dir in environments/*/
do
cd ${dir}
env=${dir%*/}
env=${env*/}
echo ""
echo "*************** TERRAFOM PLAN ******************"
echo "******* At environment: ${env} ********"
echo "*************************************************"
terraform plan || exit 1
cd ../../
done
fi
# # [END tf-plan]
# #[START tf-apply]
- id: 'tf apply'
name: 'hashicorp/terraform:0.13.0'
entrypoint: 'sh'
args:
- '-c'
- |
if [ -d "environments/$BRANCH_NAME/" ]; then
cd environments/$BRANCH_NAME
export TF_LOG=TRACE
terraform apply -auto-approve
else
echo "***************************** SKIPPING APPLYING *******************************"
echo "Branch '$BRANCH_NAME' does not represent an oficial environment."
echo "*******************************************************************************"
fi
# [END tf-apply]
我得到的错误是
On ../../modules/services/CloudBuildTriggers/cloudbuildtrig.tf line 22:
Expected a comma to mark the beginning of the next item.
如何简单地将bash命令放在云构建触发器的构建步骤中? 还是它们仅引用GCLOUD SDK命令?
答案 0 :(得分:1)
正确的解决方案是不使用{},然后在bash脚本的开头和结尾使用EOF或EOT。
build {
step {
id = "branch name"
name = "alpine"
entrypoint = "sh"
args = [
"-c",
<<-EOF
echo "***********************"
echo "$BRANCH_NAME"
echo "***********************"
EOF
]
}
step {
id = "tf init"
name = "hashicorp/terraform:0.13.0"
entrypoint = "sh"
args = [
"-c",
<<-EOF
if [ -d "environments/$BRANCH_NAME/" ]; then
cd environments/$BRANCH_NAME
terraform init
else
for dir in environments/*/
do
cd $dir
env=$dir%*/
env=$env#*/
echo ""
echo "*************** TERRAFORM INIT ******************"
echo "******* At environment: $env ********"
echo "*************************************************"
terraform init || exit 1
cd ../../
done
fi
EOF
]
}
答案 1 :(得分:0)
您在此处[1]上有一些有关运行bash命令的信息,在此处[2]上可以创建和管理触发器。 另一方面,您可以在terraform社区[3]上提问,因为它可以帮助您解决错误消息。
[1] https://cloud.google.com/cloud-build/docs/configuring-builds/run-bash-scripts
[2] https://cloud.google.com/cloud-build/docs/automating-builds/create-manage-triggers