微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

设置Terraform Cloud构建触发器向GCP项目构建步骤

如何解决设置Terraform Cloud构建触发器向GCP项目构建步骤

在我的开发中,我正在测试是否有一种方法可以将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命令?

解决方法

正确的解决方案是不使用{},然后在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]上有一些有关运行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

[3] https://www.terraform.io/community.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。