Jenkins自定义镜像推送到Harbor仓库

之前Jenkins需要推送jar包到远程到目标服务器再进行构建

现在Jenkins容器内部可以直接使用Docker了

直接在Jenkins构建好推送到Harbor仓库上,然后不管是哪台目标服务器,只需要去Harbor仓库拉取镜像即可。

修改Jenkins任务

将代码检测下面的远程服务器步骤删掉,不需要去目标服务器构建镜像了

添加执行shell步骤

编写脚本,将target目录下的jar包移到docker目录下

然后构建镜像命名mytest:版本($version通过上面拉取Gitlab的标签版本号一致)

接着登录Harbor仓库

登录后,把构建的镜像打个标签

然后上传至Harbor仓库

mv target/*.jar docker
docker build -t mytest:$version docker/
docker login -u admin -p Harbor12345 192.168.170.111:80
docker tag mytest:$version 192.168.170.111:80/monster/mytest:$version
docker push 192.168.170.111:80/monster/mytest:$version

修改项目

把Docker目录下的docker-compose.yml文件删掉,构建镜像只用到Dockerfile文件,同时修改了页面内容用于分别版本然后推送到Gitlab。

进入Gitlab项目仓库,打个新标签

v1.0.2

构建任务

回到Jenkins构建任务

构建成功

Started by user 我是真滴帅
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/mytest
The recommended git tool is: NONE
No credentials specified
 > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/mytest/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://192.168.170.111:8888/root/jenkins-cicd-mytest.git # timeout=10
Fetching upstream changes from http://192.168.170.111:8888/root/jenkins-cicd-mytest.git
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
 > git fetch --tags --force --progress -- http://192.168.170.111:8888/root/jenkins-cicd-mytest.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/main^{commit} # timeout=10
Checking out Revision b92e927c41d08ab99a8ac8a462535deadc68c972 (refs/remotes/origin/main)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f b92e927c41d08ab99a8ac8a462535deadc68c972 # timeout=10
Commit message: "删除docker-compose.yml,修改页面内容"
 > git rev-list --no-walk b92e927c41d08ab99a8ac8a462535deadc68c972 # timeout=10
[mytest] $ /bin/sh -xe /tmp/jenkins792213470151738602.sh
+ git checkout v1.0.2
HEAD is now at b92e927 删除docker-compose.yml,修改页面内容
[mytest] $ /var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven/bin/mvn clean package -DskipTests
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< com.monster:mytest >-------------------------
[INFO] Building mytest 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] Parameter 'archive' is unknown for plugin 'spring-boot-maven-plugin:2.6.13:repackage (repackage)'
[INFO] 
[INFO] --- clean:3.2.0:clean (default-clean) @ mytest ---
[INFO] Deleting /var/jenkins_home/workspace/mytest/target
[INFO] 
[INFO] --- resources:3.3.1:resources (default-resources) @ mytest ---
[INFO] Copying 2 resources from src/main/resources to target/classes
[INFO] 
[INFO] --- compiler:3.8.1:compile (default-compile) @ mytest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 4 source files to /var/jenkins_home/workspace/mytest/target/classes
[INFO] 
[INFO] --- resources:3.3.1:testResources (default-testResources) @ mytest ---
[INFO] skip non existing resourceDirectory /var/jenkins_home/workspace/mytest/src/test/resources
[INFO] 
[INFO] --- compiler:3.8.1:testCompile (default-testCompile) @ mytest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /var/jenkins_home/workspace/mytest/target/test-classes
[INFO] 
[INFO] --- surefire:3.2.2:test (default-test) @ mytest ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- jar:3.3.0:jar (default-jar) @ mytest ---
[INFO] Building jar: /var/jenkins_home/workspace/mytest/target/mytest.jar
[INFO] 
[INFO] --- spring-boot:2.6.13:repackage (repackage) @ mytest ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.980 s
[INFO] Finished at: 2024-03-15T08:54:57Z
[INFO] ------------------------------------------------------------------------
[mytest] $ /var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.host.url=http://192.168.170.111:9000 ******** -Dsonar.projectKey=mytest -Dsonar.projectname=mytest -Dsonar.java.binaries=target -Dsonar.source=./ -Dsonar.projectBaseDir=/var/jenkins_home/workspace/mytest
INFO: Scanner configuration file: /var/jenkins_home/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 5.0.1.3006
INFO: Java 17.0.7 Eclipse Adoptium (64-bit)
INFO: Linux 3.10.0-1160.el7.x86_64 amd64
INFO: User cache: /var/jenkins_home/.sonar/cache
INFO: Analyzing on SonarQube server 9.2.3
INFO: Default locale: "en", source code encoding: "UTF-8"
INFO: Load global settings
INFO: Load global settings (done) | time=66ms
INFO: Server id: 54000601-AYjKh1Zs1hD1Rss9XdjF
INFO: User cache: /var/jenkins_home/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=34ms
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
INFO: Load/download plugins (done) | time=127ms
INFO: Process project properties
INFO: Process project properties (done) | time=1ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=2ms
INFO: Project key: mytest
INFO: Base dir: /var/jenkins_home/workspace/mytest
INFO: Working dir: /var/jenkins_home/workspace/mytest/.scannerwork
INFO: Load project settings for component key: 'mytest'
INFO: Load project settings for component key: 'mytest' (done) | time=15ms
INFO: Load project branches
INFO: Load project branches (done) | time=17ms
INFO: Load project pull requests
INFO: Load project pull requests (done) | time=8ms
INFO: Load branch configuration
INFO: Load branch configuration (done) | time=1ms
INFO: Auto-configuring with CI 'Jenkins'
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=45ms
INFO: Auto-configuring with CI 'Jenkins'
INFO: Load active rules
INFO: Load active rules (done) | time=997ms
INFO: Indexing files...
INFO: Project configuration:
INFO: 10 files indexed
INFO: 14 files ignored because of scm ignore settings
INFO: Quality profile for java: Easyspeed
INFO: Quality profile for web: Sonar way
INFO: Quality profile for xml: Sonar way
INFO: ------------- Run sensors on module mytest
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=21ms
INFO: Sensor JavaSensor [java]
INFO: Configured Java source version (sonar.java.source): none
INFO: JavaClasspath initialization
INFO: JavaClasspath initialization (done) | time=6ms
INFO: JavaTestClasspath initialization
INFO: JavaTestClasspath initialization (done) | time=0ms
INFO: Java "Main" source files AST scan
INFO: 5 source files to be analyzed
INFO: Load project repositories
INFO: Load project repositories (done) | time=14ms
INFO: 5/5 source files have been analyzed
WARN: Dependencies/libraries were not provided for analysis of SOURCE files. The 'sonar.java.libraries' property is empty. Verify your configuration, as you might end up with less precise results.
WARN: Unresolved imports/types have been detected during analysis. Enable DEBUG mode to see them.
INFO: Java "Main" source files AST scan (done) | time=1016ms
INFO: No "Test" source files to scan.
INFO: No "Generated" source files to scan.
INFO: Sensor JavaSensor [java] (done) | time=1228ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=3ms
INFO: Sensor CSS Rules [javascript]
WARN: Error when running: 'node -v'. Is Node.js available during analysis?
INFO: Sensor CSS Rules [javascript] (done) | time=1698ms
INFO: Sensor C# Project Type Information [csharp]
INFO: Sensor C# Project Type Information [csharp] (done) | time=0ms
INFO: Sensor C# Analysis Log [csharp]
INFO: Sensor C# Analysis Log [csharp] (done) | time=12ms
INFO: Sensor C# Properties [csharp]
INFO: Sensor C# Properties [csharp] (done) | time=0ms
INFO: Sensor SurefireSensor [java]
INFO: parsing [/var/jenkins_home/workspace/mytest/target/surefire-reports]
INFO: Sensor SurefireSensor [java] (done) | time=2ms
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=52ms
INFO: Sensor XML Sensor [xml]
INFO: 1 source file to be analyzed
INFO: 1/1 source file has been analyzed
INFO: Sensor XML Sensor [xml] (done) | time=176ms
INFO: Sensor VB.NET Project Type Information [vbnet]
INFO: Sensor VB.NET Project Type Information [vbnet] (done) | time=0ms
INFO: Sensor VB.NET Analysis Log [vbnet]
INFO: Sensor VB.NET Analysis Log [vbnet] (done) | time=12ms
INFO: Sensor VB.NET Properties [vbnet]
INFO: Sensor VB.NET Properties [vbnet] (done) | time=0ms
INFO: Sensor com.github.mc1arke.sonarqube.plugin.scanner.ScannerPullRequestPropertySensor
INFO: Sensor com.github.mc1arke.sonarqube.plugin.scanner.ScannerPullRequestPropertySensor (done) | time=1ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=13ms
INFO: Sensor Java CPD Block Indexer
INFO: Sensor Java CPD Block Indexer (done) | time=25ms
INFO: CPD Executor 3 files had no CPD blocks
INFO: CPD Executor Calculating CPD for 3 files
INFO: CPD Executor CPD calculation finished (done) | time=6ms
INFO: Load New Code definition
INFO: Load New Code definition (done) | time=12ms
INFO: Analysis report generated in 103ms, dir size=101.0 kB
INFO: Analysis report compressed in 25ms, zip size=24.7 kB
INFO: Analysis report uploaded in 18ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://ci.hkeasyspeed.com/dashboard?id=mytest
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://ci.hkeasyspeed.com/api/ce/task?id=AY5BSiHJJVfWiirduY0k
INFO: Analysis total time: 6.860 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 7.835s
INFO: Final Memory: 19M/67M
INFO: ------------------------------------------------------------------------
[mytest] $ /bin/sh -xe /tmp/jenkins18228163841312323354.sh
+ mv target/mytest.jar docker
+ docker build -t mytest:v1.0.2 docker/
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  17.57MB

Step 1/4 : FROM daocloud.io/library/java:8u40-jdk
8u40-jdk: Pulling from library/java
a3ed95caeb02: Pulling fs layer
c06482b548c8: Pulling fs layer
673d979c2997: Pulling fs layer
3a7abcbae28e: Pulling fs layer
a5c16e57f56a: Pulling fs layer
e14d7ebd8d21: Pulling fs layer
bba22b66f004: Pulling fs layer
3a7abcbae28e: Waiting
a5c16e57f56a: Waiting
e14d7ebd8d21: Waiting
bba22b66f004: Waiting
a3ed95caeb02: Verifying Checksum
a3ed95caeb02: Download complete
a3ed95caeb02: Pull complete
673d979c2997: Verifying Checksum
673d979c2997: Download complete
a5c16e57f56a: Download complete
3a7abcbae28e: Verifying Checksum
3a7abcbae28e: Download complete
bba22b66f004: Verifying Checksum
bba22b66f004: Download complete
c06482b548c8: Verifying Checksum
c06482b548c8: Download complete
c06482b548c8: Pull complete
673d979c2997: Pull complete
3a7abcbae28e: Pull complete
a5c16e57f56a: Pull complete
e14d7ebd8d21: Verifying Checksum
e14d7ebd8d21: Download complete
e14d7ebd8d21: Pull complete
bba22b66f004: Pull complete
Digest: sha256:436b4bd7e2b8d87961006cf53d8c5d763ded4cacccef28bd156bb1c57adc8860
Status: Downloaded newer image for daocloud.io/library/java:8u40-jdk
 ---> 4aefdb29fd43
Step 2/4 : COPY mytest.jar /usr/local/
 ---> 5ea6dcb01ab4
Step 3/4 : WORKDIR /usr/local
 ---> Running in 65f62d748177
 ---> Removed intermediate container 65f62d748177
 ---> 2e7f01ae8448
Step 4/4 : CMD java -jar mytest.jar
 ---> Running in 5059f1f177ed
 ---> Removed intermediate container 5059f1f177ed
 ---> 90f3c2197da6
Successfully built 90f3c2197da6
Successfully tagged mytest:v1.0.2
+ docker login -u admin -p Harbor12345 192.168.170.111:80
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /var/jenkins_home/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
+ docker tag mytest:v1.0.2 192.168.170.111:80/monster/mytest:v1.0.2
+ docker push 192.168.170.111:80/monster/mytest:v1.0.2
The push refers to repository [192.168.170.111:80/monster/mytest]
d1e9e9d20278: Preparing
50ecdabc71b7: Preparing
3e9cda2eceec: Preparing
5f70bf18a086: Preparing
5f70bf18a086: Preparing
5f70bf18a086: Preparing
bb7b60f93aea: Preparing
0ef3d186e2bd: Preparing
1e0931f30489: Preparing
5f70bf18a086: Preparing
fd97e4a10f39: Preparing
5f70bf18a086: Preparing
0ef3d186e2bd: Waiting
1e0931f30489: Waiting
fd97e4a10f39: Waiting
5f70bf18a086: Pushed
50ecdabc71b7: Pushed
bb7b60f93aea: Pushed
d1e9e9d20278: Pushed
1e0931f30489: Pushed
0ef3d186e2bd: Pushed
fd97e4a10f39: Pushed
3e9cda2eceec: Pushed
v1.0.2: digest: sha256:05f520f02a73cdb349202166ca7e3c60e1a54b2f7b43bad1f4cd60023bf0a122 size: 2828
Finished: SUCCESS

Harbor仓库查看

已经上传到了Harbor仓库

相关推荐
追风林2 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
一坨阿亮2 小时前
Linux 使用中的问题
linux·运维
A ?Charis3 小时前
Gitlab-runner running on Kubernetes - hostAliases
容器·kubernetes·gitlab
城南vision4 小时前
Docker学习—Docker核心概念总结
java·学习·docker
wclass-zhengge4 小时前
Docker篇(Docker Compose)
运维·docker·容器
李启柱4 小时前
项目开发流程规范文档
运维·软件构建·个人开发·设计规范
力姆泰克5 小时前
看电动缸是如何提高农机的自动化水平
大数据·运维·服务器·数据库·人工智能·自动化·1024程序员节
BPM_宏天低代码5 小时前
低代码 BPA:简化业务流程自动化的新趋势
运维·低代码·自动化
sun0077005 小时前
拷贝 cp -rdp 和 cp -a
linux·运维·服务器
wowocpp6 小时前
ubuntu 22.04 server 安装 anaconda3
linux·运维·ubuntu