参数部分没有
不要照抄,只供参考
需求:
CI利用confd+etcd生成配置文件
CI把git的COMMIT 传到openshift的buildconfigs
#!/bin/bash echo echo echo "第一阶段:生成配置文件" echo echo Output_Path=/data/html/nightly/$ENV_NAME/$Project_Name echo "项目名【$Project_Name】" echo "发布的环境【$ENV_NAME】" echo "发布的git分支【$GIT_BRANCH】 " echo "发布的GIT COMMIT【$GIT_COMMIT】" /data/root/.jenkins/downtown_scripts/oc_confd_get_config_.sh $ENV_NAME $Project_Name $WORKSPACE $Output_Path echo "+++++++++配置文件生成完毕++++++++++" echo echo echo "第二阶段:开始更新build配置" echo echo echo "正在更新buildconfigs设置>>>>>>>>" Output_Path_File=/data/html/nightly/$ENV_NAME/$Project_Name/oc_status/${Project_Name}.json mkdir -p /data/html/nightly/$ENV_NAME/$Project_Name/oc_status NAMESPACE=$ENV_NAME NAME=$Project_Name #获取目前的状态 curl -k -H "Authorization: Bearer $TOKEN" -H 'Accept: application/json' $ENDPOINT/oapi/v1/namespaces/$NAMESPACE/buildconfigs/$NAME 2>/dev/null > $Output_Path_File #替换GIT COMMIT REF_Value=`cat $Output_Path_File |jq -r '.spec | .source | .git | .ref'` sed -i s/"$REF_Value"/"$GIT_COMMIT"/g $Output_Path_File Build_Configs_Status=`cat $Output_Path_File` #更新到oc api curl -k -X PUT -d @- -H "Authorization: Bearer $TOKEN" -H 'Accept: application/json' -H 'Content-Type: application/json' $ENDPOINT/oapi/v1/namespaces/$NAMESPACE/buildconfigs/$NAME >/dev/null 2>&1 << EOF $Build_Configs_Status EOF echo echo echo "+++++++更新后的buildconfigs配置+++++++++" curl -k -H "Authorization: Bearer $TOKEN" -H 'Accept: application/json' $ENDPOINT/oapi/v1/namespaces/$NAMESPACE/buildconfigs/$NAME 2>/dev/null echo "+++++++更新后的buildconfigs配置+++++++++" echo echo
附上/data/root/.jenkins/downtown_scripts/oc_confd_get_config_.sh
#!/bin/bash ENV_NAME=$1 Project_Name=$2 WORKSPACE=$3 Output_Path=$4 Config_Templates_Path=$Output_Path/config-templates Confd_Path=$Output_Path/confd echo "配置文件生成路径【$Output_Path】" echo "jenkins工作空间路径【$WORKSPACE】" #拷贝配置文件 rm -rf $Output_Path mkdir -p $Output_Path cd $WORKSPACE cp -Rf * $Output_Path ls $Output_Path #先结束掉该项目的confd进程 ps -ef | grep $Confd_Path | grep -v grep | awk '{ print "kill -9 " $2}' | bash echo "===============================" echo "[confd] for $ENV_NAME-$Project_Name killed" echo "===============================" #清理confd项目目录 rm -rf $Confd_Path mkdir -p $Confd_Path/conf.d mkdir -p $Confd_Path/templates ls $Confd_Path ##复制配置文件到指定目录 find $Config_Templates_Path -type f -name "*.*" -exec cp -fr {} $Confd_Path/templates ; echo ========templates copied===================== ls $Config_Templates_Path/*/* echo ========templates copied===================== ##生成模式化文件format_standard.txt ##格式为:文件名 文件路径 ##清空format_standard.txt文件 echo "" > $Confd_Path/format_standard.txt ##提取Config_Templates_Path目录内的子目录作为变量,并提出所有文件名 Config_Templates_L2_List=`cd $Config_Templates_Path ; find $PWD | xargs ls -dl | grep -v "^d" | awk 'BEGIN{FS="/"} {print $(NF-1)"/"$NF}'` #i代表的是$Config_Templates_L2_List输出出来的所有文件的相对路径 echo ========config file need to be generate ===================== echo "${Config_Templates_L2_List}" echo ========config file need to be generate ===================== cd $Confd_Path for i in $Config_Templates_L2_List do if [ -f $Output_Path/$i ];then find $Output_Path/$i -type f -name "*.*" |awk 'BEGIN{FS="/"} {printf("%30s %s ",$NF,$0)}' >> $Confd_Path/format_standard.txt fi done for i in $Config_Templates_L2_List do if [ -f $Output_Path/config/$i ];then find $Output_Path/config/$i -type f -name "*.*" |awk 'BEGIN{FS="/"} {printf("%30s %s ",$NF,$0)}' >> $Confd_Path/format_standard.txt fi done echo "################# format_standard.txt############" cat $Confd_Path/format_standard.txt echo "################# format_standard.txt############" ##遍历格式化文件并生成模板文件 cd $Confd_Path cat format_standard.txt |awk 'NR>1' |while read F_CONFIG_NAME F_CONFIG_PATH do ##生成模板文件 cat << EOF > conf.d/$F_CONFIG_NAME.toml [template] #模板文件,基于它进行修改 src = "$F_CONFIG_NAME" #目标文件地址 dest = "$F_CONFIG_PATH" #keys是在etcd上订阅消息的前缀 keys = [ "", ] #更新配置后的命令 changed_log_cmd = "echo $ENV_NAME-$Project_Name configuration changed `date` >> /tmp/confd_changed.log" EOF done #显示该项目的confd进程数 ps -ef | grep $Confd_Path | grep -v grep | wc -l export ETCDCTL_API=2 nohup /data/app/confd/bin/confd -config-file /data/app/confd/$ENV_NAME/confd.config -confdir $Confd_Path/ -username root -password Downtown2013 -basic-auth true > $Confd_Path/confd.log 2>&1 & sleep 5 #显示该项目的confd日志 echo "====confd_logs last 100 lines=================" tail -n 100 $Confd_Path/confd.log echo "====confd_logs last 100 lines=================" ##confd