需求背景:
在實際項目時我們很少把源碼用個tar給到客戶,這樣顯得很不專業,且有的時候我們提供補丁,那么這個時候我們提供一個補丁的bin包可以直接安裝運行就顯得很高大上了。
物料準備
準備一臺liunx,虛擬機亦可,我當前使用的是Debian10
創建目錄
mkdir Demo_build_bin
cd Demo_build_bin
準備打包腳本
主要原理就是將我們的安裝腳本和源碼文件封裝一個可以執行的文件里面xxx.bin
運行腳本時先把源碼文件提取出來進行對應的操作
vim build_bin.sh
#!/bin/sh -e
DIR=$(dirname `readlink -f $0`)
version=$1
if [ ! $version ]; thenecho 'Please enter version info'exit -1
fi
git_version=`git log -n1 --pretty=%H | cut -c 1-11`
if [ ! $git_version ] ; thenecho 'Please enter git version'git_version=123456#exit -1
fi
if [ -e 'source_install.tar' ]; thenrm source_install.tar
fi
echo ${DIR}
pushd $DIR
if [ -e './source_install.tar' ];thenecho 'rm ./source_install.tar'rm ./source_install.tar
fi
tar -zcf source_install.tar ./source_installbuild_exe(){
architecture=`arch`
time=`date +"%Y%m%d"`
md5_source=$(md5sum source_install.tar | awk '{print $1}')
file_name=Demo_V${version}_${architecture}_Debian10_${time}_Git${git_version}.bin
sed -e "s/%%source_md5%%/$md5_source/" ./install.sh >$file_name
cat source_install.tar >> ${file_name}
chmod +x ${file_name}
echo 'Source md5 : ' $md5_source
echo 'New install bin file generated:' ${file_name}
}
if [ $? -ne 0 ];thenecho "Package fusionnos cli fail"
elsebuild_exepopd
fi
準備源碼目錄
mkdir source_install
cd source_install
echo 'hello world!!' > hello_world
將hello_world文件拷貝到opt目錄
編輯install.sh腳本,用于執行xxx.bin包
#!/bin/sh -e
outname=`pwd`
tail -n +25 $0 > $outname/source_install.tar
source_md5=%%source_md5%%
echo 'Source md5 : ' $source_md5
md5_source=`md5sum source_install.tar | awk '{ print $1 }'`
if [ $md5_source != $source_md5 ];thenechoecho ">>>ERROR: Unable to verify source archive checksum"echo ">>>Expected: $md5_source"echo ">>>Found : $source_md5"exit 1
elseecho "Md5 verify pass!"
fiecho 'Unzip fusionnos cli dependences'
tar -xvf source_install.tar >/dev/nul
cd ./source_install
cp ./hello_world /opt
cd ..
rm -rf source_install source_install.tar
echo 'Install complete!'
exit 0
打包
這里我們對生成的文件進行了md5驗證放置在文件的傳輸過程中或者安裝過程中損壞了
root@:/home/tiger/Study/Demo_build_bin# ./build_bin.sh 1.0
/home/tiger/Study/Demo_build_bin
/home/tiger/Study/Demo_build_bin /home/tiger/Study/Demo_build_bin
Source md5 : 829bd47bca2856081b27768c984230b0
New install bin file generated: Demo_V1.0_aarch64_Debian10_20231206_Git123456.bin
/home/tiger/Study/Demo_build_bin
執行查看效果
./Demo_V1.0_aarch64_Debian10_20231206_Git123456.bin
root@fusionnos:/home/tiger/Study/Demo_build_bin/test# ls /opt/hello_word
/opt/hello_word