1、新建Item,输入名称后选择Freestyle project后点击确定

CocosCreator基于<a class=jenkins自动构建_服务器" style="outline: none;" />

2、配置项目,自定义工作目录

CocosCreator基于<a class=jenkins自动构建_json_02" style="outline: none;" />

3、配置源码管理和要摘取的分支

CocosCreator基于<a class=jenkins自动构建_github_03" style="outline: none;" />

CocosCreator基于<a class=jenkins自动构建_github_04" style="outline: none;" />

4、构建触发器选择github触发

CocosCreator基于<a class=jenkins自动构建_github_05" style="outline: none;" />

5、构建选择执行windows命令,之后点击保存

CocosCreator基于<a class=jenkins自动构建_json_06" style="outline: none;" />

#--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,tarfileclass 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 = hostnameself.username = usernameself.port = portself.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, reserrdef 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_codedef 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)

CocosCreator基于<a class=jenkins自动构建_github_07" style="outline: none;" />

CocosCreator基于<a class=jenkins自动构建_服务器_08" style="outline: none;" />

CocosCreator基于<a class=jenkins自动构建_json_09" style="outline: none;" />

7、启动ngrok,把forwarding地址填写到github

ngrok http http://172.18.188.8:8080

CocosCreator基于<a class=jenkins自动构建_json_10" style="outline: none;" />

注意:

     如果卡在([1900] checking language setting...)检查语言设置,请检查jenkins服务登录设置,需要新建一个管理员账号,然后使用新建的管理员账号启动jenkins

CocosCreator基于<a class=jenkins自动构建_服务器_11" style="outline: none;" />

CocosCreator基于<a class=jenkins自动构建_服务器_12" style="outline: none;" />

参考链接:
       https://ngrok.com/download   #ngrok下载链接
       https://github.com/FastTunnel/FastTunnel/releases     # FastTunnel内网穿透