近期專案進入測試階段,經常需要包版與加密壓縮交付給客戶,每次手動都要花費不少時間(又怕出錯)。為了可以準時下班,就想說來寫一個 shell script 來做這段重覆又花時間的事情。咻一下就可以把檔案準備好了,快又爽!
當專案開發進入測試階段之後,每天都會上演的戲碼就是包版做程式的更新。為了每天可以準時下班,就必需讓包版自動化。一方面可以省時,也可以減少犯錯的可能。接下來,我使用 React 專案來做範例,此專案已設定好不同環境(uat、prod)的 scripts 透過 npm、webpack 打包程式,說明在 Mac OS 如何使用 shell script 來進行打包、壓縮加密、更新版本,產出應交付的檔案。
使用文字編輯器,增加以下內容,定義所要使用的 shell。
#!/bin/bash
PROJECT_PATH='/Users/demo/Projects/react-app'
TARGET_PATH='/Users/demo/react-app'
BUILD_OUTPUT_FOLDER=$(date +"%Y%m%d%H%M")
BUILD_OUTPUT_PATH=$TARGET_PATH/$BUILD_OUTPUT_FOLDER
ZIP_PASSWORD='test123'
read -p '請輸入版本號:' version
cd $PROJECT_PATH
sed -i "" 's/"version": "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*",/"version": "'"$version"'",/g' package.json
if [ ! -d $BUILD_OUTPUT_PATH/xxx\ v$version\ uat ]
then
mkdir -p $BUILD_OUTPUT_PATH/xxx\ v$version\ uat
fi
npm run build-uat
cp -r $PROJECT_PATH/build/* $BUILD_OUTPUT_PATH/xxx\ v$version\ uat/
cd $BUILD_OUTPUT_PATH
expect -c "
spawn zip -er ./xxx\ v$version\ uat.zip ./xxx\ v$version\ uat/
expect \"Enter password\"
send \"$ZIP_PASSWORD\r\"
expect \"Verify password\"
send \"$ZIP_PASSWORD\r\"
expect eof
"
cd $PROJECT_PATH
ENV='uat prod'
for value in $ENV; do
if [ ! -d $BUILD_OUTPUT_PATH/xxx\ v$version\ $value ]
then
mkdir -p $BUILD_OUTPUT_PATH/xxx\ v$version\ $value
fi
npm run build-$value
cp -r $PROJECT_PATH/build/* $BUILD_OUTPUT_PATH/xxx\ v$version\ $value/
cd $BUILD_OUTPUT_PATH
expect -c "
spawn zip -er ./xxx\ v$version\ $value.zip ./xxx\ v$version\ $value/
expect \"Enter password\"
send \"$ZIP_PASSWORD\r\"
expect \"Verify password\"
send \"$ZIP_PASSWORD\r\"
expect eof
"
cd $PROJECT_PATH
done
read -p "確定要更新版控的版本號碼至 $version (y/N)?" isUpdateGitVersion
isUpdateGitVersion=${isUpdateGitVersion:-N}
if [ $isUpdateGitVersion = 'Y' ] || [ $isUpdateGitVersion = 'y' ]; then
echo "Updating git..."
git commit -a -m "release v$version"
git push
echo "The version control is updated to $version."
else
echo "The version control is not updated."
fi
增加一些資訊輸出,幫助了解目前 shell script 執行的狀況
#!/bin/bash
PROJECT_PATH='/Users/demo/Projects/react-app'
TARGET_PATH='/Users/demo/react-app'
BUILD_OUTPUT_FOLDER=$(date +"%Y%m%d%H%M")
BUILD_OUTPUT_PATH=$TARGET_PATH/$BUILD_OUTPUT_FOLDER
ZIP_PASSWORD='test123'
read -p '請輸入版本號:' version
echo "\n### 準備打包版號為:$version ###\n"
echo "專案路徑:$PROJECT_PATH"
echo "檔案輸出路徑:$BUILD_OUTPUT_PATH"
echo "壓縮密碼:$ZIP_PASSWORD"
cd $PROJECT_PATH
sed -i "" 's/"version": "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*",/"version": "'"$version"'",/g' package.json
echo "Project version is updated to $version"
ENV='uat prod'
for value in $ENV; do
if [ ! -d $BUILD_OUTPUT_PATH/xxx\ v$version\ $value ]
then
echo "\nCreating [$BUILD_OUTPUT_PATH/xxx v$version $value] folder"
mkdir -p $BUILD_OUTPUT_PATH/xxx\ v$version\ $value
echo "\033[0;37;43m[$BUILD_OUTPUT_PATH/xxx v$version $value] folder created success!\033[0m"
fi
npm run build-$value
echo "\033[0;37;43m$value build success!\033[0m\n"
echo "Copying file..."
echo "From => $PROJECT_PATH/build/*"
echo "To => $BUILD_OUTPUT_PATH/xxx v$version $value/"
cp -r $PROJECT_PATH/build/* $BUILD_OUTPUT_PATH/xxx\ v$version\ $value/
echo "\033[0;37;43mCopy file to [$BUILD_OUTPUT_PATH/xxx v$version $value/] success!\033[0m\n"
cd $BUILD_OUTPUT_PATH
expect -c "
spawn zip -er ./xxx\ v$version\ $value.zip ./xxx\ v$version\ $value/
expect \"Enter password\"
send \"$ZIP_PASSWORD\r\"
expect \"Verify password\"
send \"$ZIP_PASSWORD\r\"
expect eof
"
cd $PROJECT_PATH
done
echo "\n\033[0;37;43mThe package is successful.\033[0m\n"
echo "Output => $BUILD_OUTPUT_PATH\n"
read -p "確定要更新版控的版本號碼至 $version (y/N)?" isUpdateGitVersion
isUpdateGitVersion=${isUpdateGitVersion:-N}
if [ $isUpdateGitVersion = 'Y' ] || [ $isUpdateGitVersion = 'y' ]; then
echo "Updating git..."
git commit -a -m "release v$version"
git push
echo "The version control is updated to $version."
else
echo "The version control is not updated."
fi
~ END ~