简体   繁体   English

使用与在后步骤中的 Shell 脚本中克隆相同的 http git 凭据

[英]Use same http git credentials as for cloning in a Shell script in a Post Step

I want to automate our Release process and I have a following Jenkins build job for a Maven project:我想自动化我们的发布过程,我有一个 Maven 项目的以下 Jenkins 构建作业:

  • Cloning a Git repo with configured Jenkins Credentials (username/password)使用已配置的 Jenkins 凭据(用户名/密码)克隆 Git 存储库
  • Executing some Maven commands for the build为构建执行一些 Maven 命令
  • Having a Post Step configured which executes some additional Git commands: merge, tag, push配置了一个 Post Step,它执行一些额外的 Git 命令:merge、tag、push

If I run the build I receive following error during executing my Post Step Shell script:如果我运行构建,我会在执行我的Post Step Shell 脚本时收到以下错误:

fatal: could not read Username for 'https://mygitserver': Input/output error

The Git Repo server uses HTTP for Authentication. Git Repo 服务器使用 HTTP 进行身份验证。

In the Console Log I can see that Jenkins uses.gitcredentials to handle the Authentication:在控制台日志中,我可以看到 Jenkins 使用 .gitcredentials 来处理身份验证:

using .gitcredentials to set credentials
> git config --local credential.username jenkins # timeout=10
> git config --local credential.helper store --file=/tmp/git2442727044778485.credentials # timeout=10

I would like now to reuse actually these credential store because they are created at the beginning of my build, but are removed again after cloning.我现在想实际重用这些凭据存储,因为它们是在我构建开始时创建的,但在克隆后又被删除了。

Is this somehow possible or do I need to handle this somehow by myself with the "Credentials Binding Plugin", etc.?这在某种程度上是可能的,还是我需要使用“凭据绑定插件”等以某种方式自行处理?

因为我还没有找到任何解决方案来在构建开始时重用来自 clone 命令的初始 Git 凭据,所以我现在刚刚使用了 Jenkins 凭据绑定插件,并在 Post 中为我的自定义 git 命令创建了自己的凭据存储步骤。

Here is a path I took in order to fulfil a similar need:这是我为满足类似需求而采取的路径:

  • I added a public ssh key in my git account我在我的 git 帐户中添加了一个公共 ssh 密钥
  • I added the ssh private key and passphrase as ssh credential in jenkins我在 jenkins 中添加了 ssh 私钥和密码作为 ssh 凭证
  • I used this credential in my git repo clone phase我在我的 git repo 克隆阶段使用了这个凭证
  • I reused the same credential in my ssh agent jenkins build environment ** hence all post build shell script will use it我在我的 ssh 代理 jenkins 构建环境中重用了相同的凭据 ** 因此所有构建后的 shell 脚本都将使用它

The Credentials Binding plugin mentioned by @olibur can be used together with a custom GIT_ASKPASS script to allow authentification with a GitHub App: @olibur 提到的凭据绑定插件可以与自定义GIT_ASKPASS脚本一起使用,以允许使用 GitHub 应用程序进行身份验证:

First the GitHub Branch Source plugin can be used to store credentials for an installed GitHub App.首先, GitHub Branch Source插件可用于存储已安装 GitHub 应用程序的凭据。

The Credentials Binding plugin must be set up to give "Username and password (separate)" with the GitHub App credentials.必须设置凭据绑定插件以提供“用户名和密码(单独)”以及 GitHub 应用程序凭据。 The password will be the temporary access token.密码将是临时访问令牌。 In the following it is accessed as GITHUB_TOKEN .在下面,它作为GITHUB_TOKEN访问。 The user name is the App Id.用户名是应用程序 ID。

To allow to access GitHub the token must be returned from an askpass script.要允许访问 GitHub,必须从 askpass 脚本返回令牌。 With an "Inject environment variables" build step the location of the script can be defined to be in a known location available in all build steps:通过“注入环境变量”构建步骤,可以将脚本的位置定义为在所有构建步骤中可用的已知位置:

GIT_ASKPASS=$WORKSPACE/git-askpass.sh

It must be filled as first bash script build step before the repository can actually be accessed by git:在 git 实际访问存储库之前,它必须作为第一个 bash 脚本构建步骤填写:

echo 'echo $GITHUB_TOKEN' > $GIT_ASKPASS
chmod +x $GIT_ASKPASS

The script is now available in all following build steps and git can be used without specifying any credentials.该脚本现在可用于所有以下构建步骤,并且无需指定任何凭据即可使用 git。

There is interesting tutorial how to pass jenkins credentials into the pipeline scripts: https://www.baeldung.com/ops/jenkins-inject-git-secrets#2-use-credentials-in-pipeline有一个有趣的教程如何将 jenkins 凭据传递到管道脚本中: https://www.baeldung.com/ops/jenkins-inject-git-secrets#2-use-credentials-in-pipeline

And additional one here: https://www.jenkins.io/blog/2021/07/27/git-credentials-binding-phase-1/这里还有一个: https://www.jenkins.io/blog/2021/07/27/git-credentials-binding-phase-1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM