下載 Jenkins RPM包 java
wget https://pkg.jenkins.io/redhat/jenkins-2.197-1.1.noarch.rpm複製代碼
安裝 Jenkins mysql
rpm -ivh jenkins-2.197-1.1.noarch.rpm複製代碼
啓動Jenkins nginx
sudo service jenkins start複製代碼
解鎖git
這裏提示解鎖Jenkins,複製 /Users/Shared/Jenkins/Home/secrets/initialAdminPassword
文件內容到頁面redis
cat /Users/Shared/Jenkins/Home/secrets/initialAdminPassword
複製代碼
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://pvo5vonp.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
複製代碼
設置用戶spring
解鎖成功後,跳轉到設置用戶頁面 sql
這裏設置 admin / admindocker
安裝默認插件shell
而後提示安裝插件,選擇默認的安裝,等待安裝完成。有些插件可能安裝失敗,提示要求升級最新版本Jenkins,升級成功後能夠安裝成功。apache
點擊 >系統設置>全局工具配置
配置maven、JDK、Docker、git
Dockerfile內容:
#FROM openjdk:8-jdk-alpine
FROM hub.c.163.com/dwyane/openjdk:8
VOLUME /tmp
ADD docker-springboot-1.0-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
複製代碼
而後上傳到本身的git地址
在jenkins頁面添加自由風格的軟件項目
在源碼管理
裏添加git倉庫和用戶名、密碼配置,而且選擇代碼分支(這裏是master)
配置JenkinsSSH免密訪問GitLab
ssh-keygen -t rsa -C "youmail@mail.com" -b 4096
cat ~/.ssh/id_rsa.pub,複製添加到GitLab的SSH祕鑰
設置Jenkins啓動爲root,解決拉代碼權限問題
vim /etc/sysconfig/jenkins,修改JENKINS_USER="root"
在構建
步驟中,添加2個步驟
1.頂級maven
選擇maven版本(上面的全局配置中配的maven),添加maven打包命令
clean install -Dmaven.test.skip=true
複製代碼
2.
執行shell
添加shell:
/opt/apache-maven-3.2.5/bin/mvn docker:build
echo "當前docker 鏡像:"
docker images | grep dockerspringboot
echo "啓動容器----->"
docker run -p 8001:8001 -d dockerspringboot
echo "啓動服務成功!"
複製代碼
上面配置完成後,到Jenkins主頁,選擇配置好的項目,菜單中點擊當即構建
看到左邊菜單裏有執行的進度條,點進去後會看到執行日誌
構建中 在工做空間 /Users/yangzhenlong/.jenkins/workspace/docker-springboot 中
> /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
...
> /usr/bin/git checkout -f de05bbef05fae5ade0c79ce1d49154723d84fa88
Commit message: "修改dockerfile" #git中最後的提交記錄
> /usr/bin/git rev-list --no-walk 330c170c5829017d00942d7301834c2a196a29ac # timeout=10
[docker-springboot] $ /Users/xxxx/apache-maven-3.5.4/bin/mvn -s /Users/xxx/apache-maven-3.5.4/conf/settings.xml -gs /Users/xxx/apache-maven-3.5.4/conf/settings.xml clean install -Dmaven.test.skip=true # 執行maven腳本
[INFO] Scanning for projects...
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.412 s
[INFO] Finished at: 2018-11-19T18:01:54+08:00
[INFO] ------------------------------------------------------------------------
[docker-springboot] $ /bin/sh -xe /Users/yangzhenlong/tools/tomcat8-jenkins/temp/jenkins7206360448776968606.sh
+ mvn docker:build #構建成功後,執行shell腳本開始...
[INFO] Scanning for projects...
...
[INFO] Copying /Users/yangzhenlong/.jenkins/workspace/docker-springboot/src/main/docker/Dockerfile -> /Users/yangzhenlong/.jenkins/workspace/docker-springboot/target/docker/Dockerfile
[INFO] Building image dockerspringboot #執行Dockerfile腳本開始...
Step 1/4 : FROM hub.c.163.com/dwyane/openjdk:8
---> 96cddf5ae9f1
Step 2/4 : VOLUME /tmp
---> Using cache
---> 2e62fad6a16a
Step 3/4 : ADD docker-springboot-1.0-SNAPSHOT.jar app.jar
---> 2d1319472a12
Step 4/4 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
---> Running in f5d8c06b3b7d
Removing intermediate container f5d8c06b3b7d
---> 3f24ff705b46
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 3f24ff705b46
Successfully tagged dockerspringboot:latest
[INFO] Built dockerspringboot
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.841 s
[INFO] Finished at: 2018-11-19T18:02:00+08:00
[INFO] ------------------------------------------------------------------------
+ echo '當前docker 鏡像:' # dockerFile構建鏡像成功後,shell腳本打印docker鏡像
當前docker 鏡像:
+ docker images
+ grep dockerspringboot
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerspringboot latest 3f24ff705b46 1 second ago 657MB
+ echo '啓動容器----->'
啓動容器-----> #這裏的中文會亂碼,後面建議使用英文
+ docker run -p 8080:8080 -d dockerspringboot
e0aeafb47410a77f30bcb24b744b49dd57b251594a68da997defeef24c256f2b
+ echo $'�\220��\212��\234\215�\212��\210\220�\212\237�\201'
啓動服務成功!
Finished: SUCCESS
複製代碼
而後瀏覽器訪問 localhost:8080
若是下次構建該項目的時候,docker鏡像和服務已存在,須要先刪除鏡像和服務
# 先刪除以前的容器
echo "remobe old container"
docker ps -a | grep dockerspringboot | awk '{print $1}'| xargs docker rm -f
# 刪除以前的鏡像
echo "romove old image"
docker rmi dockerspringboot
# 構建鏡像
mvn docker:build
# 打印當前鏡像
echo "current docker images"
docker images | grep dockerspringboot
# 啓動容器
echo "start container"
docker run -p 8001:8001 -d dockerspringboot
# 打印當前容器
echo "current container"
docker ps -a | grep dockerspringboot
echo "star service success!"
複製代碼
輸出日誌:
+ echo 'current docker images'
current docker images
+ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerspringboot latest 8c9f93a324c0 Less than a second ago 657MB
<none> <none> 3f24ff705b46 2 hours ago 657MB
<none> <none> e276f4ef3cc5 2 days ago 657MB
<none> <none> 02b677ad12e1 2 days ago 657MB
nginx latest dbfc48660aeb 4 weeks ago 109MB
mongo latest a41c82c0998a 2 months ago 380MB
redis 3.2 e2e6164a20de 4 months ago 76MB
hub.c.163.com/library/nginx latest 46102226f2fd 19 months ago 109MB
mysql 5.7 9e64176cd8a2 19 months ago 407MB
hub.c.163.com/library/mysql 5.7 9e64176cd8a2 19 months ago 407MB
hub.c.163.com/dwyane/openjdk 8 96cddf5ae9f1 2 years ago 641MB
+ echo 'start container'
start container
+ docker run -p 8001:8001 -d dockerspringboot
0b79b907e9ca49831bd9259eeea2c60ec40132342b2ed3c00d4cf183756d6610
+ echo 'current container'
current container
+ docker ps -a
+ grep dockerspringboot
0b79b907e9ca dockerspringboot "java -Djava.securit…" 1 second ago Up Less than a second 0.0.0.0:8001->8001/tcp happy_johnson
+ echo 'star service success!'
star service success!
Finished: SUCCESS複製代碼