Jenkins pipline pulls git historical version

Statement, this article is based on: Jenkins pipeline (jenkinsfile) detailed explanation, nanny tutorial_I can't recognize your blog-CSDN blog , the following content introduces pulling git historical version through Commit ID

What is the Commit ID (node ​​number)? (The picture below is the information of gitee)

My understanding is: every time we submit git, in addition to saving our modified code, we also cache the previously submitted code, which can be used for us to view the updated content, and the Commit ID is to identify these historical submission versions

insert image description here

1. Set character parameters

I only show a character information about the git version here, which is also the most important one in this article, and the others are not necessary

Others I set nameand tagto specify the name and version of the image made by docker

insert image description here

2. Write pipeline code

1. Click Pipeline Syntax

insert image description here

2. Choosecheckout:Check out from version control

Then choose git, and fill in the url of your git project later. This does not refer to the url of the web page, but the url git clone urlof , and then add the credentials

insert image description here

3. Don't worry about other things, just click Generate Pipeline Script, and I will talk about the specific uses later

insert image description here

4. My Jenkins piplinescript

pipeline {
    
    
    // 指定任务在哪个集群节点中执行,any表示任意节点
    agent any
    
        // 声明全局变量,方便后面修改使用
    environment {
    
    
                        
        // 源jar名称,mvn打包之后,target目录下的jar包名称
        JAR_NAME='main*'

        // jenkins下的目录
        JENKINS_HOME='/var/jenkins_home/workspace/graduationProject'
    }

    // 工具,这是我在 jenkins 的全局配置配的,之前的博客有讲过
    tools{
    
    
        maven 'maven-3.8.4'
    }
    
    
    stages {
    
    
        stage('拉取代码') {
    
    
            steps {
    
    
                // 清除工作空间
                deleteDir()
                // 拉取代码 gitTag 是 之前自定义的 字符串参数
                checkout([$class: 'GitSCM', branches: [[name: '${gitTag}']], extensions: [], userRemoteConfigs: [[credentialsId: '生成的', url: 'git的url']]])
                echo '拉取成功'
            }
        }
        
        stage('执行构建') {
    
    
            steps {
    
    
            //    sh "mvn --version"
              sh "mvn clean package"
                echo '构建完成'
            }
        }
        
        stage('把jar包构建为docker镜像并运行') {
    
    
            steps {
    
    
                sh '''#!/bin/bash
                        
                        // 进入 jar 包所在目录 , 好配合 Dockerfile 生成 Docker 镜像
                        cd $JENKINS_HOME/admin/target
                        
                        // 把Dockerfile 复制到 jar 包所在目录 ,好配合 Dockerfile 生成 Docker 镜像
                        cp $JENKINS_HOME/Dockerfile $JENKINS_HOME/admin/target
                        
                        # 修改文件权限  JAR_NAME
                        chmod 755 $JAR_NAME.jar
                        
                        # name 和 tag 是我 又设置的 一个字符参数 ,步骤和 gitTag 是一样的
                        echo "停止容器"
                        docker stop $name
                        
                        echo "删除容器"
                        docker rm $name
                        
                        echo "删除镜像"
                        docker rmi $name
                        
                        # 这个点(.)就是指的本目录下的 Dockerfile 文件,而
                        echo "打包镜像"
                        docker build -t $name:$tag .
                        
                        echo "运行镜像"
                        docker run -d -p 9999:9600 --name $name $name:$tag
                '''
                echo '运行成功'
            }
        }
    }
}

3. Explanation of generating parameters in 2.2

checkout: Check out from version control 文档:Pipeline: SCM Step (jenkins.io)

For detailed parameters of GitSCM, you can refer to the tutorial on the official website: scmGit (jenkins.io) . Although the official website is called scmGit, you must use GitSCM when using it, otherwise you will report an error.

// doGenerateSubmoduleConfigurations 可要可不要
// submoduleCfg 可要可不要
// extensions 也可不要
checkout(
    [$class: 'GitSCM', 
     doGenerateSubmoduleConfigurations: false, 
     submoduleCfg: [], 
     extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]],
     branches: [[name: '${gitTag}']],
     userRemoteConfigs: [[url: "git的url", credentialsId: "生成的"]]]
)
// 简化
checkout(
    [$class: 'GitSCM', 
     branches: [[name: '${gitTag}']],
     userRemoteConfigs: [[url: "git的url", credentialsId: "生成的"]]]
)
  • GitSCM: Specify the source code to pull git (required

  • doGenerateSubmoduleConfigurations: boolean (optional)

    • Removed tools for testing git submodule version combinations. Removed in git plugin 4.6.0. The user-supplied value is ignored and always used as its value.false
  • submoduleCfg(optional)

    Removed tools for testing git submodule version combinations. Removed in git plugin 4.6.0. User-supplied values ​​are ignored, and an empty value is always used.

  • extensions: Extensions add new behaviors or modify existing plug-in behaviors for different purposes, helping users to more precisely adjust plug-in behaviors to meet usage needs. There are many parameter items, so I will pick some commonly used ones and explain them. (optional)

    • $class: 'CloneOption': Used to define some parameter configurations when cloning.
      • depth: int type, set shallow clone depth, usually set to 1 to speed up pull, if you want regular clone, delete this configuration item.
      • noTags: bool type, deselect this option to perform cloning without tags, saving time and disk space when you only want to access what the refspec specifies.
      • reference: String type, specifies the folder containing the repository, which Git will use as a reference during the clone operation. This option will be ignored if the folder is not available on the controller or agent performing the clone.
      • shallow: bool type, perform shallow cloning, so that git will not download the history of the project, which can save time and disk space.
      • timeout: int type, specify the timeout of clone and get operations (in minutes, default value: 10).
  • branches: Specifies the identification information related to the branch to be built. (required

    • name: String type, if you want to track a specific branch in the repository, please specify the branch. For example, the master is usually passed with parameters. However, it not only supports the definition of branch names, but also supports the following types:

      • <branchName>: Specifies the branch to check out, such as master.
      • <tagName>: Specify the tag to be checked out, such as v0.0.1.
      • <commitId>: Checkout the specified commit, for example: 5062ac843f2b947733e6a3b105977056821bd352, 5062ac84.

      需要注意的是: If you use commitID to build, and it is a past ID, then depth: 1 needs to be removed, otherwise an error will always be reported.

  • userRemoteConfigs: Specify information related to the git repository (required

    • url: String type, specifying the URL or path of the git repository. The following are examples of valid git URLs:

      It is recommended to use the https style uniformly, so that it is easy to jump in daily use.

    • credentialsId: String type, used to check the source credentials, usually only need to write the name of the credentials.

    • name: String type, the ID of the repository, which is generally not used.

  • gitTool: String type, specifying the path of the git command. Usually, git is available globally in the system, so this parameter can be ignored. (optional)

4. Test

1. Copy the commit id

insert image description here

2. The test uses the branch to start

2.1, master branch

name and tag are required for the production of Docker images and do not affect

insert image description here

2.2. Results

insert image description here

3. Test commit id start

3.1、commit id

insert image description here

3.2. Results

insert image description here

previous tutorial

CICD and DevOps

Jenkins uses docker to install a nanny-level tutorial (for beginners, the latest and most complete, full text) 2022-8-1, it doesn't matter if you don't know docker

Use jenkins+gitee to create a docker image and run it

This article reference

Detailed explanation of GitSCM parameter configuration items in Jenkins' pipeline practice - Programmer Sought

Use jenkins+gitee to create a docker image and run it

Guess you like

Origin blog.csdn.net/qq_57581439/article/details/130810392