【问题标题】:Terraform cannot pull modules as part of jenkins pipelineTerraform 无法将模块作为 jenkins 管道的一部分提取
【发布时间】:2018-10-12 15:41:19
【问题描述】:

我有一个可以使用 terraform 自动部署一些基础设施的 jenkinsfile。不幸的是,在使用 git 源添加 terraform 模块后,它停止工作并出现以下错误:

+ terraform init -input=false -upgrade

Upgrading modules...

- module.logstash

  Updating source "git::https://bitbucket.org/*****"

Error downloading modules: Error loading modules: error downloading 'https://bitbucket.org/*****': /usr/bin/git exited with 128: Cloning into '.terraform/modules/34024e811e7ce0e58ceae615c545a1f8'...

fatal: could not read Username for 'https://bitbucket.org': No such device or address



script returned exit code 1

上面的网址在事后被混淆了。以下是精简的模块语法:

module "logstash" {
  source             = "git::https://bitbucket.org/******"
  ...
}

以下是 Jenkins 文件:

pipeline {
  agent {
    label 'linux'
  }
  triggers {
    pollSCM('*/5 * * * *')
  }
  stages {
    stage ('init') {
      steps {
        sh 'terraform init -input=false -upgrade'
      }
    }
    stage('validate') {
      steps {
        sh 'terraform validate -var-file="production.tfvars"'
      }
    }
    stage('deploy') {
      when {
        branch 'master'
      }
      steps {
        sh 'terraform apply -auto-approve -input=false -var-file=production.tfvars'
      }
    }
  }
}

我认为这是 terraform 在内部使用 git 签出模块的问题,但 Jenkins 尚未在管道作业本身内配置 git 客户端。最好我能够以某种方式将多分支管道作业使用的凭据传递到作业本身并配置 git,但我不知道如何做到这一点。任何帮助,将不胜感激。

【问题讨论】:

  • 是公共项目吗?
  • @ydaetskcoR 不,不幸的是。父项目和模块都是 bitbucket 中的私有仓库
  • 这是一个 git 问题,而不是 Terraform/Jenkins 问题。
  • @MattSchuchard 我知道这是一个 git 凭据问题,但是它是从我遇到问题的 Jenkins 构建中传递结帐凭据的行为,而不是我可以配置的 git 本身手动操作。
  • 我在你的管道代码中没有看到任何withCredentials

标签: jenkins jenkins-pipeline terraform


【解决方案1】:

所以我找到了一个不理想的解决方案,它要求您在 Jenkinsfile 中指定凭据,而不是自动使用作业使用的凭据进行结帐。

withCredentials([usernamePassword(credentialsId: 'bitbucketcreds', passwordVariable: 'GIT_PASS', usernameVariable: 'GIT_USER')]) {
  sh "git config --global credential.helper '!f() { sleep 1; echo \"username=${env.GIT_USER}\\npassword=${env.GIT_PASS}\"; }; f'"
  sh 'terraform init -input=false -upgrade'
  sh 'git config --global --remove-section credential'
}

诀窍是使用 withCredentials 块将凭据加载到环境变量中,然后我使用来自 this question 的答案设置 git 的凭据帮助程序以读取这些凭据。然后你可以运行terraform init,它会拉下你的模块。最后,它会清除修改后的 git 设置,以避免污染其他构建。请注意,这里的--global 配置对于大多数人来说可能不是一个好主意,但由于我们的 Jenkins 代理中的一个怪癖,我需要它。

如果有人有更流畅的方法,我会非常有兴趣听到它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 2018-05-17
    • 2017-12-10
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    相关资源
    最近更新 更多