简体   繁体   English

如何在 Jenkins 中签出具有特定修订版的 git repo

[英]How to checkout git repo with an specific revision in Jenkins

I am running Jenkins with git, s3 and aws-codedeploy.我正在使用 git、s3 和 aws-codedeploy 运行 Jenkins。 for deploy build application over s3 and it trigger aws codedeploy for post deployment process.用于通过 s3 部署构建应用程序,并触发 aws codedeploy 以进行部署后过程。 The above setup is running perfectly.以上设置运行完美。

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...提前谢谢...

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; Git 没有 svn 这样的分支; thus if you want to deploy a specific commit, you just supply it.因此,如果你想部署一个特定的提交,你只需提供它。

I understand you don't want to change to job every time you need to build it.我知道您不想在每次需要构建它时都换工作。 You can make the build parametrized and then use the parameter as branch specifier .您可以使构建参数化,然后将该参数用作分支说明符

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>

I am using Jenkins Pipeline .我正在使用Jenkins 管道

I can deploy old commit (any old commit), just run checkout in gitlab docker 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”)上的变量

ps OMG, i hope, you understand what i wrote, because my English is very bad =) ps OMG,我希望你能理解我写的东西,因为我的英语很糟糕=)

Good luck祝你好运

According to documentation of git plugin:根据git插件的文档:

NOTE: The checkout step is the preferred SCM checkout method.注意:结帐步骤是首选的 SCM 结帐方法。 It provides significantly more functionality than the git step.它提供了比 git 步骤更多的功能。

Following above I made the version using checkout with credentials:在上面我使用带有凭据的checkout制作了版本:

def branchDeployName = 'd5e043c80b51067c397ae12567005efde5b2517f' //commit ID
/*or simply*/
//def branchDeployName = '*/master' //branch name
checkout([
  $class: 'GitSCM', 
  branches: [[name: branchDeployName]], 
  doGenerateSubmoduleConfigurations: false, 
  extensions: [], 
  submoduleCfg: [], 
  userRemoteConfigs: [
    [credentialsId: 'ac3a0ae1-d3aa-1111-ac70-cba42f82e760', 
     url: 'https://bitbucket.org/user/repo.git']
  ]
])

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

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