jenkins 执行git

2013-3-6 雨辰 Git

#杀死所有unity进程
function KillUnityProcess()
{
	PROCESS=`ps -ef|grep Unity|grep -v grep|grep -v PPID|awk '{ print $2}'`
	for i in $PROCESS
	do
	echo "Kill the $1 process [ $i ]"
	kill -9 $i
	done
}

#切换Library目录,参数1:Library名称
function SwitchLibrary()
{
	echo start switch library ...
	
	#删除工程目录下所有的Library文件夹#
	rm -rf ${DEV_PROJ_PATH}/Library

	#创建完整资源模式的库目录
	if [ ! -d ${DEV_PROJ_PATH}/$1 ];then
	mkdir ${DEV_PROJ_PATH}/$1
	fi

	#链接Library文件夹
	ln -s ${DEV_PROJ_PATH}/$1 ${DEV_PROJ_PATH}/Library
	
	echo end switch library
}

#切换平台,参数1:目标平台,参数2:Library名称
function SwitchPlatform()
{	
	SwitchLibrary $2
	echo start switch to platform: $1 ...
	$UNITY_PATH -batchmode -quit -nographics -projectPath $DEV_PROJ_PATH -buildTarget $1
	echo end switch to platform: $1
}

#打Bundle包 参数1:目标平台,参数2:打包方法名称
function BuildBundles()
{
	echo start build assetbundles for platform: $1 ...
	$UNITY_PATH -batchmode -quit -nographics -projectPath ${DEV_PROJ_PATH} -buildTarget $1 -executeMethod $2 -logFile ${LOG_BUNDLE_PATH}
	echo end build assetbundles for platform: $1
}

#Git清理并切换到目标分支
function InitGit()
{
	echo start init git ...
	cd ${DEV_ROOT_PATH}
	git reset --hard
	echo qyg301 | sudo -S git clean -df
	git fetch -p
	git checkout ${BRANCH_NAME}    
	git pull --rebase origin ${BRANCH_NAME}
	echo end init git
}

#剪切Resources目录,参数1:Library名称
function CutResources()
{
	echo start move game assets ...
	
	if [ -e ${RESOURCES_TMP_PATH} ];then
	rm -rf ${RESOURCES_TMP_PATH}
	fi

	if [ ! -d ${RESOURCES_TMP_PATH} ];then
	mkdir -p ${RESOURCES_TMP_PATH}
	fi

	mv -f ${DEV_RESOURCE_PATH} ${RESOURCES_TMP_PATH}
	
	SwitchLibrary $1
	
	echo end move game assets
}

#剪切obb资源目录
function CutObbResources()
{
    echo start move obb assets ...
	OBB_ASSETS_FOLDER=assets
	STREAMING_ASSET_FOLDER=StreamingAssets

	if [ -e ${OBB_TEMP_FOLDER_PATH} ];then
	rm -rf ${OBB_TEMP_FOLDER_PATH}
	fi

	mkdir -p ${OBB_TEMP_FOLDER_PATH}

	mv -f ${PROJECT_STREAMINGASSETS_PATH} ${OBB_TEMP_FOLDER_PATH}
	cd ${OBB_TEMP_FOLDER_PATH}
	mv $STREAMING_ASSET_FOLDER $OBB_ASSETS_FOLDER
	cd ~
	echo end move obb assets
}

#恢复Resources目录,参数1:Library名称
function RecoverResources()
{
	echo start move back game assets ...
	
	mv -f ${RESOURCES_TMP_PATH}/Resources ${DEV_PROJ_PATH}/Assets
	rm -rf ${RESOURCES_TMP_PATH}
	
	SwitchLibrary $1
	
	echo end move back game assets
}

#生成obb文件
function GenerateObb()
{
    #读取AndroidManifest.xml文件unity.build-id值
	UNITY_BUILD_ID=$(cat $ANDROID_MANIFEST_PATH | egrep 'android:value="(.*)-(.*)-(.*)-(.*)-(.*)"' | awk -F '=' '{print $3}' | awk -F '"' '{print $2}')
   
    #创建名为build-id的文件
    BUILD_ID_FILE_PATH=${OBB_TEMP_FOLDER_PATH}/$OBB_ASSETS_FOLDER/$UNITY_BUILD_ID

    if [ -f $BUILD_ID_FILE_PATH ];then
    rm -rf $BUILD_ID_FILE_PATH
    fi

    touch $BUILD_ID_FILE_PATH

    #读取AndroidManifest.xml文件package值
	PACKAGE_NAME=com.thegyee.www #$(cat $ANDROID_MANIFEST_PATH | grep -e 'package=' | awk -F '=' '{print $4}' | awk -F '"' '{print $2}')
    if [ ${BUILD_LANGUAGE} = "EN" ];then
    	PACKAGE_NAME=com.thegyee.sea1 #$(cat $ANDROID_MANIFEST_PATH | grep -e 'package=' | awk -F '=' '{print $4}' | awk -F '"' '{print $2}')
    fi
    
    if [ ${BUILD_LANGUAGE} = "JP" ];then
    	PACKAGE_NAME=com.thegyee.jp #$(cat $ANDROID_MANIFEST_PATH | grep -e 'package=' | awk -F '=' '{print $4}' | awk -F '"' '{print $2}')
    fi
    
    #读取Version.json的AndroidBuild号
    OBB_BUILD_VERSION=$(cat ${DEV_RESOURCE_PATH}/Version.json | grep -e 'AndroidBuild' | awk -F ':' '{print $2}' | sed 's/ //g')
    #最终的压缩文件路径
    OBB_TARGET_ZIP_FILE=/Users/qyg/Desktop/publish_ftp/main.$OBB_BUILD_VERSION.$PACKAGE_NAME.obb

    if [ -f $OBB_TARGET_ZIP_FILE ];then
    rm -rf $OBB_TARGET_ZIP_FILE
    fi

    #压缩该文件夹
    cd ${OBB_TEMP_FOLDER_PATH}
    zip -q -r -0 $OBB_TARGET_ZIP_FILE $OBB_ASSETS_FOLDER
    cd ~
}

#还原obb资源
function RecoverObbResources()
{
    #obb资源移动回原目录
    mv -f ${OBB_TEMP_FOLDER_PATH}/$OBB_ASSETS_FOLDER ${DEV_ASSETS_PATH}
	cd ${DEV_ASSETS_PATH}
	mv $OBB_ASSETS_FOLDER $STREAMING_ASSET_FOLDER
	cd ~

    #删除obb临时目录
    rm -rf ${OBB_TEMP_FOLDER_PATH}
}

#导出Ipa程序包,参数0:导出模式
function ExportIpa()
{
	EXPORT_MODLE=$1

	# 工程版本
	PROJECT_VERSION=$(egrep -o "([0-9]+(\.)*){3,}" ${DEV_RESOURCE_PATH}/Version.json | head -n 1)
	# 工程名
	PROJECT_NAME=Unity-iPhone
	# 工程方案
	PROJECT_SCHEME=$PROJECT_NAME
	#生成路径
	PUBLISH_TEMP_PATH=${DEV_ROOT_PATH}/PublishTemp

	# xcode工程路径
	PROJECT_PATH=$PUBLISH_TEMP_PATH/gyeelegend_ios_${EXPORT_MODLE}_${PROJECT_VERSION}

	# 配置信息 Release版本
	CONFIGURATION="Distribution"

	# 生成路径
	BUILD_PATH=$PUBLISH_TEMP_PATH/Build

	# 导出ipa参数表路径
	EXPROT_OPTIONS_PLIST_PATH=${DEV_PROJ_PATH}/Tools/export_ipa/ExportIpaOptions.plist

	# xcode路径
	XCODE_PATH=/Applications/Xcode.app/Contents/Developer

	# xcode-select查看选中的xcode路径
	XCODE_SELECT_PROCESS=$(xcode-select -p)

	echo XCODE_PROJECT_NAME=gyeelegend_ios_${EXPORT_MODLE}_${PROJECT_VERSION}

	# 1、判定是否选过xcode
	if [ $XCODE_SELECT_PROCESS != $XCODE_PATH ];then
	echo "xcode-select"
	sudo xcode-select -s $XCODE_PATH
	fi

	# 2、清理生成文件夹里的文件
	echo "remove old xcarchive and ipa files"
	if [ -d $BUILD_PATH ];then
	rm -rf $BUILD_PATH
	fi
	mkdir $BUILD_PATH

	# 3、归档
	echo "archive Unity-iPhone.xcarchive file"
	#如果是新的机器,需要执行一次ln -s ~/Library/Keychains/login.keychain-db ~/Library/Keychains/login.keychain
	security unlock-keychain -p qyg301 /Users/qyg/Library/Keychains/Login.keychain
	xcodebuild -project $PROJECT_PATH/${PROJECT_NAME}.xcodeproj -scheme $PROJECT_SCHEME -configuration $CONFIGURATION clean archive -UseModernBuildSystem=NO -archivePath $BUILD_PATH/${PROJECT_NAME}.xcarchive
	echo "archive finish"

	# 4、导出ipa包
	echo "export Unity-iPhone.ipa file"
	xcodebuild -exportArchive -archivePath $BUILD_PATH/${PROJECT_NAME}.xcarchive -exportOptionsPlist $EXPROT_OPTIONS_PLIST_PATH -exportPath $BUILD_PATH
	echo "export finish"
	
	#5、拷贝到发布目录
	IPA_SRC_PATH=/Users/qyg/Desktop/project/PublishTemp/Build/Unity-iPhone.ipa
	IPA_TARGET_PATH=/Users/qyg/Desktop/publish_ftp/gyeelegend_ios_${EXPORT_MODLE}_${PROJECT_VERSION}_${BRANCH_NAME}_${BUILD_ID}.ipa
		
	if [ -e ${IPA_TARGET_PATH} ];then
	rm -rf ${IPA_TARGET_PATH}
	fi
	cp ${IPA_SRC_PATH} ${IPA_TARGET_PATH}
}

function PublishApk()
{	
	EXPORT_MODLE=$1	
    
    #gradle编译安卓包
    cd ${PROJECT_ANDROID_PATH}
    gradle clean
    gradle assembleRelease
	
	# 工程版本
	PROJECT_VERSION=$(egrep -o "([0-9]+(\.)*){3,}" ${DEV_RESOURCE_PATH}/Version.json | head -n 1)
	
	# 拷贝到发布目录
	APK_SRC_PATH=${PROJECT_ANDROID_PATH}/build/outputs/apk/release/gyeelegend-release.apk
	APK_TARGET_PATH=/Users/qyg/Desktop/publish_ftp/gyeelegend_android_${EXPORT_MODLE}_${PROJECT_VERSION}_${BRANCH_NAME}_${BUILD_ID}.apk
	
    if [ ${RECHARGE_TYPE} = "WEB" ];then
		APK_TARGET_PATH=/Users/qyg/Desktop/publish_ftp/gyeelegend_android_web_${EXPORT_MODLE}_${PROJECT_VERSION}_${BRANCH_NAME}_${BUILD_ID}.apk	
    fi
    
	if [ -e ${APK_TARGET_PATH} ];then
	rm -rf ${APK_TARGET_PATH}
	fi
	cp ${APK_SRC_PATH} ${APK_TARGET_PATH}
}

function CopyLog()
{
	if [ -e ${LOG_BUNDLE_PUBLISH_PATH} ];then
	rm -rf ${LOG_BUNDLE_PUBLISH_PATH}
	fi
	cp ${LOG_BUNDLE_PATH} ${LOG_BUNDLE_PUBLISH_PATH}
    
    if [ -e ${LOG_EXPORT_PUBLISH_PATH} ];then
	rm -rf ${LOG_EXPORT_PUBLISH_PATH}
	fi
	cp ${LOG_EXPORT_PATH} ${LOG_EXPORT_PUBLISH_PATH}
}

#使用设置的缓存库
function UseLibraryCache()
{
	echo "start checkout cache branch..."${CACHE_NAME}
	
    if [ ${CACHE_NAME} = "DONT_USE" ];then
    	echo "do not switch cache branch..."    	
    else
    	CACHE_BRANCH_NAME=dev
        if [ ${CACHE_NAME} = "DEV" ];then
            CACHE_BRANCH_NAME=dev
        elif [ ${CACHE_NAME} = "ZH" ];then
            CACHE_BRANCH_NAME=zh
        elif [ ${CACHE_NAME} = "EN" ];then
            CACHE_BRANCH_NAME=en
        elif [ ${CACHE_NAME} = "JP" ];then
            CACHE_BRANCH_NAME=jp
        fi
        
        echo "begin prepare library cache: "${CACHE_BRANCH_NAME}
        cd ${CACHE_PATH}
        git reset --hard
        echo qyg301 | sudo -S git clean -df
        git checkout ${CACHE_BRANCH_NAME}
        git pull origin ${CACHE_BRANCH_NAME}
        echo "prepare library cache complete: "${CACHE_NAME}
    fi
    
    echo "end checkout cache branch..."
}

#清理PackageCache
function ClearPackageCache()
{
	rm -rf Desktop/project/Src/Game301/Game301_Library_Cache/IosLibrary/PackageCache
	rm -rf Desktop/project/Src/Game301/Game301_Library_Cache/IosNoResLibrary/PackageCache
	rm -rf Desktop/project/Src/Game301/Game301_Library_Cache/AndroidLibrary/PackageCache
	rm -rf Desktop/project/Src/Game301/Game301_Library_Cache/AndroidNoResLibrary/PackageCache
	rm -rf Desktop/project/Src/Game301/Game301_Library_Cache/WindowsLibrary/PackageCache
	rm -rf Desktop/project/Src/Game301/Game301_Library_Cache/WindowsNoResLibrary/PackageCache
}


KillUnityProcess
ClearPackageCache
InitGit

UseLibraryCache

#返回根路径
cd ${DEV_ROOT_PATH}
RES_VERSION=$(egrep -o "([0-9]+(\.)*){3,}" ${DEV_RESOURCE_PATH}/Version.json | tail -n 1)
SwitchPlatform ${PLATFORM_ANDROID} ${LIB_ANDROID}

if [ ${IS_BUILD_BUNDLE} = true ];then
    if [ ${BUILD_LANGUAGE} = "ZH" ];then
    	BuildBundles ${PLATFORM_ANDROID} ${PACK_METHOD_ANDROID}
    fi

    if [ ${BUILD_LANGUAGE} = "TW" ];then
    	BuildBundles ${PLATFORM_ANDROID} ${PACK_METHOD_ANDROID_TW}
    fi
    
    if [ ${BUILD_LANGUAGE} = "EN" ];then
    	BuildBundles ${PLATFORM_ANDROID} ${PACK_METHOD_ANDROID_EN}
    fi
    
    if [ ${BUILD_LANGUAGE} = "JP" ];then
    	BuildBundles ${PLATFORM_ANDROID} ${PACK_METHOD_ANDROID_JP}
    fi
fi

CutResources ${LIB_ANDROID_NO_RES}

#剪切obb资源
if [ ${IS_EXPORT_OBB} = true ];then
	CutObbResources
fi

BUILD_NAME=debug

if [ ${IS_RELEASE} = false ];then
    if [ ${BUILD_LANGUAGE} = "ZH" ];then
		if [ ${RECHARGE_TYPE} = "IN_APP" ];then
			EXPORT_METHOD_ANDROID=AutoExport.ExportDebugApk
		elif [ ${RECHARGE_TYPE} = "WEB" ];then
			EXPORT_METHOD_ANDROID=AutoExport.ExportWebRechargeDebugApk
		else
			EXPORT_METHOD_ANDROID=AutoExport.ExportDebugApk
		fi
    fi

    if [ ${BUILD_LANGUAGE} = "TW" ];then
		if [ ${RECHARGE_TYPE} = "IN_APP" ];then
			EXPORT_METHOD_ANDROID=AutoExport.ExportDebugApkTw
		elif [ ${RECHARGE_TYPE} = "WEB" ];then
			EXPORT_METHOD_ANDROID=AutoExport.ExportWebRechargeDebugApkTw
		else
			EXPORT_METHOD_ANDROID=AutoExport.ExportDebugApkTw
		fi
    fi
    
    if [ ${BUILD_LANGUAGE} = "EN" ];then
    	EXPORT_METHOD_ANDROID=AutoExport.ExportDebugApkEn
    fi
    
    if [ ${BUILD_LANGUAGE} = "JP" ];then
    	EXPORT_METHOD_ANDROID=AutoExport.ExportDebugApkJp
    fi
    
	BUILD_NAME=debug
fi

if [ ${IS_RELEASE} = true ];then
    if [ ${BUILD_LANGUAGE} = "ZH" ];then
		if [ ${RECHARGE_TYPE} = "IN_APP" ];then
			EXPORT_METHOD_ANDROID=AutoExport.ExportReleaseApk
		elif [ ${RECHARGE_TYPE} = "WEB" ];then
			EXPORT_METHOD_ANDROID=AutoExport.ExportWebRechargeReleaseApk
		else
			EXPORT_METHOD_ANDROID=AutoExport.ExportReleaseApk
		fi
    fi

    if [ ${BUILD_LANGUAGE} = "TW" ];then
		if [ ${RECHARGE_TYPE} = "IN_APP" ];then
			EXPORT_METHOD_ANDROID=AutoExport.ExportReleaseApkTw
		elif [ ${RECHARGE_TYPE} = "WEB" ];then
			EXPORT_METHOD_ANDROID=AutoExport.ExportWebRechargeReleaseApkTw
		else
			EXPORT_METHOD_ANDROID=AutoExport.ExportReleaseApkTw
		fi
    fi
    
    if [ ${BUILD_LANGUAGE} = "EN" ];then
    	EXPORT_METHOD_ANDROID=AutoExport.ExportReleaseApkEn
    fi
    
    if [ ${BUILD_LANGUAGE} = "JP" ];then
    	EXPORT_METHOD_ANDROID=AutoExport.ExportReleaseApkJp
    fi
	BUILD_NAME=release
fi

echo start export...
$UNITY_PATH -batchmode -quit -nographics -projectPath ${DEV_PROJ_PATH} -buildTarget ${PLATFORM_ANDROID} -executeMethod ${EXPORT_METHOD_ANDROID} -logFile ${LOG_EXPORT_PATH}
echo end export

KillUnityProcess
Update_Folder=${DEV_ROOT_PATH}/Src/gyee-client-publish
if [ ${BUILD_LANGUAGE} = "EN" ];then
	Update_Folder=${DEV_ROOT_PATH}/Src/gyee_sea-client-publish
fi

if [ ${BUILD_LANGUAGE} = "JP" ];then
	Update_Folder=${DEV_ROOT_PATH}/Src/gyee_jp-client-publish
fi

RecoverResources ${LIB_ANDROID}

#编译apk
cd ${DEV_ROOT_PATH}
git checkout -f ${ASSERT_ANDROID_IGNORES}

#生成obb文件
if [ ${IS_EXPORT_OBB} = true ];then
    GenerateObb
fi

#git add . ${ASSET_ANDROID_PROJECT}
#git commit --allow-empty -m "Build android project(By Jenkins)" ${ASSET_ANDROID_PROJECT}
PublishApk ${BUILD_NAME}

#生成obb文件
if [ ${IS_EXPORT_OBB} = true ];then
    RecoverObbResources
fi

#提交分包资源
#if [ ${IS_SLIM_SPLIT} = true ];then	    
#	cd ${Update_Folder}
#    git add .
#    git commit -am "分包资源_${RES_VERSION}"
#    git pull --rebase origin master
#    echo 提交分包资源
#    git push origin master
#fi
# InitGit

标签: Jenkins git

发表评论:

朱文华 joyimp|@2011-2020 京ICP备16030765号