-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path.gitlab-ci.yml
executable file
·140 lines (122 loc) · 3.67 KB
/
.gitlab-ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
variables:
PCF_DEPLOY_REPO: "http://arthur:[email protected]/arthur/hourglass.git"
GITEE_REPO: "https://arthur19q3:[email protected]/unilink_1/UniLinkExecution.git"
PCF_DEPLOY_BRANCH: master
PCF_COMMIT_USER_NAME: arthur
PCF_COMMIT_USER_EMAIL: [email protected]
GIT_STRATEGY: none
stages:
- mergeCode
mergeCode:
stage: mergeCode
script:
- echo "Starting mergeCode stage"
- eval $(ssh-agent -s)
- ssh-add /home/gitlab-runner/.ssh/id_rsa
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
##
## Check if inside a git vault, if not, initialize one
##
- |
if [ ! -d .git ]; then
git init
git remote add origin "$PCF_DEPLOY_REPO"
git remote add gitee "$GITEE_REPO"
git commit --allow-empty -m "Initial commit"
git branch -M $PCF_DEPLOY_BRANCH
fi
##
## Configure git
##
- git config --global user.email "$PCF_COMMIT_USER_EMAIL"
- git config --global user.name "$PCF_COMMIT_USER_NAME"
- git config pull.rebase false
# ##
# ## Check if inside a git vault, if not, initialize one
# ##
# - |
# if [ ! -d .git ]; then
# git init
# # Create an initial commit to allow pull and push
# git commit --allow-empty -m "Initial commit"
# git branch -M $PCF_DEPLOY_BRANCH
# fi
##
## Remove existing remotes and add new ones
##
- |
if git remote | grep -q '^origin$'; then
git remote remove origin
fi
if git remote | grep -q '^gitee$'; then
git remote remove gitee
fi
- git remote add origin "$PCF_DEPLOY_REPO"
- git remote add gitee "$GITEE_REPO"
##
## Debugging: Print the remotes again to ensure they are set correctly
##
- git remote -v
- rm -fr ".git/rebase-merge"
##
## Clean up any existing conflicts
##
- |
if [ -n "$(git ls-files -u)" ]; then
echo "Cleaning up existing conflicts..."
git reset --hard HEAD
git clean -fd
fi
##
## Fetch and checkout the branch
##
- git fetch origin
- git fetch gitee
- git checkout -B $PCF_DEPLOY_BRANCH origin/$PCF_DEPLOY_BRANCH
##
## Pull from origin and handle conflicts
##
- git pull origin $PCF_DEPLOY_BRANCH || true
- |
if [ -n "$(git ls-files -u)" ]; then
git ls-files -u | cut -f 2 | sort -u | while read file; do
if [ ! -f "$file" ]; then
git rm "$file"
else
git checkout --ours "$file"
git add "$file"
fi
done
git commit -m "Resolved merge conflicts by choosing local changes from origin"
fi
##
## Pull from gitee and handle conflicts, always preferring origin changes
##
- git pull gitee $PCF_DEPLOY_BRANCH || true
- |
if [ -n "$(git ls-files -u)" ]; then
git ls-files -u | cut -f 2 | sort -u | while read file; do
if [ "$(git status --porcelain | grep "$file" | grep -o 'D')" = "D" ]; then
git rm "$file"
else
git checkout --ours "$file"
git add "$file"
fi
done
git commit -m "Resolved merge conflicts by choosing origin changes over gitee"
fi
##
## Check if there are changes to push to origin
##
- |
if [ -z "$(git status --porcelain)" ]; then
echo "No changes to push to origin"
else
echo "Pushing changes to origin"
git push origin $PCF_DEPLOY_BRANCH
fi
## Force push changes to gitee
##
- git push -f gitee $PCF_DEPLOY_BRANCH
tags:
- sync_task