问题
Using groovy syntax in Jenkins pipeline, below is the syntax used for check out:
git branch: branchName,
credentialsId: credential,
url: "${gitLabServer}/${projectName}/${repo}.git"
Where credential
is jenkins credential(111111-222222-33333-44444
) shown below:
jenkins does the following under the hood, for groovy syntax(above):
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"
For the given groovy syntax, What is the equivalent git command suffice to run on shell? Does git clone -branch
or git checkout
would not suffice? Why Jenkins is running git fetch
multiple times?
回答1:
From what I see, following need to happen. Your credential in Jenkins need to be a private key.
Authentication phase:
# 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 clone phase:
# Finally, clone the repo with branchName as a parameter
git clone -b ${branchName} git@${gitLabServer}:${projectName}/${repo}.git
回答2:
The command you are looking for is
git checkout branchName
回答3:
You can use a certificate as explained in this answer
If your repo provider support token based communication as github add token to repo url https://[token here]@github.com/[your name]/[your repo].git See the documentation to know how to provide token.
You also can use something as:
git remote set-url --push origin https://<username>:<password>@github.com/<repo>
But we understand that it's the unsecured way and strongly not recommended to use it.
来源:https://stackoverflow.com/questions/54797126/git-command-to-checkout-latest-commit-from-develop-branch