Git命令从开发分支签出最新提交

时间:2019-02-20 23:57:24

标签: git jenkins jenkins-pipeline git-clone git-checkout

在Jenkins管道中使用groovy语法,以下是用于签出的语法:

         git branch: branchName, 
                       credentialsId: credential, 
                       url: "${gitLabServer}/${projectName}/${repo}.git"

credential是下面显示的詹金斯凭证(111111-222222-33333-44444):

enter image description here

对于时髦的语法,jenkins做了以下工作:

Cloning the remote Git repository
Cloning repository ssh://git@10.xx.xx.xx:2222/abc/def.git
 > git init /app/jenkins/workspace/../def # timeout=10
Fetching upstream changes from ssh://git@10.xx.xx.xx:2222/abc/def.git
 > git --version # timeout=10
using GIT_SSH to set credentials abcuser
 > git fetch --tags --progress ssh://git@10.xx.xx.xx:2222/abc/def.git +refs/heads/*:refs/remotes/origin/*
 > git config remote.origin.url ssh://git@10.xx.xx.xx:2222/abc/def.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url ssh://git@10.xx.xx.xx:2222/abc/def.git # timeout=10
Fetching upstream changes from ssh://git@10.xx.xx.xx:2222/abc/def.git
using GIT_SSH to set credentials abcuser
 > git fetch --tags --progress ssh://git@10.xx.xx.xx:2222/abc/def.git +refs/heads/*:refs/remotes/origin/*
Checking out Revision 5df59884ecb3aa1b55aabf99ab8bd0adcd4eb41b (refs/remotes/origin/develop)
Commit message: "new commit"

对于给定的常规语法,要在shell上运行的等效git命令足够? git clone -branchgit checkout不够吗?为什么Jenkins多次运行git fetch

3 个答案:

答案 0 :(得分:1)

据我所见,需要发生以下情况。您在詹金斯(Jenkins)的凭据需要是私钥。

身份验证阶段:

# optional. check whether the gitLabServer is already trusted
ssh-keygen -F ${gitLabServer} 
# adds the gitLabServer into known_hosts. This stops interactive prompts during git clone.
ssh-keyscan ${gitLabServer} >> ~/.ssh/known_hosts

# Add your existing private key (like id_rsa) into authentication agent
eval `ssh-agent -s`
ssh-add /path/to/key/credential

Git克隆阶段:

# Finally, clone the repo with branchName as a parameter
git clone -b ${branchName} git@${gitLabServer}:${projectName}/${repo}.git

答案 1 :(得分:0)

您要查找的命令是

git checkout branchName

答案 2 :(得分:0)

您可以按照this answer

中的说明使用证书

如果您的回购提供者支持基于github的基于令牌的通信,请在这里将令牌添加到回购网址https://[token] @ github.com/ [您的名字] / [您的回购] .git 请参阅the documentation以了解如何提供令牌。

您还可以使用以下内容:

git remote set-url --push origin https://<username>:<password>@github.com/<repo>

但是我们知道这是不安全的方式,因此强烈建议您不要使用它。