1.编辑Dockerfile文件
FROM jenkins USER root ADD sources.list /etc/apt/sources.list RUN apt-get update && apt-get install -y rsync RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ADD rsync.password /etc/rsync.password RUN chmod 600 /etc/rsync.password && chown jenkins:jenkins /etc/rsync.password ADD timezone /etc/timezone USER jenkins
注:sources.list 为国内的debian源。
rsync.password 为rsync远程同步的密码文件。
timezone 为时区文件否则,jenkins会差8个小时。内容Asia/Shanghai。
2.构建镜像
docker build -t jenkins:jenkins-chao .
3.构建Docker容器
docker run --name myjenkins -uroot -p 8081:8080 -p 50000:50000 -v /srv/jenkins_home:/var/jenkins_home -d jenkins:jenkins-chao
注:-u 表示使用root权限。
4.需要同步到远程服务器上开启rsync deamo
vi /etc/rsyncd.conf #Rsync server uid = rsync gid = rsync use chroot = no maxconnections = 2000 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/run/rsyncd.log ignore errors read only = false list = false hosts allow = 10.211.55.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password [backup] comment = www by oldboy 2016.09.23 path = /backup
5.创建rsync用户
useradd rsync -s /sbin/nologin -M
6.创建备份目录及修改权限
mkdir /backup chown -R rsync /backup echo "rsync_backup:oldboy" >/etc/rsync.password chmod 600 /etc/rsync.password
7.启动rsync
rsync --daemon
Jenkins的构建项目脚本
#切换到项目目录 JobName='xiangmu' JobDir='/var/jenkins_home/www' cd $JobDir #如果有项目目录则删除 if [ -d $JobName ]: then rm -rf $JobName fi #克隆远程项目链接 git clone ssh://git@***/"$JobName".git cd $JobName #删除git目录 rm -rf .git #项目初始化 rsync -az $JobDir"/xiangmu-dev/dev/" $JobDir"/xiangmu" #目录设置权限 chmod -R 777 $JobDir"/xiangmu/backend/runtime" && chmod -R 777 $JobDir"/xiangmu/backend/web/assets" #远程copy cd $JobDir"/"$JobName #同步 rsync -avz --delete $JobDir"/"$JobName --exclude-from=$JobDir"/xiangmu-dev/xiangmu_exclude.list" rsync_backup@*.*.*.*::backup --password-file=/etc/rsync.password
rsync启动命令
#/usr/bin/rsync –daemon
注:jenkins脚本为yii2的自动化部署。如果jenkins为root,最好rsync同步时也为root