代码之家  ›  专栏  ›  技术社区  ›  George Richardson

Terraform不能将模块作为jenkins管道的一部分来拉

  •  0
  • George Richardson  · 技术社区  · 6 年前

    我有一个jenkinsfile,它可以使用terraform自动部署一些基础设施。不幸的是,添加带有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/******"
      ...
    }
    

    下面是詹金森的档案:

    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,但我不知道如何做到这一点。任何帮助都将不胜感激。

    1 回复  |  直到 6 年前
        1
  •  1
  •   George Richardson    6 年前

    因此,我找到了一个不理想的解决方案,它要求您在jenkins文件中指定凭据,而不是自动使用作业用于签出的凭据。

    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 terraform init --global 配置这里可能不是一个好主意,但对大多数人来说,是我所需要的,因为在我们的詹金斯代理怪癖。

    如果有人有一个更顺利的方式做这件事,我会非常有兴趣听到它。