Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 知乎專欄 | 視頻教程 | About

6.6. Jenkinsfile Pipeline Example

6.6.1. Maven 子模組範例

Maven 子模組創建方法 https://www.netkiller.cn/java/build/maven.html#maven.module

目錄結構

			
Project
    |
    |--- common (Shared)
    |     | ---pom.xml
    |--- project1 (depend common)
    |     |--- pom.xml
    |--- project2 (depend common)
    |     |--- pom.xml
    |---pom.xml	
					
			
		

構建 父項目

			
pipeline {
    agent {
        label "default"
    }
    stages  {
        
        stage("檢出") {
            steps {
                checkout(
                  [$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]], 
                  userRemoteConfigs: [[url: env.GIT_REPO_URL]]]
                )
            }
        }

        stage("構建") {
            steps {
                echo "構建中..."
              	sh 'mvn package -Dmaven.test.skip=true' // mvn 示例
                archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true // 收集構建產物              	
                echo "構建完成."
            }
        }

        stage("測試") {
            steps {
                echo "單元測試中..."
                // 請在這裡放置您項目代碼的單元測試調用過程,例如:
                sh 'mvn test' // mvn 示例
                echo "單元測試完成."
                junit '**/target/surefire-reports/*.xml' // 收集單元測試報告的調用過程
            }
        }

        stage("部署") {
            steps {
                echo "部署中..."
                echo "部署完成"
            }
        }
    }
}
			
		

構建共享項目

			
pipeline {
    agent {
        label "default"
    }
    stages  {
        
        stage("檢出") {
            steps {
                checkout(
                  [$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]], 
                  userRemoteConfigs: [[url: env.GIT_REPO_URL]]]
                )
            }
        }

        stage("構建") {
            steps {
                echo "構建中..."
                dir(path: 'common') {
              		sh 'mvn package -Dmaven.test.skip=true' // mvn 示例
              		archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true // 收集構建產物
              	}
                echo "構建完成."
            }
        }

        stage("測試") {
            steps {
                echo "單元測試中..."
                sh 'mvn test' // mvn 示例
                echo "單元測試完成."
                junit 'target/surefire-reports/*.xml' // 收集單元測試報告的調用過程
            }
        }

        stage("部署") {
            steps {
                echo "部署中..."
                dir(path: 'common') {
                	sh 'mvn install'
                }
                echo "部署完成"
            }
        }
    }
}			
			
		

構建 project1 和 project2

			
pipeline {
    agent {
        label "default"
    }
    stages  {
        
        stage("檢出") {
            steps {
                checkout(
                  [$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]], 
                  userRemoteConfigs: [[url: env.GIT_REPO_URL]]]
                )
            }
        }
		stage("共享庫") {
            steps {
                echo "構建中..."
                dir(path: 'common') {
              		sh 'mvn install -Dmaven.test.skip=true' // mvn 示例
              		archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true // 收集構建產物
              	}
                echo "構建完成."
            }
        }
        stage("構建") {
            steps {
                echo "構建中..."
	            dir(path: 'project1') {
    	            sh 'mvn package -Dmaven.test.skip=true' // mvn 示例
   		            archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true // 收集構建產物
                }
                echo "構建完成."
                
            }
        }

        stage("測試") {
            steps {
                echo "單元測試中..."
                sh 'mvn test' // mvn 示例
                echo "單元測試完成."
                junit 'target/surefire-reports/*.xml' // 收集單元測試報告的調用過程
            }
        }

        stage("部署") {
            steps {
                echo "部署中..."
                // 部署腳本
                echo "部署完成"
            }
        }
    }
}			
			
		

6.6.2. 使用指定鏡像構建

		
pipeline {
    agent any
    stages {
        stage("Checkout") {
            steps {
                sh 'ci-init'
                checkout(
                        [$class           : 'GitSCM', branches: [[name: env.GIT_BUILD_REF]],
                         userRemoteConfigs: [[url: env.GIT_REPO_URL]]]
                )
            }
        }


        stage("Compile") {

            // 構建的 docker 鏡像
            agent {
                docker { image 'maven' }
            }

            steps {
                echo "構建中..."
                sh 'mvn -v'
                sh 'mvn compile'
            }
        }


        stage('Test') {

            agent {
                docker { image 'maven' }
            }

            steps {
                echo '單元測試...'
                sh 'mvn test'
                junit 'target/surefire-reports/*.xml'
            }

        }

        stage("Deploy") {
            steps {
                echo "部署中..."
                echo "部署完成"
            }
        }
    }
}		
		
		

6.6.3. 命令行製作 Docker 鏡像

		
pipeline {
    agent any
    stages {

        stage('Build') {

            steps {
                echo '編譯中...'
                // 編譯 docker 鏡像
                sh "docker build $tag $contextPath"
            }

        }

        stage('Push Image') {

            steps {

                sh "echo ${REGISTRY_PASS} | docker login -u ${REGISTRY_USER} --password-stdin ${REGISTRY_URL}"
                sh "docker tag ${image} ${registry_image}"
                sh "docker push ${registry_image}"
                
            }

        }
     
    }
}		
		
		
		
pipeline {

    agent any
    
    stages {
        stage("Checkout") {
          steps {
            checkout([
              $class: 'GitSCM',
              branches: [[name: env.GIT_COMMIT]],
              extensions: [[$class: 'PruneStaleBranch']],
              userRemoteConfigs: [[
                url: env.GIT_REPO_URL,
                refspec: "+refs/heads/*:refs/remotes/origin/*"
              ]]
            ])

            sh '''
                #!/bin/bash
                echo ${GIT_COMMIT}
                echo ${REF}
                echo ${GIT_LOCAL_BRANCH}
            '''
          }
        }

        stage('Build') {
            steps{
                echo "Building begin"
                script{
                    // 設置鏡像名                
                    env.BUILD_MODULE = "common"
                    env.DOCKER_IMAGE_TAG = env.BUILD_MODULE + ':' + env.GIT_COMMIT
                    env.DOCKER_REMOTE_IMAGE_TAG = "${env.REGISTRY_URL}/${env.DOCKER_IMAGE_TAG}"

                    sh "docker login ${DOCKER_REGISTER_URL} -u ${DOCKER_REPOSITORY_USERNAME} -p ${DOCKER_REPOSITORY_PASSWORD}"

                    def statusCode = sh(script:"docker pull ${DOCKER_REMOTE_IMAGE_TAG}", returnStatus:true)
                    
					// 判斷該鏡像在倉庫是否存在
                    if (statusCode != 0) {

                        sh '''
                        #!/bin/bash

                        # build docker image
                        docker build . -f Dockerfile -t ${DOCKER_IMAGE_TAG}

                        # tag docker image
                        docker tag ${DOCKER_IMAGE_TAG} ${DOCKER_REMOTE_IMAGE_TAG}

                    }
                }
                echo "Build end"
            }
        }

        stage('Deploy') {
            steps{
                echo "Deploying begin"
                script{
                        # push to
                        docker push ${DOCKER_REMOTE_IMAGE_TAG}

                        # rm
                        docker rmi ${DOCKER_IMAGE_TAG}
                        docker rmi ${DOCKER_REMOTE_IMAGE_TAG}
                        '''
                }
                echo "Deploy end"
            }
        }
    }
}		
		
		

6.6.4. Yarn

		
pipeline {
    agent {
        label "default"
    }
    stages  {
        
        stage("檢出") {
            steps {
                checkout(
                  [$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]], 
                  userRemoteConfigs: [[url: env.GIT_REPO_URL]]]
                )
            }
        }
		stage("環境") {
          	steps {
              	sh 'apt install -y apt-transport-https'
              	sh "curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -"
              	sh 'echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list'
              	sh 'cat /etc/apt/sources.list.d/yarn.list'
              	sh 'apt update && apt install -y yarn'
              	sh 'yarn --version'
            }
        }
        stage("構建") {
            steps {
                echo "構建中..."
                sh 'yarn add webpack'
                sh 'node -v'
            }
        }

        stage("測試") {
            steps {
                echo "單元測試中..."
            }
        }

        stage("部署") {
            steps {
                // sh './deploy.sh'
            }
        }
    }
}		
		
		

6.6.5. Android

進入項目目錄,找到 local.properties 檔案,打開檔案

		
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/Users/neo/Library/Android/sdk		
		
		

sdk.dir 是 Android SDK 存放目錄,進入該目錄

		
neo@MacBook-Pro ~ % ll /Users/neo/Library/Android/sdk/        
total 0
drwxr-xr-x   3 neo  staff    96B Oct 23 09:56 build-tools
drwxr-xr-x  18 neo  staff   576B Oct 23 09:55 emulator
drwxr-xr-x   6 neo  staff   192B Oct 23 10:21 extras
drwxr-xr-x   3 neo  staff    96B Oct 23 11:35 fonts
drwxr-xr-x   4 neo  staff   128B Oct 23 11:00 licenses
drwxr-xr-x   3 neo  staff    96B Oct 23 09:55 patcher
drwxr-xr-x  19 neo  staff   608B Oct 23 09:56 platform-tools
drwxr-xr-x   4 neo  staff   128B Oct 23 10:23 platforms
drwxr-xr-x  24 neo  staff   768B Oct 23 10:57 skins
drwxr-xr-x   4 neo  staff   128B Oct 23 10:23 sources
drwxr-xr-x   4 neo  staff   128B Oct 24 15:06 system-images
drwxr-xr-x  14 neo  staff   448B Oct 23 09:55 tools

neo@MacBook-Pro ~ % ll /Users/neo/Library/Android/sdk/licenses
total 16
-rw-r--r--  1 neo  staff    41B Oct 23 10:23 android-sdk-license
-rw-r--r--  1 neo  staff    41B Oct 23 11:00 android-sdk-preview-license

neo@MacBook-Pro ~ % cat /Users/neo/Library/Android/sdk/licenses/android-sdk-license 

d56f5187479451eabf01fb78af6dfcb131a6481e	
		
		

/Users/neo/Library/Android/sdk/licenses/android-sdk-license 便是當前 Android SDK License 檔案

如果你安裝了多個版本的 SDK,例如 android-26, android-27, android-28 可以看到三行字串。

		
24333f8a63b6825ea9c5514f83c2829b004d1fee 這是 Android 8.0 - android-26
d56f5187479451eabf01fb78af6dfcb131a6481e 這是 Android 9.0 - android-28
		
		
		
pipeline {
  	agent any
    stages  {
        
        stage("Checkout") {
            steps {
                checkout(
                  [$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]], 
                  userRemoteConfigs: [[url: env.GIT_REPO_URL]]]
                )
            }
        }
		
      	stage("Android SDK") {
            steps {
              	script{
	                if (fileExists('sdk-tools-linux-4333796.zip')) {
	    				echo 'Android SDK 已安裝'
					} else {
	    				echo '安裝 Android SDK'

                		sh '''
# rm -rf sdk-tools-linux-4333796.* tools platforms platform-tools
wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
unzip sdk-tools-linux-4333796.zip
         				'''
              	
              			sh 'yes|tools/bin/sdkmanager --licenses'
              			//sh 'yes|tools/bin/sdkmanager "platform-tools" "build-tools;26.0.3" "platforms;android-26"'	// andorid 8.0	
	              		//sh 'yes|tools/bin/sdkmanager "platform-tools" "platforms;android-27"' // andorid 8.1
	              		sh 'yes|tools/bin/sdkmanager "platform-tools" "platforms;android-28"'	// andorid 9.0
	              		sh '(while sleep 3; do echo "y"; done) | tools/android update sdk -u'
	              
	              		sh 'tools/bin/sdkmanager --list'
                	}
               	}	
              	echo '安裝 Android SDK License'
              	writeFile(file: 'platforms/licenses/android-sdk-license', text: '''
8933bad161af4178b1185d1a37fbf41ea5269c55
24333f8a63b6825ea9c5514f83c2829b004d1fee
d56f5187479451eabf01fb78af6dfcb131a6481e
 					''')
				sh 'ls -1 platforms'
            }
        }

        stage("Build") {
            steps {
              	echo "構建中..."
                sh './gradlew'
              	echo "構建完成."
            }
        }
        stage("Test") {
            steps {
                echo "單元測試中..."
                sh './gradlew test'
                echo "單元測試完成."
                //junit 'app/build/test-results/*/*.xml'
            }
        }      
      	stage("Package") {
            steps {
                sh './gradlew assemble'
                // 收集構建產物
                archiveArtifacts artifacts: 'app/build/outputs/apk/*/*.apk', fingerprint: true 
            }
        }

        stage("Deploy") {
            steps {
                echo "部署中..."
                // sh './deploy.sh' // 自研部署腳本
                echo "部署完成"
            }
        }
    }
}