1. 程式人生 > 其它 >CocosCreator基於jenkins自動構建

CocosCreator基於jenkins自動構建

1、新建Item,輸入名稱後選擇Freestyle project後點擊確定

2、配置專案,自定義工作目錄

3、配置原始碼管理和要摘取的分支

4、構建觸發器選擇github觸發

5、構建選擇執行windows命令,之後點選儲存

#--disable-gpu,跳過語言設定,如不加此選項構建時會卡在語言設定,--path,指定構建後文件路徑
echo "開始構建"
C:\CocosDashboard_1.1.0\resources\.editors\Creator\2.4.6\CocosCreator.exe --disable-gpu --path D:\game\Archery --build "platform=web-mobile;debug=false"
echo
"構建完成" echo "開始上傳檔案到伺服器" C:\Python38\python.exe ../../unzip.py echo "檔案上傳完成"
#!/usr/bin/env python
# -*- coding:utf-8 -*-


import os,json
import paramiko,zipfile,tarfile


class comupload(object):
    def __init__(self, hostname, username='root', port=22):
        self.private_key = paramiko.RSAKey.from_private_key_file('
C:\\Users\\southpark\\.ssh\\id_rsa') self.hostname = hostname self.username = username self.port = port self.transport = paramiko.Transport((self.hostname, self.port)) self.transport.connect(username=self.username, pkey=self.private_key) self.sftp = paramiko.SFTPClient.from_transport(self.transport) self.client
= paramiko.SSHClient() self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 允許連線不存在在know_hosts檔案裡的主機 self.client.connect(hostname=self.hostname, port=self.port, username=self.username, pkey=self.private_key) def upload(self, local_path, remote_path): # 將檔案上傳至伺服器 self.sftp.put(local_path, remote_path) def download(self, remotepath, localpath): # 將檔案下載到本地 self.sftp.get(remotepath, localpath) def comand(self, com): # 執行命令 stdin, stdout, stderr = self.client.exec_command(com) result = stdout.read().decode() reserr = stderr.read().decode() return result, reserr def exec_com(self, com): # 執行命令,返回命令結果和狀態碼 self.channel = self.client.get_transport().open_session() self.channel.exec_command(com) stdout = self.channel.makefile().read() stderr = self.channel.makefile_stderr().read() exit_code = self.channel.recv_exit_status() self.channel.close() return stdout, stderr, exit_code def sshclose(self): # 關閉連線 self.sftp.close() self.client.close() def zipDir(dirpath,outFullName): zip=zipfile.ZipFile(outFullName,"w",zipfile.ZIP_DEFLATED) for path,dirnames,filenames in os.walk(dirpath): fpath = path.replace(dirpath,'') for filename in filenames: print(filename,path,fpath) zip.write(os.path.join(path,filename),os.path.join(fpath,filename)) zip.close() def compress_file(dirpath,filename,project=None): cur_path = os.getcwd() os.chdir(dirpath) tarfilename=filename+'.tar.gz' with tarfile.open('../../../tarfile/'+tarfilename,"w") as tar: for root,dirs,files in os.walk('.'): for single_file in files: filepath = os.path.join(root,single_file) tar.add(filepath) sshtftp=comupload('172.17.0.2') filepath='D:\\tarfile\\{}'.format(tarfilename) if project: sshtftp.upload(filepath,'/root/3nm-web/site/game/{}/game/{}'.format(project,tarfilename)) sshtftp.comand("cd /root/3nm-web/site/game/{project}/game && tar xf {tarfilename} && rm -fr {tarfilename} {filename} && mv web-mobile {filename}".format(project=project,tarfilename=tarfilename,filename=filename)) else: sshtftp.upload(filepath,'/root/3nm-web/site/game/publicgame/game/public/{}'.format(tarfilename)) sshtftp.comand("cd /root/3nm-web/site/game/publicgame/game/public && tar xf {tarfilename} && rm -fr {tarfilename} {filename} && mv web-mobile {filename}".format(tarfilename=tarfilename,filename=filename)) sshtftp.sshclose() os.remove(filepath) if __name__ == '__main__': cur_path = os.getcwd() with open("settings/builder.json") as f: res=json.loads(f.read()) filename=res.get('title') project=res.get('project') if project: compress_file('{}\\build\\'.format(cur_path),filename,project) else: compress_file('{}\\build\\'.format(cur_path),filename)
構建完成後上傳檔案到測試伺服器指令碼

6、開啟github-webhook(點選管理jenkins→配置系統→高階→勾選為github指定另外一個HooK URL)


7、啟動ngrok,把forwarding地址填寫到github

ngrok http http://172.18.188.8:8080

參考連結:
       https://ngrok.com/download   #ngrok下載連結
       https://www.cnblogs.com/xiloweiEVE/p/15499112.html
       https://www.cnblogs.com/panda-123/p/14456428.html
       https://www.cnblogs.com/weschen/p/6867885.html
       https://blog.csdn.net/weixin_38320674/article/details/110412976