Skip to content

Instantly share code, notes, and snippets.

@tigi44
Created May 25, 2020 08:00
Show Gist options
  • Save tigi44/125efed73ac0de0c4f972497ab2f1787 to your computer and use it in GitHub Desktop.
Save tigi44/125efed73ac0de0c4f972497ab2f1787 to your computer and use it in GitHub Desktop.
remote build iOS Xcode Project(+ project version up) on Jenkins Server using shell script
#!/bin/bash
PROJECT_PATH='PROJECT_PATH'
XCODE_PROJECT_FILE_PATH='XCODE_PROJECT_FILE_PATH.xcodeproj/XCODE_PROJECT_FILE_PATH.pbxproj'
JENKINS_SERVER='JENKINS_SERVER'
function string_replace {
string=$1
old_string=$(echo $2 | sed 's#/#\\/#g')
new_string=$(echo $3 | sed 's#/#\\/#g')
result=$(echo $string | sed "s/$old_string/$new_string/g")
echo "$result"
}
function git_checkout_pull
{
branch=$1
echo ""
echo "#### START git checkout pull : $branch"
cd $PROJECT_PATH
git stash
git remote update
git fetch
git pull
git checkout $branch
success_checkout=$?
if [[ $success_checkout -ne 0 ]];
then
echo "#### ERROR git checkout pull : $branch"
echo ""
return
fi
git pull
echo "#### END git checkout pull : $branch"
echo ""
}
function git_ios_project_version_up
{
branch=$1
version=$2
build_number=$3
echo ""
echo "#### START git project version up : $branch"
echo "APP_VERSION : " $version
sed -i "" "s/APP_VERSION = [0-9\.]*;/APP_VERSION = $version;/g" $XCODE_PROJECT_FILE_PATH
echo "BUILD_NUMBER : " $build_number
sed -i "" "s/BUILD_NUMBER = [0-9]*;/BUILD_NUMBER = $build_number;/g" $XCODE_PROJECT_FILE_PATH
echo "#### END git project version up : $branch"
echo ""
}
function git_push_commit_version_up_comment
{
branch=$1
version=$2
build_number=$3
echo ""
echo "#### START git push commit : $branch"
git add $XCODE_PROJECT_FILE_PATH
git commit -a -m "<update version> $version $build_number"
git push origin $branch
echo "#### END git push commit : $branch"
echo ""
}
function git_commit_log
{
days=$1
echo ""
echo "#### START git commit log days : $days"
git log --pretty="- %s" --since=$days"days ago" --grep="<update version>" --grep="]"
echo "#### END git commit log days: $days"
echo ""
}
function git_log
{
branch=$1
days=$2
git_checkout_pull $branch
git_commit_log $days
}
function git_version_up
{
branch=$1
version=$2
build_number=$3
git_checkout_pull $branch
if [ -z $version ];then
version=$(egrep -o 'APP_VERSION = [0-9\.]*;' $XCODE_PROJECT_FILE_PATH | sort -u | sed -e "s/APP_VERSION = //g" | sed -e "s/;//g")
version=$(echo $version | awk '{ split($0, arr, "."); arr[4]+=1; print arr[1]"."arr[2]"."arr[3]"."arr[4];}')
fi
if [ -z $build_number ];then
build_number=$(egrep -o 'BUILD_NUMBER = [0-9\.]*;' $XCODE_PROJECT_FILE_PATH | sort -u | sed -e "s/BUILD_NUMBER = //g" | sed -e "s/;//g")
(( build_number+=1 ))
fi
git_ios_project_version_up $branch $version $build_number
git_push_commit_version_up_comment $branch $version $build_number
git_log $branch 4
}
function jenkins_build_excute
{
job_name=$1
job_url=$2
echo ""
echo "#### START Jenkins Build Excute : $job_name"
curl -s $job_url/build
echo "#### END Jenkins Build Excute : $job_name"
echo ""
}
function jenkins_build_edit_config_xml
{
job_name=$1
job_url=$2
branch=$3
version=$4
config_xml_url=$job_url/config.xml
mkdir -p xml/new
echo ""
echo "#### START Jenkins Build Edit .XML : $job_name"
# download .xml
curl -s $config_xml_url > xml/$job_name.xml
# edit branch name and ipa file name on .xml
xml_tag_name='name'
current_branch=$(grep "<$xml_tag_name>.*<.$xml_tag_name>" xml/$job_name.xml | sed -e "s/^.*<$xml_tag_name/<$xml_tag_name/" | cut -f2 -d">"| cut -f1 -d"<")
new_branch=$branch
xml_tag_version='artifactVersion'
version_seperator='_v'
current_filename=$(grep "<$xml_tag_version>.*<.$xml_tag_version>" xml/$job_name.xml | sed -e "s/^.*<$xml_tag_version/<$xml_tag_version/" | cut -f2 -d">"| cut -f1 -d"<")
current_filename_array=(${current_filename//_v/ })
# replace a "/" path of a branch to "\\/"
if [[ "$current_branch" == *"/"* ]];then
current_branch=$(echo $current_branch | awk '{ split($0,arr,"/"); printf("%s\\/%s\n",arr[1],arr[2]); }')
fi
if [[ "$branch" == *"/"* ]];then
new_branch=$(echo $branch | awk '{ split($0,arr,"/"); printf("%s\\/%s\n",arr[1],arr[2]); }')
fi
# replace .xml data
if [ $version ]
then
sed -e "s/<$xml_tag_name>$current_branch<\/$xml_tag_name>/<$xml_tag_name>$new_branch<\/$xml_tag_name>/g" -e "s/<$xml_tag_version>$current_filename<\/$xml_tag_version>/<$xml_tag_version>${current_filename_array[0]}$version_seperator$version<\/$xml_tag_version>/g" xml/$job_name.xml > xml/new/$job_name.xml
else
sed -e "s/<$xml_tag_name>$current_branch<\/$xml_tag_name>/<$xml_tag_name>$new_branch<\/$xml_tag_name>/g" -e "s/<$xml_tag_version>$current_filename<\/$xml_tag_version>/<$xml_tag_version>${current_filename_array[0]}<\/$xml_tag_version>/g" xml/$job_name.xml > xml/new/$job_name.xml
fi
# upload .xml
curl -X POST -H 'Content-Type: application/xml; charset=utf-8' $config_xml_url --data-binary @xml/new/$job_name.xml
echo "#### END Jenkins Build Edit .XML : $job_name"
echo ""
}
function jenkins_job_name_from_job_url
{
job_url=$1
job_name=$(string_replace $job_url $JENKINS_SERVER '')
job_name=$(string_replace $job_name 'job' '')
job_name=$(string_replace $job_name '/' '')
echo "$job_name"
}
function jenkins_build
{
jenkins_view=$1
branch=$2
version=$3
echo ""
echo "#### START Jenkins Build jenkinsView=$jenkins_view, branch=$branch, version=$version"
urls=($(curl -s $JENKINS_SERVER/view/$jenkins_view/api/json | python -m json.tool | awk '/url/ && /job/ {print $2}' | sed 's/\"//g'))
for job_url in "${urls[@]}"; do
job_name=$(jenkins_job_name_from_job_url $job_url)
if [ $branch ]
then
jenkins_build_edit_config_xml $job_name $job_url $branch $version
fi
jenkins_build_excute $job_name $job_url
done
echo "#### END Jenkins Build jenkinsView=$jenkins_view, branch=$branch, version=$version"
echo ""
}
#### main ####
function help_message()
{
echo ""
if [ $# -gt 0 ];then
echo "Unknown option : $1"
fi
echo "usage : ./jenkins_build.sh [-v] [-b] [-l] [-s]"
echo " - version_up : ./jenkins_build.sh -v <BRANCH> [<APP_VERSION> <BUILD_NUMBER>]"
echo " - build_jenkins : ./jenkins_build.sh -b <JENKINS_VIEW> [<BRANCH>] [<APP_VERSION>]"
echo " - git_log : ./jenkins_build.sh -l <BRANCH> <SINCE_DAYS>"
echo ""
}
if [ $# -eq 0 ]
then
help_message
exit
fi
# OPTION=${1:?"option: $0 [OPTIONS : -v:version_up, -b:build_jenkins, -s:build_jenkins_server, -l:log]"}
OPTION=$1
shift
case $OPTION in
-h | -help)
help_message
;;
-v)
if [ $# -lt 1 ] || [ $# -gt 3 ]
then
help_message
else
git_version_up $1 $2 $3
fi
;;
-b)
if [ $# -lt 1 ] || [ $# -gt 3 ]
then
help_message
else
jenkins_build $1 $2 $3
fi
;;
-l)
if [ $# -ne 2 ]
then
help_message
else
git_log $1 $2
fi
;;
*)
help_message $OPTION
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment