如何在 Jenkins 中签出具有特定修订版的 git repo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36793734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to checkout git repo with an specific revision in Jenkins
提问by Mitesh Jaiswal
I am running Jenkins with git, s3 and aws-codedeploy. for deploy build application over s3 and it trigger aws codedeploy for post deployment process. The above setup is running perfectly.
我正在使用 git、s3 和 aws-codedeploy 运行 Jenkins。用于通过 s3 部署构建应用程序,并触发 aws codedeploy 以进行部署后过程。以上设置运行完美。
I am getting issue whenever I run jenkins job, my job all time clone git repo then upload a zip of build/contents to s3 then aws-codedeploy deploy full zip on my servers.
每当我运行 jenkins 工作时,我都会遇到问题,我的工作总是克隆 git repo,然后将构建/内容的 zip 上传到 s3,然后 aws-codedeploy 在我的服务器上部署完整的 zip。
But I want when I run jenkin job it is take specific git revision clone only not for full contents and build deployment with that only.
但是我希望当我运行 jenkin 工作时,它只需要特定的 git 修订克隆,而不是完整的内容,并且仅使用它来构建部署。
Please help me out on the above issue. thanks in advance...
请帮助我解决上述问题。提前致谢...
回答by trainmaster
As git treats any branch as a pointer to commit, you can specify the commit in branch specifier.
由于 git 将任何分支视为提交的指针,因此您可以在分支说明符中指定提交。
EDIT (reply to comment):What you call a branch, is in fact just a pointer to a particular commit. Git has no such branches as svn; thus if you want to deploy a specific commit, you just supply it.
编辑(回复评论):你所说的分支,实际上只是一个指向特定提交的指针。Git 没有 svn 这样的分支;因此,如果你想部署一个特定的提交,你只需提供它。
I understand you don't want to change to job every time you need to build it. You can make the build parametrizedand then use the parameter as branch specifier.
我知道您不想在每次需要构建它时都换工作。您可以使构建参数化,然后将该参数用作分支说明符。
回答by CodeWizard
In Pre Steps
add this script under the Execute shell
:
在以下位置Pre Steps
添加此脚本Execute shell
:
# update the local git repository
git fetch
# pull the desired branch
git pull origin <branch>
# checkout the specific commit you want.
git checkout <commit version>
回答by Noah Nour
I am using Jenkins Pipeline.
我正在使用Jenkins 管道。
I can deploy old commit (any old commit), just run checkout in gitlabdocker image, before deploy:
我可以部署旧提交(任何旧提交),只需在部署之前在gitlab docker镜像中运行 checkout :
node {
stage('checkout') {
checkout scm
}
docker.image('docker.io/gitlab/gitlab-ce').inside('-u root -e MAVEN_OPTS="-Duser.home=./"') {
stage('git checkout') {
sh 'git checkout ${REV_VER}'
}
}
After this, i use any other docker image for build, and deploy.
在此之后,我使用任何其他 docker 映像进行构建和部署。
Example:
例子:
docker.image('openjdk:8').inside('-u root -e MAVEN_OPTS="-Duser.home=./"') {
stage('build') {
build process*
}
stage('deploy') {
deploy process*
}
}
REV_VER, this is string parametr in Jenkins config.
REV_VER,这是 Jenkins 配置中的字符串参数。
Default variable - REV_VER (in Jenkins config) - Develop(my work branch).
默认变量 - REV_VER(在 Jenkins 配置中) - Develop(我的工作分支)。
But if you need deploy any old commit, just change variable on short ID commit (like "5221e28") in string REV_VER, when you run "Build with Parametrs"
但是,如果您需要部署任何旧提交,只需在运行“Build with Parameters”时更改字符串 REV_VER 中的短 ID 提交(如“5221e28”)上的变量
p.s. OMG, i hope, you understand what i wrote, because my English is very bad =)
ps OMG,我希望你能理解我写的东西,因为我的英语很差=)
Good luck
祝你好运