-
ansible 目录结构:
[root@jenkins ansible]# tree
.
├── ansible.cfg
├── hosts
├── project
│ └── three-layer-dream
│ ├── templates
│ │ ├── default.conf.j2
│ │ └── dockerfile.j2
│ └── varsfile
├── roles
│ ├── dockerimage
│ │ └── tasks
│ │ └── main.yml
│ └── synccode
│ └── tasks
│ └── main.yml
└── site.yml
8 directories, 8 files
-
site.yml
-
---
# This playbook deploys the whole application stack in this site.
- hosts: 10.3.1.11
user: root
gather_facts: false
vars_files:
- project/{{ project }}/varsfile // 引用项目下的变量文件
roles:
- { role: dockerimage, when: "branch not in 'master'" } // 非 master 分支,bulid image,push image to registry
- { role: synccode, when: "branch in 'master'" } // master 分支, push code to 发布系统
[root@jenkins ansible]# cat project/three-layer-dream/varsfile
code_dir: /data/jenkins_data/workspace/three-layer-dream/dist/ // jenkins workspace 目录地址
image_code_dir: /usr/share/nginx/html/ // docker image 里代码存放的目录
image: nginx // FROM 父景象
frontend: true // 定义变量,程序的类型,task 会根据变量值执行相应任务
dockerfile: /etc/ansible/project/three-layer-dream/templates/dockerfile.j2 // 使用 jinja template 生成 dockerfile
template: /etc/ansible/project/three-layer-dream/templates/default.conf.j2 // 使用 jinja template 生成 nginx conf
remote_code_dir: /data/www/wepiao_project/three-layer-dream/ // master 分支, 推送发布系统的代码路径
[root@jenkins three-layer-dream]# cat templates/dockerfile.j2
FROM {{ image }}
MAINTAINER wepiao_Cail
COPY {{ project }} {{ image_code_dir }}
ADD default.conf /etc/nginx/conf.d/default.conf
[root@jenkins three-layer-dream]# cat templates/default.conf.j2
upstream mqqcgi_pool {
{% if "test" in npm|string() %}
server 192.168.101.xx:9082;
{% else %}
server 10.104.2.xx:9082;
{% endif %}
keepalive 32;
} // 根据前面选择的构建参数内容,选择下游的cgi转发环境
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
# gzip compression configure
gzip on;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript application/json ;
gzip_vary on;
proxy_buffering on;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
location ~* /(cgx|jsox|datx)/|\.json$ {
proxy_pass
proxy_set_header Host $host;
proxy_set_header X-Forwaded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^/[a-z_-]*[0-9]*\.html {
try_files $uri /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
[root@jenkins ansible]# tree roles/dockerimage/
roles/dockerimage/
└── tasks
└── main.yml
1 directory, 1 file
[root@jenkins ansible]# cat roles/dockerimage/tasks/main.yml
---
- name: Create a temporary directory
command: mktemp -d
args:
chdir: /tmp
register: tempdir
- name: Working directory is defined variable
set_fact:
workdir: "{{ tempdir.stdout }}"
- name: Synchronization code
synchronize: src={{ code_dir }} dest={{ workdir }}/{{ project }}
when: tempdir.changed
- name: Configure dockerfile jinja template
template: >
src={{ dockerfile }}
dest={{ workdir }}/Dockerfile
owner=root
group=root
mode=0644
- name: Configure nginx config jinja template
template: >
src={{ template }}
dest={{ workdir }}/default.conf
owner=root
group=root
mode=0644
when: frontend
- name: Build docker image version "{{ project }}:{{ branch }}-{{ TAG }}"
docker_image: >
path={{ workdir }}
name={{ project }}
tag={{ branch }}-{{ TAG }}
state=present
- name: Clean {{ workdir }} directory
file: path={{ workdir }} state=absent
- name: Docker tag "{{ project }}:{{ branch }}-{{ TAG }}" image
command: docker tag {{ project }}:{{ branch }}-{{ TAG }} registry.wepxx.com:5000/{{ project }}:{{ branch }}-{{ TAG }}
- name: Push "{{ project }}:{{ branch }}-{{ TAG }}" to registry.wexx.com
command: docker push registry.wepxx.com:5000/{{ project }}:{{ branch }}-{{ TAG }}
register: result
- name: Untag image
command: docker rmi registry.wepxx.com:5000/{{ project }}:{{ branch }}-{{ TAG }}
when: result.changed
- name: Clean image
command: docker rmi {{ project }}:{{ branch }}-{{ TAG }}
when: result.changed
[root@jenkins ansible]# cat roles/synccode/tasks/main.yml
---
- name: Mkdir pulish code directory
file: >
path={{ remote_code_dir }}
state=directory
mode=0755
delegate_to: 10.104.33.xxx
- name: Synchronize {{ project }} branch:{{ branch }} code
command: rsync -avSH --delete {{ code_dir }} 10.104.33.xxx:{{ remote_code_dir }} warn=false
delegate_to: 127.0.0.1
我们进入测试环节 触发一次构建,进入构建任务
--- 省略代码构建环节日志 ---
[three-layer-dream] $ /bin/sh -xe /usr/local/tomcat/temp/hudson1906818216314016688.sh
+ branch=develop
++ date +%Y%m%d%H%M
+ defatult_tag=201607011053
+ GIT_TAG_NAME=201607011053
+ ansible-playbook --extra-vars 'project=three-layer-dream TAG=201607011053 branch=develop npm=npm run pre' /etc/ansible/site.yml
PLAY ***************************************************************************
TASK [dockerimage : Create a temporary directory] ******************************
changed: [10.3.1.11]
TASK [dockerimage : Working directory is defined variable] *********************
ok: [10.3.1.11]
TASK [dockerimage : Synchronization code] **************************************
changed: [10.3.1.11]
TASK [dockerimage : Configure dockerfile jinja template] ***********************
changed: [10.3.1.11]
TASK [dockerimage : Configure nginx config jinja template] *********************
changed: [10.3.1.11]
TASK [dockerimage : Build docker image version "three-layer-dream:develop-201607011053"] ***
changed: [10.3.1.11]
TASK [dockerimage : Clean /tmp/tmp.TQZ5oRa1Qo directory] ***********************
changed: [10.3.1.11]
TASK [dockerimage : Docker tag "three-layer-dream:develop-201607011053" image] *
changed: [10.3.1.11]
TASK [dockerimage : Push "three-layer-dream:develop-201607011053" to registry.weixx.com] ***
changed: [10.3.1.11]
TASK [dockerimage : Untag image] ***********************************************
changed: [10.3.1.11]
TASK [dockerimage : Clean image] ***********************************************
changed: [10.3.1.11]
TASK [synccode : Mkdir pulish code directory] **********************************
TASK [synccode : Synchronize three-layer-dream branch:develop code] ************
PLAY RECAP *********************************************************************
10.3.1.11 : ok=11 changed=10 unreachable=0 failed=0
Finished: SUCCESS
登陆 registry UI 查看镜像是否成功上传:
OK, 完美收工,后续我们会讲解,如何使用 swarm UI 界面 shipyard 去启动,停止,销毁一个镜像,
使我们整体的构建流程完全可以给QA自助服务。
Cail_wepiao
参考资料: