@@ -20,7 +20,6 @@ configure_and_check() {
2020 GIT_REPO=${GIT_REPO:- git.example.com/ user/ repo.git}
2121 GIT_CLONE_METHOD=${GIT_CLONE_METHOD:- https:// }
2222 GIT_LOCAL_PATH=$( mktemp -d)
23- GIT_BRANCH=${GIT_BRANCH:- main}
2423
2524 GIT_REPO_URL=${GIT_CLONE_METHOD}${GIT_USER_NAME} :${GIT_USER_PASSWORD} @${GIT_REPO}
2625
@@ -33,11 +32,13 @@ generate_git_config() {
3332 git config --global user.email " ${GIT_USER_MAIL} "
3433 # to store credentials in memory for a short period of time
3534 git config --global credential.helper cache
35+ # set 'autoSetupRemote' in order to automatically set an upstream tracking branch
36+ git config --global push.autoSetupRemote true
3637}
3738
3839clone_git_repository () {
3940 log_subsection " Clone git repository"
40- git clone -b ${GIT_BRANCH} ${GIT_REPO_URL} ${GIT_LOCAL_PATH}
41+ git clone ${GIT_REPO_URL} ${GIT_LOCAL_PATH}
4142}
4243
4344compare_files () {
@@ -58,15 +59,24 @@ update_git_repository() {
5859 log_subsection " Commit and push changes"
5960 local src_file=$1
6061 local dst_file=$2
62+ local git_ci_branch=" ci-release-${CI_PIPELINE_ID} "
6163 local date_now=$( date +" %Y-%m-%d %H:%M:%S" )
6264 local commit_message=" Automatic update by pipeline ${CI_PIPELINE_ID} ${date_now} "
6365
6466 cp -v ${src_file} ${dst_file}
6567
66- git -C ${GIT_LOCAL_PATH} add ${dst_file}
67- git -C ${GIT_LOCAL_PATH} commit -am " ${commit_message} "
68- # will use credential helper, no need to specify again credentials
69- git -C ${GIT_LOCAL_PATH} push
68+ # Check the branch exists ot not.
69+ if git -C ${GIT_LOCAL_PATH} ls-remote --exit-code --heads origin " $git_ci_branch " > /dev/null 2>&1 ; then
70+ # Skip commit
71+ echo " Skip commit, branch '$git_ci_branch ' already exists."
72+ else
73+ git -C ${GIT_LOCAL_PATH} checkout -b " $git_ci_branch "
74+
75+ git -C ${GIT_LOCAL_PATH} add ${dst_file}
76+ git -C ${GIT_LOCAL_PATH} commit -am " ${commit_message} "
77+ # will use credential helper, no need to specify again credentials
78+ git -C ${GIT_LOCAL_PATH} push
79+ fi
7080}
7181
7282log_section " Configure and check"
0 commit comments