1. 安装Docker(略过)
2. 编写docker-compose.yaml
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 up
4. 浏览器中打开10.0.0.8:81,将自己的项目活动开关打开,这时应该可以监控到代码的变化了
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内容如下:
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发布。
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发布,内容如下:
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
以上