1. 程式人生 > 其它 >Jenkins之job型別部署Django

Jenkins之job型別部署Django

Jenkins之job型別部署Django

1、建立Job

1.1、基礎部分

1.2、引數化

1.2.1、文字引數(變數)

​ 輸出內容

​ 構建

​ 控制檯輸出

1.2.2、布林引數

線上構建執行shell加入echo $名稱

構建

輸出結果

1.2.3、密碼引數

可以在下面新增一條shell裡echo一下看看內容

構建

輸出結果

1.2.4、選項引數

可以在下面新增一條shell裡echo一下看看內容

構建

輸出結果

1.2.5、List Git branches引數(分支)

構建

1.2.6、級聯引數

在新增一個 級聯

改內容

構建頁面

1.2.7、隱藏引數

可以在下面新增一條shell裡echo一下看看內容

構建

輸出結果

1.2.8、憑證引數

可以在下面新增一條shell裡echo一下看看內容

構建輸出憑證id

2、憑證

2.1、jenkins拉去girlab的程式碼
2.2、缺少憑證 建立憑證

連線

直接儲存即可

構建輸出

拉下來的程式碼

3、構建觸發器

每分鐘執行一次 其實就是使用crontab表示式構建

3.1、其他工程構建後觸發

通過回撥鉤子方式來構建

3.2、將更改推送到 GitLab 時構建

程式碼一提交到gitlab 、jenkins就構建

要去掉安全認證

加上token連線就好了

4.構建

執行在jenkins伺服器上,沒有在遠端伺服器執行,構建後操作是傳送到遠端伺服器上

4、部署Django

4.1、部署實驗環境

先部署一個標籤 然後 構建裡執行shell git checkout $list git branaches的名字 找到node1伺服器 安裝python3-devel、django、uwsgi

遠端連線機器

如果失敗 給機器做免密 在jenkins給node01做免密

yum install python3 python3-devel -y
pip3 install django==2.2.2
pip3 install uwsgi


#node01 實驗環境
mkdir /opt/linux

yum install nginx -y

vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
}


vim /etc/nginx/conf.d/django.conf

server {
        # 監聽的埠
        listen 80;
        # 配置域名
        server_name bbs.test.com;
        # 配置路徑
        location / {
                # 載入Nginx代理uwsgi的配置項 
                include uwsgi_params;
                # 指定uwsgi的訪問地址
                uwsgi_pass 127.0.0.1:8000;
                # 連線uwsgi的超時時間
                uwsgi_read_timeout 2;
                # 自定義uwsgi代理專案的路徑及配置項
                uwsgi_param UWSGI_SCRIPT linux.wsgi;
                # 指定python專案的路徑
                uwsgi_param UWSGI_CHDIR /opt/linux;
                # 索引檔案
                index  index.html index.htm;
                # 客戶端上傳檔案的最大值
                client_max_body_size 35m;
        }
}

4.2、在Jenkins中增加實驗環境

總結

gitlib作為倉庫  Python上傳程式碼到gitlab   ,jenkins 設定了只有gitlab push了 就拉取程式碼 連線gitlab通過了jenkins的私鑰  ,拉取到 genkins伺服器下面的/var/lib/jenkins/workspace/cao, 然後又設定構建後操作  先判斷pid檔案是不是存在,重新啟動uwsgi: /usr/local/bin/uwsgi --reload  /var/run/haha.pid
不在啟動uwsgi :/usr/local/bin/uwsgi   --uid 666 --gid 666 myweb.ini

5、流水線

5.1、片段生成

5.2、宣告性指令生成器

5.3、生成切換分支指令碼

5.4、部署程式碼生成

5.5基本語法

pipeline {   #pipeline流水線的意思  有且只有一個
    agent any   #agent:指定環境,any當前環境   還有引數:docker、kubernetes、dockerfile
    stages {       #代表某個階段,階段可以有多個步驟
      stage('拉取程式碼') {  #步驟
        steps {
          git credentialsId: '42c93c1c-de13-42ad-a7f7-6df55d16f770', url: '[email protected]:aoteman/test.git'
        }
      }

    }
}

5.6、組合之後

pipeline {   
    agent any   
    stages {       
      stage('拉取程式碼') { 
        steps {
          git credentialsId: '42c93c1c-de13-42ad-a7f7-6df55d16f770', url: '[email protected]:aoteman/test.git'
        }
      }

    }
    stages {
      stage('切換分支') {
        steps {
            sh """#!/bin/bash

            git checkout $GIT_TAG"""
        }
      }

    }
    stages {
      stage('部署程式碼') {
        steps {
            sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s-node-01', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''#!/bin/bash
cd /opt/haha
if [ -f  /var/run/haha.pid];then
    /usr/local/bin/uwsgi --reload  /var/run/haha.pid
else
    /usr/local/bin/uwsgi   --uid 666 --gid 666 myweb.ini   
fi''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '**/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
        }
      }

    }

}

6、自動生成流水線