Jenkins+Docker+Git+Harbor流水線打包


           Jenkins+Docker+Git+Harbor流水線打包

html


環境:java

CentOS Linux release 7.6.1810 (Core)
192.168.247.214    Jenkins+docker
192.168.247.215    git+harbor
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld

Step1: 安裝jdk環境(214)

下載地址:https://www.oracle.com/technetwork/java/javase/downloads/index.htmlnode

tar -xf jdk-8u171-linux-x64.tar.gz
mv jdk1.8.0_171/ /usr/local/java
cat >>/etc/profile <<EOF
> JAVA_HOME=/usr/local/java
> PATH=$PATH:$JAVA_HOME/bin
> EOF
source /etc/profile
[root@localhost ~]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

Step2:安裝Jenkins

tomcat下載地址:https://mirrors.aliyun.com/apache/tomcat/tomcat-8/v8.5.38/bin/apache-tomcat-8.5.38.tar.gz
Jenkins下載地址:http://mirrors.jenkins.io/war-stable/latest/jenkins.warlinux

wget https://mirrors.aliyun.com/apache/tomcat/tomcat-8/v8.5.38/bin/apache-tomcat-8.5.38.tar.gz
tar -xf apache-tomcat-8.5.38.tar.gz
mv apache-tomcat-8.5.38 /usr/local/tomcat_jenkins
wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
mv jenkins.war /usr/local/tomcat_jenkins/webapps/
bash /usr/local/tomcat_jenkins/bin/startup.sh 

訪問地址:http://192.168.247.214:8080/jenkins/
git

Step3:安裝docker(2臺都裝)

官方文檔地址:https://docs.docker.com/install/linux/docker-ce/centos/ github

安裝國內加速器: curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

$ sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

$ yum install docker-ce -y

$ sudo systemctl start docker && systemctl enable docker

Step4:安裝docker-compose

下載地址:https://github.com/docker/compose/releasesweb

curl -L https://github.com/docker/compose/releases/download/1.24.0-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

 Step5:安裝Harbor

​    Harbor是一個用於存儲和分發Docker鏡像的企業級Registry服務器,經過添加一些企業必需的功能特性,例如安全、標識和管理等,擴展了開源Docker Distribution。做爲一個企業級私有Registry服務器,Harbor提供了更好的性能和安全。提高用戶使用Registry構建和運行環境傳輸鏡像的效率。Harbor支持安裝在多個Registry節點的鏡像資源複製,鏡像所有保存在私有Registry中,確保數據和知識產權在公司內部網絡中管控。另外,Harbor也提供了高級的安全特性,諸如用戶管理,訪問控制和活動審計等。

下載Harbor地址:https://storage.googleapis.com/harbor-releases/release-1.7.0/harbor-offline-installer-v1.7.4.tgzdocker

tar -xf harbor-offline-installer-v1.7.4.tgz
cd harbor
#vim harbor.cfg
hostname = 192.168.247.215
./prepare
./install.sh
Harbor倉庫的stop與start:
進入Harbor目錄執行以下命令便可:
docker-compose stop/start

訪問地址:http://192.168.247.215
默認用戶密碼是:admin/Harbor12345
數據庫

  Step6:配置jenkins

一、建立pipeline項目apache

二、參數化構建

三、編寫pipeline腳本

模板:

node (){ //指定Slave標籤
    //拉取代碼
   stage('Git Checkout') { 
      checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@code.aliyun.com:mtr-sscp/sscp-antipass.git']]])
   }
   //代碼編譯 
   stage('Maven Build') {
        sh '''
        export JAVA_HOME=/usr/local/java
        /usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true
        '''
   }
   stage('Build and Push Image') {
sh '''
TIME=$(date +%Y%m%d%H%M)
echo ${TIME} >TIME.txt REPOSITORY=172.31.182.143/dev/sscp-antipass:${Tag} cd target cat >> Dockerfile <<EOF # 以java:8爲基礎鏡像 FROM java:8 MAINTAINER du.cn # 告訴 Docker 服務端容器暴露的端口號 EXPOSE 8100 # 建立一個能夠從本地主機或其餘容器掛載的掛載點,通常用來存放數據庫和須要保持的數據等。 VOLUME /tmp # 格式爲 ADD <src> <dest>。 # 該命令將複製指定的 <src> 到容器中的 <dest>。 其中 <src> 能夠是Dockerfile所在目錄的一個相對路徑;也能夠是一個 URL;還能夠是一個 tar 文件(自動解壓爲目錄)。 ADD sscp-antipass.jar /app.jar # 從命令自己看,是爲了執行一個touch命令,前面的add命令把jar複製過去,後面touch命令的做用是修改這個文件的訪問時間和修改時間爲當前時間。 RUN bash -c 'touch /app.jar' # container啓動時執行的命令,可是一個Dockerfile中只能有一條ENTRYPOINT命令,若是多條,則只執行最後一條 ENTRYPOINT ["java","-jar","/app.jar"] EOF docker build -t $REPOSITORY . docker login -u admin -p Harbor%12345 172.31.182.143 docker push $REPOSITORY ''' } stage('Deploy to Docker') { sh ''' REPOSITORY=172.31.182.143/dev/sscp-antipass:${Tag} docker rm -f antipass |true docker image rm $REPOSITORY |true docker login -u admin -p Harbor%12345 172.31.182.143 docker run -p 8100:8100 -v /home/sscp/sscp-antipass/dockerDeploy/logs:/logs -d --name antipass $REPOSITORY ''' } }

 Git Checkout在Pipeline Syntax自動生成。

六、完成構建

 發佈到K8s pipeline模板:

node (){ //指定Slave標籤
    //拉取代碼
   stage('Git Checkout') { 
      checkout([$class: 'GitSCM', branches: [[name: '*/k8s']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@code.aliyun.com:mtr-sscp/sscp-transaction.git']]])
   }
   //代碼編譯 
   stage('Maven Build') {
        sh '''
        export JAVA_HOME=/usr/local/java
        /usr/local/maven/bin/mvn clean package -Pk8s -Dmaven.test.skip=true
        '''
   }
   stage('Build and Push Image') {
sh '''
TIME=latest
echo ${TIME} >TIME.txt
REPOSITORY=172.31.182.143/k8s/sscp-transaction:${TIME}
cp Dockerfile target/
cd target
docker build -t $REPOSITORY .
docker login -u admin -p Harbor%12345 172.31.182.143
docker push $REPOSITORY
'''
}
    stage('Deploy to k8s') {
    sh 'K8S_MASTER="172.31.182.152";APP="sscp-transaction";ssh ${K8S_MASTER} kubectl delete deployment.apps/${APP};ssh ${K8S_MASTER} istioctl kube-inject -f /root/sscp/${APP}/${APP}.yaml "|" kubectl apply -f -'
    }
}
相關文章
相關標籤/搜索