jenkins自動部署windwos服務器


jenkins 持續構建windows 項目

需求說明

  公司新購windwos服務器,並配置了堡壘機,由於經常要提交代碼進行更新,導致手動部署很是麻煩,故采用公司jenkins實行持續構建

  jenkins服務部署到linux服務器,項目代碼存放到windows服務器

環境說明

  服務端: Centos7

  客戶端: Windows Server 2012 R2

軟件說明

  python 3.5.6

  winrm 'winRM服務是windows server下PowerShell的遠程管理服務。Python腳本通過連接winRM模塊操作windows命令行'

操作過程

配置windows服務器

  • 查看winRM服務狀態,默認都是未啟動狀態

   winrm e winrm/config/listener

      由於之前我已經配置過了,所以已經啟動監聽

  

  • 開啟winRM

   winrm quickconfig

    

  •  為winrm service 配置auth

   winrm set winrm/config/service/auth '@{Basic="true"}'

  •  為winrm service 配置加密方式為允許非加密

    winrm set winrm/config/service '@{AllowUnencrypted="true"}'

配置jenkins服務

  • 選擇自由風格項目  

shell命令'進入你要構建的windows項目目錄下,執行python腳本',這個路徑是我搭建的gogs路徑,root下一個項目

配置了linux python腳本 

cd /usr/local/cpgroup/git/gogs-repositoriesd/root/saasiorder.git/hooks

#!/usr/bin/python
import winrm import sys import re tag_name=sys.argv[1] build_name=sys.argv[2] Host='windows server 地址' Port='5985' User='windwos server 用戶名 如:adminstrator' Pass='密碼'

def Running(host,port,user,passwd): win2012 = winrm.Session('http://' + host + ':' + port + '/wsman',auth=(user,passwd)) try: r1=win2012.run_cmd('cd /d C:/Users/Administrator/Desktop/ && python deploy.py %s %s'%(tag_name,build_name)) print(r1.std_out.decode()) except BaseException as e: print ('部署異常,%s'%e) if __name__ == '__main__': Running(Host,Port,User,Pass)

 #tag_name 表示選擇那個標簽進行構建,本人的項目操作是通過打標簽的方式進行構建,其他人請自行更改
 #build_name 表示當前構建的項目名稱

配置windows python 腳本

#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:hc.li
from subprocess import PIPE, Popen import os, sys Source_path = os.getcwd() Iorder_Repo_path = r'D:\SAASiOrder' Iorder_Project_path = r"D:\iOrder"

def cmdline(command): process = Popen( args=command, stdout=PIPE, shell=True ) return process.communicate()[0] # tag_name='release-20190403-02'
tag_name = sys.argv[1] build_name = sys.argv[2] def Check_Tag(path): os.chdir(path) cmdline('git checkout master') cmdline('git pull') Status_code = cmdline('git tag |findstr %s' % tag_name)   #當前方法進行判斷標簽使用,如果沒有使用標簽構建 則將當前方法注釋
    Status_code = str(Status_code, encoding='utf8') if not Status_code: print('標簽不存在,請檢查...') return
    else: return Status_code def Update_Code_Iorder(): try: res = Check_Tag(Iorder_Repo_path)           #判斷標簽是否存在
        if not res: return
        print(res) cmdline('git checkout %s' % tag_name)       #如果傳入標簽值存在 則切換到當前標簽
        rs = cmdline("xcopy " + Iorder_Repo_path + "\\*.* " + Iorder_Project_path + " /s/e/y")   #執行拷貝命令
        print(rs) except BaseException as e: print('iorder 更新失敗 %s'%e) if __name__ == '__main__': Update_Code_Iorder()

 后續

總結:第一次操作linux下jenkins構建windwos服務器,執行的cmd命令都在linux下的腳本中執行,導致出現很多問題(拷貝失敗,無法創建文件夾等),盡量將linux下的腳本做成調用腳本,去調用遠程服務器上的python腳本。

並使用windows下的python腳本去執行其他的所有操作並返回結果。jenkins會直接輸出到控制台

如果該windwos服務器上搭建多個項目,可以將其他的操作 整合到windwos下python腳本,jenkins將工程名稱傳入過去進行判斷去執行那個項目的更新。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM