首页 > 系统相关 >Jenkins之shell脚本分享

Jenkins之shell脚本分享

时间:2022-11-24 15:12:01浏览次数:62  
标签:shell string path BuildTarget Jenkins 分享 true GetArg

1.一键打ab包,包括拉项目,打包,上传ftp

#!/bin/bash

echo "---------------------------------------------->Invoke BuildAB.sh<----------------------------------------------"
pwd

m_UnityPath=${1}
m_ProjFolder=${2}
m_Platform=${3}
m_ForceRebuild=${4}
m_FtpUrl=${5}
m_FtpDir=${6}
m_CdnUrl=${7}
m_FtpRSA=${8}
m_FtpPort=${9}
m_FtpUser=${10}


# 项目拉到最新
if [ $m_UpdateMain = "true" ];then
    echo "---------------------------------------------->Update main project<----------------------------------------------"
    cd $m_ProjFolder
    git checkout .;git clean -fd;git pull;git log -1
fi

if [ $m_UpdateArt = "true" ];then
    echo "---------------------------------------------->Update art project<----------------------------------------------"
    cd $m_ProjFolder/UnityClient/Assets/FashionBeat_ArtSVN
    svn cleanup;svn revert --recursive .;svn update;svn log -l 1
fi

if [ $m_UpdateMusic = "true" ];then
    echo "---------------------------------------------->Update music project<----------------------------------------------"
    cd $m_ProjFolder/UnityClient/Assets/FashionBeat_MusicGit
    git checkout .;git clean -fd;git pull;git log -1
fi

if [ $m_ConvertConfig = "true" ];then
    echo "---------------------------------------------->Update excel config<----------------------------------------------"
    cd $m_ProjFolder/../server
    svn cleanup;svn revert --recursive .;svn update;svn log -l 1
fi

# Unity打包
if [ $m_ConvertConfig = "true" -o $m_BuildAB = "true" ];then
    echo "---------------------------------------------->Unity, JenkinsTool.BuildAB<----------------------------------------------"
    
    echo "m_UnityPath:$m_UnityPath"
    
    "$m_UnityPath" -projectPath "$m_ProjFolder/UnityClient" \
    -buildTarget $m_Platform \
    -logFile "${WORKSPACE}/Publish/log/log_BuildAB.txt" \
    -executeMethod JenkinsTool.BuildAB \
    -quit -batchmode -nographics \
    -buildParams \
    "m_ConvertConfig=$m_ConvertConfig" \
    "m_BuildAB=$m_BuildAB" \
    "m_CopyABToStream=$m_CopyABToStream" \
    "m_BuildTarget=$m_Platform" \
    "m_ForceRebuild=$m_ForceRebuild" \
    || exit 1
    
    cd $m_ProjFolder
    git add "UnityClient/Assets/Config/Tbl";git add "UnityClient/Assets/Scripts/XLua/LuaScripts/Config"
    
    #if [ $m_Platform = "iOS" -a $m_CopyABToStream = "true" ];then
    #    git add "UnityClient/Assets/StreamingAssets";
    #fi
    
    git pull;git commit -m "Jenkins: 上传打包结果";git push;git show head --stat
    
    echo "Unity build succeed"
fi

# Ftp上传
if [ $m_UploadFtp = "true" ];then
    echo "---------------------------------------------->Rsync ab to ftp<----------------------------------------------"
    cd ${m_ProjFolder}

    ssh -i ${JenkinsCode}/rsa/${m_FtpRSA} -p ${m_FtpPort} ${m_FtpUser}@${m_FtpUrl} "mkdir -p ${m_CdnUrl}/${m_FtpDir}/"
    rsync -arze "ssh -i ${JenkinsCode}/rsa/${m_FtpRSA} -p ${m_FtpPort}" --progress "UnityClient/a/" ${m_FtpUser}@${m_FtpUrl}:${m_CdnUrl}/${m_FtpDir}/

    echo "Upload to ftp succeed"
fi

echo "---------------------------------------------->BuildAB.sh invoked finished!<----------------------------------------------"

 

2.一键导出项目

#!/bin/bash

echo "---------------------------------------------->Invoke BuildApkExportProj.sh<----------------------------------------------"
pwd

m_UnityPath=${1}
m_ProjFolder=${2}
m_Platform=${3}

m_ExportPath="${WORKSPACE}/Publish/export/UnityExportProject/buildplayer"

# 杀unity进程
if [ $m_KillUnity = "true" ];then
    echo "---------------------------------------------->Kill Unity.exe<----------------------------------------------"
    ${JenkinsCode}/KillUnity.bat
fi

# 清理XLua code
if [ $m_ClearLua = "true" -o $m_GenerateLua = "true" ];then
    echo "---------------------------------------------->Clear lua code<----------------------------------------------"
    luaGenFolder="${m_ProjFolder}/UnityClient/Assets/ThirdParty/XLua/Gen"
    rm -rf $luaGenFolder
    echo "Delete folder:"${luaGenFolder}
fi

# 项目拉到最新
if [ $m_UpdateMain = "true" ];then
    echo "---------------------------------------------->Update main project<----------------------------------------------"
    cd $m_ProjFolder
    git checkout .;git clean -fd;git pull;git log -1
fi

# Unity打包
if [ $m_BuildPlayer = "true" ];then
    echo "---------------------------------------------->Unity, JenkinsTool.BuildPlayer<----------------------------------------------"

    rm -rf $m_ExportPath
    echo "Delete folder:"${m_ExportPath}

    "$m_UnityPath" -projectPath "$m_ProjFolder/UnityClient" \
    -buildTarget $m_Platform \
    -logFile "${WORKSPACE}/Publish/log/log_BuildApkExportProj.txt" \
    -executeMethod JenkinsTool.BuildPlayer \
    -quit -batchmode -nographics \
    -buildParams \
    "m_ExportPath=$m_ExportPath" \
    "m_GenerateLua=$m_GenerateLua" \
    || exit 1
    
    echo "Unity build player finished:${m_ExportPath}"
fi

# 压缩
if [ $m_ZipFiles = "true" ];then
    echo "---------------------------------------------->Zip export files<----------------------------------------------"
    zipPath="${WORKSPACE}/Publish/export/BuildApkExportProj.zip"

    cd "$m_ExportPath"
    rm -rf "${zipPath}"
    zip -r "${zipPath}" ./*

    echo "Zip finished:${zipPath}"
fi

echo "---------------------------------------------->BuildApkExportProj.sh invoked finished!<----------------------------------------------"

 

3.一键出不带sdk的apk

#!/bin/bash

echo "---------------------------------------------->Invoke BuildApkNoSDK.sh<----------------------------------------------"
pwd

m_UnityPath=${1}
m_ProjFolder=${2}
m_Platform=${3}

m_ExportPath="${WORKSPACE}/Publish/export/no_sdk.apk"

# 杀unity进程
if [ $m_KillUnity = "true" ];then
    echo "---------------------------------------------->Kill Unity.exe<----------------------------------------------"
    ${JenkinsCode}/KillUnity.bat
fi

# 清理XLua code
if [ $m_ClearLua = "true" -o $m_GenerateLua = "true" ];then
    echo "---------------------------------------------->Clear lua code<----------------------------------------------"
    luaGenFolder="${m_ProjFolder}/UnityClient/Assets/ThirdParty/XLua/Gen"
    rm -rf $luaGenFolder
    echo "Delete folder:"${luaGenFolder}
fi

# 项目拉到最新
if [ $m_UpdateMain = "true" ];then
    echo "---------------------------------------------->Update main project<----------------------------------------------"
    cd $m_ProjFolder
    git checkout .;git clean -fd;git pull;git log -1
fi

# Unity打包
if [ $m_BuildPlayer = "true" ];then
    echo "---------------------------------------------->Unity, JenkinsTool.BuildAPK<----------------------------------------------"

    rm -rf $m_ExportPath
    echo "Delete:"${m_ExportPath}

    "$m_UnityPath" -projectPath "$m_ProjFolder/UnityClient" \
    -buildTarget $m_Platform \
    -logFile "${WORKSPACE}/Publish/log/log_BuildApkNoSDK.txt" \
    -executeMethod JenkinsTool.BuildAPK \
    -quit -batchmode -nographics \
    -buildParams \
    "m_ExportPath=$m_ExportPath" \
    "m_GenerateLua=$m_GenerateLua" \
    || exit 1
    
    echo "Unity build player finished"
fi

echo "---------------------------------------------->BuildApkNoSDK.sh invoked finished!<----------------------------------------------"

 

4.一键出带sdk的apk

#!/bin/bash

echo "---------------------------------------------->Invoke BuildApkWithSDK.sh<----------------------------------------------"
pwd

m_UnityPath=${1}
m_ProjFolder=${2}
m_Platform=${3}
m_ProductCode=${4}

# 杀unity进程
if [ $m_KillUnity = "true" ];then
    echo "---------------------------------------------->Kill Unity.exe<----------------------------------------------"
    ${JenkinsCode}/KillUnity.bat
fi

# 清理XLua code
if [ $m_ClearLua = "true" -o $m_GenerateLua = "true" ];then
    echo "---------------------------------------------->Clear lua code<----------------------------------------------"
    luaGenFolder="${m_ProjFolder}/UnityClient/Assets/ThirdParty/XLua/Gen"
    rm -rf $luaGenFolder
    echo "Delete folder:"${luaGenFolder}
fi

# 项目拉到最新
if [ $m_UpdateMain = "true" ];then
    echo "---------------------------------------------->Update main project<----------------------------------------------"
    cd $m_ProjFolder
    git checkout .;git clean -fd;git pull;git log -1
fi

# SDK项目拉到最新
if [ $m_UpdateSDKProj = "true" ];then
    echo "---------------------------------------------->Update sdk project<----------------------------------------------"
    cd $SDKCode
    git checkout .;git clean -fd;git pull;git log -1
fi

# Unity导出
if [ $m_ExportUnityProj = "true" ];then
    echo "---------------------------------------------->Unity, JenkinsTool.BuildPlayer<----------------------------------------------"

    exportPath="${SDKCode}/../u2as"
    rm -rf $exportPath
    echo "Delete folder:"${exportPath}

    "$m_UnityPath" -projectPath "$m_ProjFolder/UnityClient" \
    -buildTarget $m_Platform \
    -logFile "${WORKSPACE}/Publish/log/log_BuildApkWithSDK.txt" \
    -executeMethod JenkinsTool.BuildPlayer \
    -quit -batchmode -nographics \
    -buildParams \
    "m_ExportPath=$exportPath" \
    "m_GenerateLua=$m_GenerateLua" \
    || exit 1
    
    echo "Unity build player finished:${exportPath}"
fi

# 集成sdk
if [ $m_BuildSDK = "true" ];then
    echo "---------------------------------------------->Build apk with sdk<----------------------------------------------"
    
    cd $SDKCode
    
    curTime=`date +%Y%m%d_%H%M%S`
    m_ApkName="${m_Region,,}_${curTime}.${m_TargetType,,}"
    echo "apk name: ${m_ApkName}"

    python -u "Python_script/jekinsgames.py" \
    "${m_ProductCode}" \
    "${m_Region}" \
    "${m_VersionAddress}" \
    "${m_TargetType}" \
    "${m_SplitPackage}" \
    "${m_VersionName}" \
    "${m_VersionCode}" \
    "${m_ApkName}"
fi

# 将apk移到Jenkins目录
cd "${SDKCode}/../apk"
tarFolder="${WORKSPACE}/Publish/export"
mkdir -p "${tarFolder}"
mv *.apk "${tarFolder}"

echo "---------------------------------------------->BuildApkWithSDK.sh invoked finished!<----------------------------------------------"

 

5.导出本地化翻译

#!/bin/bash

echo "---------------------------------------------->Invoke ExportLocFile.sh<----------------------------------------------"
pwd

m_UnityPath=${1}
m_ProjFolder=${2}
m_Platform=${3}
m_Language=${4}


# 项目拉到最新
if [ $m_GetChinese = "true" ];then
    echo "---------------------------------------------->Update excel config<----------------------------------------------"
    cd $m_ProjFolder/../server
    svn cleanup;svn revert --recursive .;svn update;svn log -l 1
fi
    
echo "---------------------------------------------->Update main project<----------------------------------------------"
cd $m_ProjFolder
git checkout .;git clean -fd;git pull;git log -1


# Unity打包
echo "---------------------------------------------->Unity, JenkinsTool.ExportLoc<----------------------------------------------"

"$m_UnityPath" -projectPath "$m_ProjFolder/UnityClient" \
-buildTarget $m_Platform \
-logFile "${WORKSPACE}/Publish/log/log_ExportLocFile.txt" \
-executeMethod JenkinsTool.ExportLoc \
-quit -batchmode -nographics \
-buildParams \
"m_GetChinese=$m_GetChinese" \
"m_ExportPath=${WORKSPACE}/Publish/loc" \
"m_Language=$m_Language" \
|| exit 1

echo "Unity build player finished."

#上传
cd $m_ProjFolder
git add "UnityClient/Assets/Localize/Text";git pull;git commit -m "Jenkins: 从配置和静态UI中提取本地化";git push;git show head

echo "---------------------------------------------->ExportLocFile.sh invoked finished!<----------------------------------------------"

 

6.导入本地化翻译

#!/bin/bash

echo "---------------------------------------------->Invoke ImportLocFile.sh<----------------------------------------------"
pwd

m_UnityPath=${1}
m_ProjFolder=${2}
m_Platform=${3}
m_Language=${4}

# 项目拉到最新
echo "---------------------------------------------->Update main project<----------------------------------------------"
cd $m_ProjFolder
git checkout .;git clean -fd;git pull;git log -1

# Unity打包
echo "---------------------------------------------->Unity,JenkinsTool.ImportLoc<----------------------------------------------"

"$m_UnityPath" -projectPath "$m_ProjFolder/UnityClient" \
-buildTarget $m_Platform \
-logFile "${WORKSPACE}/Publish/log/log_ImportLocFile.txt" \
-executeMethod JenkinsTool.ImportLoc \
-quit -batchmode -nographics \
-buildParams \
"m_LocalizedExcel=${WORKSPACE}/m_LocalizedExcel" \
"m_Language=$m_Language" \
|| exit 1

echo "Unity import localize file finished."

#上传
cd $m_ProjFolder
git add "UnityClient/Assets/Localize/Text";git add "UnityClient/Assets/Resources/FashionBeat/Localize";git pull;git commit -m "Jenkins: 导入已翻译的本地化文件";git push;git show head

echo "---------------------------------------------->ImportLocFile.sh invoked finished!<----------------------------------------------"

 

7.杀unity进程bat

::tasklist | findstr Unity*
taskkill /f /im Unity*

 

8.放在unity中的cs代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using CSObjectWrapEditor;
using UnityEditor;
using UnityEngine;
using YooAsset.Editor;

public class JenkinsTool : ScriptableObject
{
    static string[] BuildingScenes
    {
        get
        {
            List<string> scenes = new List<string>();
            foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
            {
                if (scene != null && scene.enabled)
                    scenes.Add(scene.path);
            }
            return scenes.ToArray();
        }
    }


    public static string GetArg(string name, string defaultArg)
    {
        if (string.IsNullOrEmpty(name))
            return defaultArg;

        string[] args = Environment.GetCommandLineArgs();
        string argStart = string.Format("{0}=", name);

        foreach (string arg in args)
        {
            if (arg.StartsWith(argStart))
            {
                string argFixed = arg.Substring(argStart.Length);
                return string.IsNullOrEmpty(argFixed) ? defaultArg : argFixed;
            }
        }

        return defaultArg;
    }

    public static void BuildAB()
    {
        bool convertConfig = GetArg("m_ConvertConfig", "false") == "true";
        bool buildAB = GetArg("m_BuildAB", "false") == "true";
        string strBuildTarget = GetArg("m_BuildTarget", "Android");
        BuildTarget buildTarget = (BuildTarget)Enum.Parse(typeof(BuildTarget), strBuildTarget);

        if (convertConfig)
            ConvertConfig();
        if (buildAB)
            YooAssetBuild(buildTarget);

        Debug.Log("#Jenkins, JenkinsTool.BuildAB() finished! ");
    }

    [MenuItem("FashionBeat/Jenkins/ConvertConfig")]
    static void ConvertConfig()
    {
        ConfigConverter.JenkinsConvertConfig();
        Debug.Log("#Jenkins, ConfigConverter.ConvertAllConfig() finished! ");
    }

    static void YooAssetBuild(BuildTarget buildTarget)
    {
        Debug.Log($"#Jenkins, Yoo asset 开始构建 : {buildTarget}");

        bool forceRebuild = GetArg("m_ForceRebuild", "false") == "true";
        var buildMode = forceRebuild ? EBuildMode.ForceRebuild : EBuildMode.IncrementalBuild;

        bool copyAB = GetArg("m_CopyABToStream", "false") == "true";
        ECopyBuildinFileOption copyOption = copyAB ? ECopyBuildinFileOption.ClearAndCopyByTags : ECopyBuildinFileOption.None;
        string copyTags = copyAB ? "local;" : "";

        Debug.Log("m_ForceRebuild=" + forceRebuild);
        Debug.Log("m_CopyABToStream=" + copyAB);

        // 构建参数
        BuildParameters p = new BuildParameters()
        {
            OutputRoot = AssetBundleBuilderHelper.GetDefaultOutputRoot(),
            BuildTarget = buildTarget,
            BuildPipeline = EBuildPipeline.BuiltinBuildPipeline,
            BuildMode = buildMode,
            BuildPackage = "DefaultPackage",
            HumanReadableVersion = "a",
            VerifyBuildingResult = true,
            EnableAddressable = true,
            EncryptionServices = null,
            OutputNameStyle = EOutputNameStyle.HashName,
            CopyBuildinFileOption = copyOption,
            CopyBuildinFileTags = copyTags,
            CompressOption = ECompressOption.LZ4,
        };

        // 执行构建
        AssetBundleBuilder builder = new AssetBundleBuilder();
        var buildResult = builder.Run(p);

        if (copyAB)
        {
            YooAssetTool.CopyBundleToStream();
        }

        Debug.Log($"#Jenkins, ConfigConverter.YooBuildInternal() finished,result {buildResult.Success}");
    }

    [MenuItem("FashionBeat/Jenkins/BuildPlayer")]
    public static void BuildPlayer()
    {
        bool generateLua = GetArg("m_GenerateLua", "false") == "true";
        if (generateLua)
        {
            Generator.ClearAll();
            Generator.GenAll();
        }

        string path = GetArg("m_ExportPath", "");
        if (string.IsNullOrEmpty(path))
        {
            path = "../../export";
        }

        if (Directory.Exists(path))
        {
            Directory.Delete(path, true);
        }

        BuildTarget target = BuildTarget.Android;
        BuildOptions options = BuildOptions.None;
        EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
        EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Gradle;
        BuildPipeline.BuildPlayer(BuildingScenes, path, target, options);
    }

    [MenuItem("FashionBeat/Jenkins/BuildAPK")]
    public static void BuildAPK()
    {
        bool generateLua = GetArg("m_GenerateLua", "false") == "true";
        if (generateLua)
        {
            Generator.ClearAll();
            Generator.GenAll();
        }

        string path = GetArg("m_ExportPath", "");
        if (string.IsNullOrEmpty(path))
        {
            path = "../../1.apk";
        }

        Debug.Log("m_ExportPath=" + path);

        if (File.Exists(path))
            File.Delete(path);

        string dirPath = Path.GetDirectoryName(path);
        if (!Directory.Exists(dirPath))
            Directory.CreateDirectory(dirPath);

        BuildTarget target = BuildTarget.Android;
        BuildOptions options = BuildOptions.None;
        EditorUserBuildSettings.exportAsGoogleAndroidProject = false;
        //EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Gradle;
        BuildPipeline.BuildPlayer(BuildingScenes, path, target, options);
    }

    [MenuItem("FashionBeat/Jenkins/ExportLoc")]
    public static void ExportLoc()
    {
        bool getChinese = GetArg("m_GetChinese", "true") == "true";
        string folder = GetArg("m_ExportPath", "C:\\");
        Language lan = (Language)Enum.Parse(typeof(Language), GetArg("m_Language", "English"));
        if (getChinese)
        {
            LocalizationTools.Jenkins_GetChinese();
        }
        LocalizationTools.Jenkins_ExportUnloc(lan, folder);
        LocalizationTools.Jenkins_ExportAllLocText(folder);
    }

    public static void ImportLoc()
    {
        string path = GetArg("m_LocalizedExcel", "");
        Language lan = (Language)Enum.Parse(typeof(Language), GetArg("m_Language", "English"));
        if (!string.IsNullOrEmpty(path))
        {
            LocalizationTools.Localize(lan, path);
        }
    }

}

 

9,配置在jenkins中的shell代码,如:

#!${GitBashPath}
sh ${JenkinsCode}/BuildApkWithSDK.sh "${Unity2020_341}" "${ProjectTwAndroid}" "Android" ${m_ProductCode}

 

转载请注明出处:https://www.cnblogs.com/jietian331/p/16921891.html

标签:shell,string,path,BuildTarget,Jenkins,分享,true,GetArg
From: https://www.cnblogs.com/jietian331/p/16921891.html

相关文章

  • s3 upload files with shell script
    TL;DR#!/usr/bin/envbash#hangj.cnblogs.coms3_key="Q3AM3UQ867SPQQA43P2F"s3_secret="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"session_token="Security-Tok......
  • 案例分享:交通运输可视化
    交通是城市经济发展的动脉,与我们的日常生活息息相关。 传统交通信息管理中,只是做了粗略的信息发布以及简单的交通流量监测,早已经不能满足现代智慧交通的需求。现代的智......
  • 12.jenkins RBAC权限管理
    在实际工作中,存在多个团队都需要Jenkins来实现持续交付,但是又希望不同团队之间进行隔离,每个项目有自己的view,只能看到自己项目的jenkinsjob.但是,jenkins默认的权限管理......
  • [原创]shell创建文件或者文件夹快捷方式到桌面的脚本shell
    刚开始学shell编程写的#!/bin/bashvartest='我是变量,变量读取和gradle类似,用${}访问'echo变量长度${#vartest}echo"支持直接不输入引号echo,"#为毛直接显示了。'$e{,ee......
  • [每日分享]android获取文件目录总大小实现清除缓存
    跟大家讲一个笑话,我维护老项目,看到有一个清除缓存功能,我看了一下怎么实现的,结果就一个​​Toast​​太逗比了/***获取指定文件夹的大小**@paramf......
  • Jenkins git访问拒绝
    Jenkinsgit访问拒绝Mikasa___关注2018.07.3114:36:31字数196阅读4,649问题一Failedtoconnecttorepository:Command"[email protected]:......
  • 【视频】K近邻KNN算法原理与R语言结合新冠疫情对股票价格预测|数据分享|附代码数据
    全文下载链接:http://tecdat.cn/?p=24057本文的目标是使用K-最近邻(K近邻),ARIMA和神经网络模型分析Google股票数据集预测Google的未来股价,然后分析各种模型 ( 点击文末“阅......
  • 「案例分享」研发效能提升之第一性原理
    作者:樊思国一、引言被埃隆·马斯克多次提及的第一性原理Firstprinciplethinking,是计算物理学领域的一个专业术语,在商业领域依然具有鲜活的生命力。读过《硅谷钢铁侠》这本......
  • 技术分享| 快对讲视频调度功能说明
    随着计算机技术的日趋成熟,融合调度方案已经在行业信息化中普及,由于近几年实时音视频能力的提升,融合调度中的视频调度方案也在往实时性、高清方向靠拢。快对讲视频调度正是结......
  • 技术分享| 快对讲视频调度功能说明
    随着计算机技术的日趋成熟,融合调度方案已经在行业信息化中普及,由于近几年实时音视频能力的提升,融合调度中的视频调度方案也在往实时性、高清方向靠拢。快对讲视频调度正是......