使用 withVault 将标签从 Jenkins 管道推送到 Bitbucket 存储库

问题描述 投票:0回答:1

我需要从 keeper 命名空间中的 Vault 获取凭证,并在 Jenkins 管道中使用

withVault
指令而不是
withCredentials
。我在使用
withVault
时遇到问题。

下面在 jenkins 管道中使用

withCredentials
指令可以正常工作。

withCredentials([gitUsernamePassword(credentialsId: 'BITBUCKET_ACCESS_TOKEN_TDM', gitToolName: 'git')]) {
                            String tagName = "${branchName}/${params.RELEASE}-${env.BUILD_NUMBER}"
                            sh """
                                git tag ${tagName}
                                git push origin --tags
                            """
                        }  

控制台输出:

12:54:23  + git tag gen_testing/8.4.1-246
12:54:23  + git push origin --tags
12:54:30  To https://bitbucket.company.com/bitbucket/scm/genpower/genpower-core.git
12:54:30   * [new tag]                 gen_testing/8.4.1-246 -> gen_testing/8.4.1-246
12:54:31  Everything up-to-date  

下面是我使用的配置

withVault

                        withVault(
                            configuration: [
                                timeout: 60, 
                                vaultCredentialId: 'tdmapprole', 
                                vaultNamespace: 'genpower/tdm', 
                                vaultUrl: 'https://nat.keeper.company.com'
                            ], 
                            vaultSecrets: [
                                [
                                    engineVersion: 2, 
                                    path: 'credentials/bitbucket_access_token', 
                                    secretValues: [
                                        [
                                            envVar: 'BITBUCKET_ACCESS_TOKEN_TDM', 
                                            vaultKey: 'tgen-tdm-tbs.gen'
                                        ]
                                    ]
                                ]
                            ]
                            ) {
                                String tagName = "${branchName}/${params.RELEASE}-${env.BUILD_NUMBER}"
                                sh """
                                    git tag ${tagName}
                                    git remote set-url origin https://tgen-tdm-tbs.gen:${env.BITBUCKET_ACCESS_TOKEN_TDM}@bitbucket.company.com/bitbucket/scm/genpower/genpower-core.git
                                    git push origin --tags
                                """
                        }   
                    } 

我收到以下错误。

09:57:54  + git push origin --tags
09:57:54  fatal: could not read Username for 'https://bitbucket.company.com': No such device or address 

请注意,我使用访问令牌来推送标签。除了

/
之外,访问令牌没有任何特殊字符。从 keeper 命名空间检索令牌工作正常。

感谢对此的任何意见。谢谢!

jenkins bitbucket hashicorp-vault git-tag
1个回答
0
投票

该错误是由于缺少 url 编码造成的。添加后,它起作用了。 访问令牌包含特殊字符,因此访问令牌失败并出现错误。 如果访问令牌包含以下任何特殊字符,请将其替换为其 url 编码表示形式。

!   #   $    &   '   (   )   *   +   ,   /   :   ;   =   ?   @   [   ]
%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D

所以一开始我将保存令牌的变量从这个

${BITBUCKET_ACCESS_TOKEN_TDM}
更改为这个
${BITBUCKET_ACCESS_TOKEN_TDM/"/"/"%2F"}

但是使用转义序列将

/
替换为
%2F
可能会让人头疼。有一个更简单的解决方案。

{
  env.URL_ENCODED_BITBUCKET_ACCESS_TOKEN_TDM=URLEncoder.encode(BITBUCKET_ACCESS_TOKEN_TDM, "UTF-8")
  String tagName = "${branchName}/${params.RELEASE}-${env.BUILD_NUMBER}"
     sh """
          set +x
          git tag ${tagName}
          git remote set-url origin https://tgen-tdm-tbs.gen:${URL_ENCODED_BITBUCKET_ACCESS_TOKEN_TDM}@bitbucket.company.com/bitbucket/scm/genpower/genpower-core.git
          git push origin --tags
          set -x
        """
}

请注意,如果您不关闭 bash

(set +x)
调试,凭据将打印在作业控制台中。

© www.soinside.com 2019 - 2024. All rights reserved.