简体   繁体   English

如何在Jenkins的shell脚本中使用git凭据?

[英]How can I use git credentials in a shell script on Jenkins?

So I have a plain shell script and I want to execute 2 git commands but don't have the credentials. 所以我有一个普通的shell脚本,我想执行2个git命令,但没有凭据。 How do I get the credentials into my environment? 如何将凭据存入我的环境? Do I have to set GIT_ASKPASS or something? 我是否必须设置GIT_ASKPASS? I am not the admin so "install a plugin" isn't a great option. 我不是管理员所以“安装插件”不是一个很好的选择。

The environment has the following available already: 环境已有以下可用:

BUILD_URL
UPDATE_VERSION
HOSTNAME
PASS            <--- This one looks like a possible but still no user id. 
POM_GROUPID
HUDSON_SERVER_COOKIE
BUILD_TAG
POM_DISPLAYNAME
GIT_PREVIOUS_COMMIT
WORKSPACE
JOB_URL
RUN_CHANGES_DISPLAY_URL
POM_ARTIFACTID
MAVEN_OPTS
JENKINS_SWARM_VERSION
NLSPATH
GIT_COMMIT
JENKINS_HOME
MAVEN_HOME
PATH
RUN_DISPLAY_URL
PWD
JAVA_HOME
HUDSON_URL
JAVA_VERSION
JOB_NAME
POM_VERSION
BUILD_VERSION
XFILESEARCHPATH
BUILD_DISPLAY_NAME
BUILD_ID
JENKINS_URL
JOB_BASE_NAME
GIT_PREVIOUS_SUCCESSFUL_COMMIT
POM_PACKAGING
HOME
GIT_SSL_NO_VERIFY
SHLVL
M2_HOME
GIT_BRANCH
EXECUTOR_NUMBER
JENKINS_SERVER_COOKIE
GIT_URL
NODE_LABELS
HUDSON_HOME
NODE_NAME
BUILD_NUMBER
JOB_DISPLAY_URL
HUDSON_COOKIE

Update: here is some new information. 更新:这里有一些新信息。 GIT_ASKPASS uses these? GIT_ASKPASS使用这些? ...and I note that Jenkins is using GIT_ASKPASS to check out in the first place. ...而且我注意到Jenkins正在使用GIT_ASKPASS进行检查。

+ git help -a
+ grep credential-
  credential-cache          remote-ext
  credential-cache--daemon  remote-fd
  credential-gnome-keyring  remote-ftp
  credential-store          remote-ftps

Afraid I don't know much about Jenkins, but you can store a git credential from a shell script using your git credential helper (assuming you have one configured), eg: 我不太了解Jenkins,但您可以使用git凭证帮助程序从shell脚本存储git凭证(假设您已经配置了一个),例如:

printf "protocol=https/nhost=your.git.host/nusername=your_user/npassword=Y0urP@55w0rd/n/n" | git credential approve # git credential takes its arguments on stdin - the final double newline is needed to signal end of input

Git commands should then use it, or if you need the credential itself you can retrieve it on stdout with: 然后Git命令应该使用它,或者如果您需要凭证本身,您可以使用以下命令在stdout上检索它:

printf "protocol=https/nhost=your.git.host/username=your_user/npassword=Y0urP/n/n" | git credential fill

If your script has to have those credentials, then you can check the second option " Job DSL plugin: Build Variables " 如果您的脚本必须具有这些凭据,那么您可以检查第二个选项“ Job DSL plugin:Build Variables

All build variables are exposed to the Job DSL scripts as variables, see User Power Moves . 所有构建变量都作为变量公开给作业DSL脚本,请参阅用户电源移动
There are several ways to define credentials as build variables, eg the EnvInject Plugin provides a "Inject passwords to the build as environment variables" setting to inject passwords either defined globally in "Configure System" or directly on a job. 有几种方法可以将凭证定义为构建变量,例如EnvInject插件提供“为构建作为环境变量注入密码”设置,以注入在“配置系统”中全局定义或直接在作业上定义的密码。

// use the FLOWDOCK_TOKEN variable to configure the Flowdock publisher
job('example-4') {
    publishers {
        flowdock(FLOWDOCK_TOKEN) {
            unstable()
            success()
            aborted()
            failure()
            fixed()
            notBuilt()
        }
    }
}

// variables can also be using in configure blocks
job('example-5') {
    configure { project ->
        project / builders << 'org.foo.FooBuilder' {
            userName(FOO_USER)
            password(FOO_PASSWORD)
        }
    }
}

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

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