jenkins 使用“Secret text”凭据作为密码克隆git repo

0lvr5msh  于 2023-03-01  发布在  Jenkins
关注(0)|答案(1)|浏览(458)

我在"Secret text"凭证中存储了一个令牌,并尝试在克隆git repo时使用该令牌作为密码。
我不想使用"带密码的用户名"凭据,因为只需要令牌,而且我不想使用虚拟用户名创建重复的凭据。
这是可行的:

pipeline {
  agent any
  environment {
    TOKEN = credentials('git-token')
    REPO = "https://_:$TOKEN@gitea.example.org/org/repo"
  }
  stages {
    stage("Clone") {
      steps {
        git env.REPO
      }
    }
  }
}

但会触发警告:

Warning: A secret was passed to "withEnv" using Groovy String interpolation, which is insecure.
         Affected argument(s) used the following variable(s): [TOKEN]
         See https://jenkins.io/redirect/groovy-string-interpolation for details.

尝试使用shell环境变量(在声明中将"替换为'REPO)失败:

ERROR: Error cloning remote repo 'origin'
hudson.plugins.git.GitException: Command "git fetch --tags --force --progress -- https://_:%24TOKEN@gitea.example.org/org/repo +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: remote: Not found.

除了创建虚拟凭据之外,我还有哪些选择?

axzmvihb

axzmvihb1#

现在,我创建了一个“用户名+密码”凭据,其中包含一个虚拟用户名,而不是“秘密文本”凭据,并使用以下Jenkinsfile

pipeline {
  agent any
  environment {
    CREDS_ID = 'git-token'
    REPO = "https://gitea.example.org/org/repo"

    // Instead of "$TOKEN", use "$CREDS_PSW" in the script.
    // `credentials(CREDS_ID)` does not work, as `credentials`
    // requires a string as an argument.
    CREDS = credentials("${env.CREDS_ID}")
  }
  stages {
    stage("Clone") {
      steps {
        git url: env.REPO, credentialsId: CREDS_ID
      }
    }
  }
}

相关问题