1. 程式人生 > >CentOS7下的Django2整合部署四:Jenkins的freestyle-job

CentOS7下的Django2整合部署四:Jenkins的freestyle-job

  •   前提:Jenkins所在的客戶機的ssh-key加入到GitLab中
  •   目標:自動化部署GitLab上的新建的專案ok-admin
  •   1.編寫git專案的nginx配置檔案
    [[email protected] ~]# vim /etc/nginx/conf.d/my_ok.conf
    server {
            listen          80;
            server_name     www.my-ok.cc;
            location / {
                    root    
    /usr/share/nginx/html/ok-admin; index index.html index.htm; } }
    /etc/nginx/conf.d/my_ok
  •   2.編寫釋出指令碼
    [[email protected] ~]# mkdir /data/scripts
    [[email protected]-ct75211 ~]# cd /data/scripts/
    [[email protected]-ct75211 ~]# vim /data/scripts/deploy.sh
    1 #! /bin/bash
    2 job_name=$1
    3 app_name=app-$(date +%F)-$(($RANDOM+10000))
    4 echo $app_name
    5 cd /var/lib/jenkins/workspace/${job_name} && tar czf /opt/${app_name}.tar.gz ./*
    6 cd /var/webroot && mkdir ${app_name}
    7 cp /opt/${app_name}.tar.gz /var/webroot/${app_name}
    8 cd /var/webroot/${app_name} && tar xf ${app_name}.tar.gz && rm -f ${app_name}.tar.gz
    
    9 cd /usr/share/nginx/html && rm -rf ok-admin && ln -s /var/webroot/${app_name} /usr/share/nginx/html/ok-admin
    /data/scripts/deploy.sh
     
       

      [[email protected] scripts]# mkdir /var/webroot

    本地測試下

    [[email protected] scripts]# ./deploy.sh my-freestyle-job
    app-2018-12-15-20566
    [[email protected]-ct75211 scripts]# ll /usr/share/nginx/html/
    total 8
    -rw-r--r--. 1 root root 537 Dec  4 10:03 50x.html
    -rw-r--r--. 1 root root 612 Dec  4 10:03 index.html
    drwxr-xr-x. 3 root root  58 Dec 14 21:37 my_django
    lrwxrwxrwx  1 root root  33 Dec 15 09:56 ok-admin -> /var/webroot/app-2018-12-15-20566
  •   3.編輯之前的my-freestyle-job,把gitlab的git地址配置到這個任務的原始碼管理裡
    •   
  •   構建觸發器
    •   
    • 需要將 GitLab CI Service URL 和 Secret Token 配置到GitLab的該git專案的settings-->intergrations中,並test下push event
    • 4.本地看下瀏覽器
  •   再次在本地專案中編輯後再看下測試結果
    [email protected] MINGW64 /e/python/ok-admin (master)
    $ git fetch
    
    $ vim index.html
    
    [email protected]-BC8FMN2 MINGW64 /e/python/ok-admin (master)
    $ git add .
    
    $ git commit -m "modified index.html "
    [master 03743e9] modified index.html
     1 file changed, 2 insertions(+), 2 deletions(-)
    
    [email protected]-BC8FMN2 MINGW64 /e/python/ok-admin (master)
    $ git push -u origin master
    Counting objects: 3, done.
    Delta compression using up to 6 threads.
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 321 bytes | 321.00 KiB/s, done.
    Total 3 (delta 2), reused 0 (delta 0)
    To 192.168.23.211:py3web/ok-admin.git
       b731093..03743e9  master -> master
    Branch 'master' set up to track remote branch 'master' from 'origin'.

  •  

  • ok,已經看到了自動部署的結果