How to add git credentials to the build so it would be able to be used within a shell code?

我与影子孤独终老i 提交于 2019-12-04 06:07:44

Using SSH credentials you can do that pretty easily.

Configure SSH credentials for Jenkins

First, you need to configure credentials on your Jenkins instance, i.e. tell Jenkins where it can find the private key that will be used to authenticate to your Git repository. Such as configuration could look like this :

I personnaly chose to generate SSH (private + public) keys for the jenkins user and add it to the classic user directory ~/.ssh and I added jenkins user as a member of my Git repository collaborators, but that's up to you. For more information about SSH configuration for your Git account, you can check Github doc for how to generate a new SSH key or how to add a new SSH key to your Github account.

Use SSH credentials in your pipeline

Next step is to actually use the credentials in your pipeline. Here is a simple example :

node("master") {
  stage 'Checkout'
  git url: "ssh://jenkins@your.git.server:port/git-project.git",
     credentialsId: 'jenkins_ssh_key',
     branch: master

  // The rest of your Groovy here...

  stage 'Use Git'
  // Do anything you like with your Git repo
  sh 'git add -A && git commit -m "Update code" && git push origin master'
}

When declared at checkout time, credentials should be kept for all Git commands you will further execute in your pipeline, so that should do the trick.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!