【问题标题】:Provisioning Terraform Cloud Build Triggers build step to GCP project将 Terraform Cloud Build 触发器构建步骤配置到 GCP 项目
【发布时间】:2020-08-21 20:17:38
【问题描述】:

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

【问题讨论】:

    标签: google-cloud-platform terraform-provider-gcp


    【解决方案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
          ]
        }
    

    【讨论】:

      【解决方案2】:

      这里 [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

      【讨论】:

      • 谢谢,一些缺失的信息是构建步骤必须用 step {} 与每个步骤分开。此外,使用 TerraForm 进行部署时,云构建会触发构建步骤。创建构建内联配置。其中 { } 在 TerraForm 和 GCP 之间不被解析。这里的 EOF 或 EOT 标签可以任意使用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 2022-01-22
      • 2022-01-07
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多