#環境centos 7.4 docker 18.03.0-cejava
# nuxus,建立maven本地源(可選,編譯java時下載jar依賴包)
# maven,編譯打包java web爲war包
# jenkins,自動化構建、發佈等git# jenkins容器裏不能執行docker命令,用ssh遠程方式
# jenkins容器 -> ssh -> maven編譯java web -> 構建docker鏡像,war包+tomcat
# war包+tomcat從新構建鏡像,便可運行java webweb
#下載鏡像 docker pull sonatype/nexus3 #data目錄設置 mkdir -p /docker/cicd/nexus-data chown -R 200 /docker/cicd/nexus-data #運行nuxus容器 docker rm -f nexus docker run -dit -p 8081:8081 --name nexus \ -v /etc/localtime:/etc/localtime:ro \ -v /docker/cicd/nexus-data:/nexus-data \ --restart=always sonatype/nexus3 #查看 docker ps -l docker logs nexus #瀏覽器打開,右上角->Sign in ,初始帳戶admin admin123 #配置阿里maven源加快下載 #新建maven2(proxy)源 #或直接更改maven-central,原地址 https://repo1.maven.org/maven2/ #阿里源地址 http://maven.aliyun.com/nexus/content/groups/public/
#下載鏡像 docker pull maven:alpine #配置maven使用私有倉庫(使用上面搭建的nuxus) #ip地址爲neuxs的10.2.1.18爲內網地址 mkdir -p /docker/cicd/maven-data cd cd /docker/cicd cat > /docker/cicd/maven-settings.xml<<EOF <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <mirrors> <mirror> <id>elven-maven</id> <name>elven-maven</name> <url>http://10.2.1.18:8081/repository/maven-public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <localRepository>/repository</localRepository> </settings> EOF
#maven容器編譯java測試docker
#下載源碼(我寫的hello world測試) mkdir -p /docker/cicd/git cd /docker/cicd/git git clone https://gitee.com/almi/helloworld.git #運行maven容器構建war包測試 docker run --rm \ -v /etc/localtime:/etc/localtime:ro \ -v /docker/cicd/maven-settings.xml:/usr/share/maven/conf/settings.xml \ -v /docker/cicd/maven-data:/repository \ -v /docker/cicd/git:/git \ maven:alpine \ mvn clean package -f /git/helloworld/pom.xml #查看 ls -l /docker/cicd/git/helloworld/target/ #如上maven容器構建war,是掛載本地源碼目錄到maven容器編譯war包 #另外一種方式,構建docker鏡像時調用maven編譯,而後建立容器拷貝出war包
#下載鏡像 docker pull jenkins:alpine #data目錄設置 mkdir -p /docker/cicd/jenkins-data cd /docker/cicd cat > /docker/cicd/jenkins-data/log.properties <<EOF handlers=java.util.logging.ConsoleHandler jenkins.level=FINEST java.util.logging.ConsoleHandler.level=FINEST EOF #目錄權限爲1000 chown -R 1000:1000 /docker/cicd/jenkins-data #啓動jenkins容器 docker rm -f jenkins docker run --name jenkins -dit \ -e JAVA_OPTS=-Duser.timezone=Asia/Shanghai \ -v /etc/localtime:/etc/localtime:ro \ -v /docker/cicd/jenkins-data:/var/jenkins_home \ --restart=always -p 8082:8080 jenkins:alpine #查看 docker ps -l docker logs jenkins #查看初始密碼 cat /docker/cicd/jenkins-data/secrets/initialAdminPassword #瀏覽器登陸,安裝插件
#步驟:jenkins -> git拉源碼 -> 構建war包 -> docker鏡像 -> 運行
#jenkins容器沒法直接運行宿主機docker命令,可以使用ssh遠程方式shell
# 建立一個自由風格的軟件項目 ->
# 源碼管理 -> git -> https://gitee.com/almi/helloworld.git
# 增長構建後操做步驟——Send build artifacts over SSH
# Exec command:在目標主機執行的shell命令
# 執行的shell以下apache
env Gitdir=${JOB_NAME} bash /docker/cicd/jenkins-data/workspace/${JOB_NAME}/build.sh
#build.sh的shell內容以下:centos
#!/bin/bash Gitdir=${Gitdir:-helloworld} # echo 'docker run maven --------' docker run --rm \ -v /docker/cicd/maven-settings.xml:/usr/share/maven/conf/settings.xml \ -v /docker/cicd/maven-data:/repository \ -v /docker/cicd/jenkins-data/workspace/:/git/ \ maven:alpine \ mvn clean package -f /git/$Gitdir/pom.xml # echo 'docker build --------' cd /docker/cicd/jenkins-data/workspace/$Gitdir docker build -t helloworld . docker images helloworld # echo "docker run helloworld --------" docker rm -f helloworld sleep 1 [[ `netstat -lntp |grep 8090 |wc -l` -ge 1 ]] && { echo "Port 8090 is used";exit; } docker run -dit --name helloworld -p 8090:8080 helloworld [[ -n `docker ps |awk '/8090/{print $1}'` ]] && { echo "run helloworld successful"; } || { echo "run helloworld error"; } docker ps -l
#jenkins還有更多實用功能如web鉤子、定時任務、郵件通知等……