Docker+Drone作Java/Tomcat的CI服務

1. 安裝Docker(略過)java

2. 編寫docker-compose.yamlgit

version: '2'

services:
drone-server:
  image: drone/drone:0.8

  ports:
    - 81:8000
    - 9000
  volumes:
    - /home/drone-image:/var/lib/drone/
  restart: always
  environment:
    - DRONE_OPEN=true
    - DRONE_HOST=http://10.0.0.8
    - DRONE_GITHUB=false
    - DRONE_GOGS=true
    - DRONE_GOGS_URL=http://10.0.0.8:10080
    - DRONE_GOGS_GIT_USERNAME=Kevin.Gu
    - DRONE_GOGS_GIT_PASSWORD=Ftit654321
    - DRONE_GOGS_SKIP_VERIFY=true
    - DRONE_SECRET=Ftit654321

drone-agent:
  image: drone/agent:0.8

  command: agent
  restart: always
  depends_on:
    - drone-server
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
  environment:
    - DRONE_SERVER=drone-server:9000
    - DRONE_SECRET=Ftit654321

3. 運行docker-compose upweb

4. 瀏覽器中打開10.0.0.8:81,將本身的項目活動開關打開,這時應該能夠監控到代碼的變化了docker

5. 在本身的項目中編寫.drone.yml文件瀏覽器

workspace:
  base: /home
  path: commission

pipeline:
  build:
    image: java:openjdk-8
  environment:
    - GRADLE_USER_HOME=~/.gradle
  commands:
    - pwd
    - wget http://mirrors.163.com/.help/sources.list.jessie -O /etc/apt/sources.list
    - apt-get update
    - apt-get install ftp
    - ./gradlew test
    - ./gradlew war
    - ftp -n < ftp_command.txt

ssh-deploy:
  image: appleboy/drone-ssh
  pull: true
  host: 10.0.0.8
  user: root
  password: M@nager
  port: 22
  command_timeout: 180
  script:
    - cd /home/commission
    - mv /home/ftpuser/commission.war .
    - mv /home/ftpuser/Dockerfile .
    - mv /home/ftpuser/deploy.sh .
    - mv /home/ftpuser/start.sh .
    - sh deploy.sh
  when:
    branch: [master]
    event: [push]

6. Build成功後,會調用FTP將相關文件拷貝到服務器上,ftp_command.txt內容以下:tomcat

open 10.0.0.8
user ftpuser 111111
put build/libs/commission-1.0.war commission.war
put Dockerfile Dockerfile
put start.sh start.sh
put deploy.sh deploy.sh
bye

7. ssh-deploy會登陸服務器,將剛剛FTP上傳的文件移到對應發佈目錄中,隨後調用deploy.sh文件,作Docker發佈。bash

deploy.sh文件內容以下:服務器

#!/bin/sh

now=`date +%Y%m%d%H%M%S`

echo building commission instance...
docker ps | grep commission | awk '{print $1}' | xargs --no-run-if-empty docker stop
docker ps -a | grep commission | awk '{print $1}' | xargs --no-run-if-empty docker rm
docker build -t commission .
echo built commission instance done...

export current_ip=$(ip route get 1 | awk '{print $NF;exit}')
docker run -it -v /home/fast_price_security:/app/security -v /home/commission_template:/app/template -v /home/commission_file:/app/file -p $current_ip:8085:8080 -d commission
Dockerfile文件內容以下:

FROM tomcat:9-alpine

MAINTAINER K.g "ytll21@gmail.com"

RUN cat /etc/issue

# RUN apt-get update && \
# apt-get install -y git build-essential curl wget software-properties-common

# RUN echo deb http://http.debian.net/debian jessie-backports main >> /etc/apt/sources.list
# RUN apt-get update && apt-get install -y openjdk-8-jdk
# RUN update-alternatives --config java

# ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64

# Adjust time zone
# RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

WORKDIR /app

# Bundle app source
COPY . /app/

EXPOSE 8080

RUN chmod +x start.sh

CMD ./start.sh

8. Dockerfile文件最後會調用start.sh文件,作Tomcat發佈,內容以下:app

cp /app/commission.war /usr/local/tomcat/webapps/ROOT.war

rm -rf /usr/local/tomcat/webapps/ROOT

cd /usr/local/tomcat/bin

./catalina.sh run

  

以上ssh

相關文章
相關標籤/搜索