詹金斯管道 Terraform 计划失败

Jenkins pipeline terraform plan failing

pipeline{
       agent any
stages{
    stage('Checkout') {
      steps {
            git branch: 'master',
                credentialsId: 'xxx-test-credentials',
                url: 'https://gitlab.com/xxxx-xxxx/xxxxx/terraform.git'

            sh "pwd"
            sh "ls -lat"
        }
    }

    stage('Set Terraform path') {
    steps {
      script {
        def tfHome = tool name: 'Terraform'
        env.PATH = "${tfHome}:${env.PATH}"
       }
       sh 'terraform version'

      }
    }

    stage('Provision infrastructure') {
        steps {
          dir('environments/dev')
          {
              withCredentials([azureServicePrincipal('xxx-test-service-principal')]) {
                  script{
                    sh  'terraform init'
                    sh  'terraform plan'
                    sh  'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID'

                    }
              }
                // sh ‘terraform destroy -auto-approve’
          }

        }
    }
  }
}

我 运行 以上基本管道 运行s Terraform 初始化和规划。我已经使用创建的服务主体凭据对我想创建资源的订阅进行身份验证,但即使 Azure CLI 插件已安装在我尝试 运行 的 Jenkins 上,我也会收到以下错误管道。

[1m[31mError: [0m[0m[1mError 刷新状态: 1 发生错误: * provider.azurerm:构建 AzureRM 客户端时出错:未找到 Azure CLI 授权配置文件。请确保已安装 Azure CLI,然后使用 az login.

登录

[0米

[0米[0米[0米

我很确定你需要先授权,然后再做计划,试试这个:

sh  'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID'
sh  'terraform init'
sh  'terraform plan'