Jenkins + Gitlab + Ansible--playbook 代码上线流程
jenkins 構建是支持 git 選擇分支
安裝 Git Parameter 插件
在系統管理中的插件管理
然后點擊選插件,在右上角輸入 Git Parameter,找到 Git Parameter 后點擊左下方的的直接安裝 。
參數化構建過程
插件安裝成功后,在項目配置中后看到多了一個"參數化構建過程"
勾選"參數化構建過程",然后添加 Git Parameter , 配置如下
在源碼管理中添加(需要提前將公鑰設置好)
參數化構建過程添加 git 參數 Git Parameter
填寫 name 為 release_version,下面使用 Ansible 時需要填寫描述 版本號
參數化構建過程添加選擇型參數 Choice Parameter
Build with Parameters
Build with Parameters 就可以看到效果
構建環境使用 shell 方式
構建環境使用 shell 方式 ,然后執行 ansible-playbook。
echo $module echo $release_version BUILD_LOG=`git tag -n | grep $release_version | awk '{print $2$3}'` #獲取tag注釋信息version="{\"release_version\":\"$release_version\",\"group\":\"$module\"}"if test ! -z $release_version thencd /home/work/ansible_script/ && /bin/ansible-playbook -i host test_online.yml --extra-vars="$version" fi線后發送信息
上線后為了能及時通知相關人員,可以使用觸發腳本的方式發送相關信息
Jenkins 獲取運行 job 的用戶名
需要安裝 user build vars plugin 插件,然后就可以取到 $BUILD_USER 變量。
user build vars plugin下載地址:?https://wiki.jenkins-ci.org/display/JENKINS/Build+User+Vars+Plugin?可以使用的用戶變量:
| BUILD_USER | Full name (first name + last name) |
| BUILD_USER_FIRST_NAME | First name |
| BUILD_USER_LAST_NAME | Last name |
| BUILD_USER_ID | Jenkins user ID |
| BUILD_USER_EMAIL | Email address |
在 jenkins 任務中使用構建變量:注意需要勾選 "Set jenkins user build variables." 構建環境添加
if [ $? -ne 0 ];thencd /home/work/ansible_script && python wechat_alert.py @all $BUILD_USER "$module $release_version <div class=\"highlight\">上線失敗,請關注</div>" $BUILD_URL $BUILD_LOGexit 1 elsecd /home/work/ansible_script && python wechat_alert.py @all $BUILD_USER "$module $release_version 已上線" $BUILD_URL $BUILD_LOG fi上線后的通知( 點擊詳情可以跳轉到 build url)
ansible-playbook
ansible-playbook (目前是 php 的環境 laravel 框架)
../test_online.yml
--- - hosts: '{{ group }}'remote_user: workbecome: Truebecome_method: sudovars:work_root_dir: /data/wwwmodule_name: testwork_repo: git@gitlab_host/devops/test.gitmodule_dir: ./roles:- test_online../roles/test_online 目錄結構
.
ansible-playbook 內容可以查看?https://github.com/Blacklight-Design/ansible-laravel5-deploy
更多詳情加技術交流群:238757010
本文轉自 xjtuhit 51CTO博客,原文鏈接:http://blog.51cto.com/51reboot/2052177
總結
以上是生活随笔為你收集整理的Jenkins + Gitlab + Ansible--playbook 代码上线流程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SVN在另类环境中实现自动提交的方法
- 下一篇: Puppet基于Master/Agent