-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_update.sh
58 lines (46 loc) · 1.8 KB
/
git_update.sh
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
#!/bin/sh
# ====( IMPORTS )==== #
# shellcheck disable=SC1091
. "$PIPELINE_REPO_PATH/utils.sh"
# =====( MAIN )===== #
run_command "cd $PIPELINE_REPO_PATH" "Failed to change to directory: $PIPELINE_REPO_PATH"
# Check that we received the branch name
if [ -n "$BRANCH_NAME" ]; then
log_message "$INFO_LVL" "Updating branch: $BRANCH_NAME"
else
log_message "$ERR_LVL" "No branch set. Exiting..."
exit "$EXIT_ERR"
fi
# Check that the PIPELINE_REPO_PATH exists
if [ ! -d "$PIPELINE_REPO_PATH" ]; then
log_message "$ERR_LVL" "The pipeline repo path does not exist! Path: $PIPELINE_REPO_PATH"
exit "$EXIT_ERR"
fi
run_command "cd $PIPELINE_REPO_PATH" "Failed to change to directory: $PIPELINE_REPO_PATH"
# Check if the directory is a git repository
if [ ! -d ".git" ]; then
log_message "$ERR_LVL" "The pipeline repo path is not a git repository. Searched at: $PIPELINE_REPO_PATH/.git"
exit "$EXIT_ERR"
fi
# Update the remote repository URL to include the TOKEN if present
if [ -n "$TOKEN" ]; then
REPO_URL_WITH_TOKEN="https://${TOKEN}@${REPO_URL#https://}"
git remote set-url origin "$REPO_URL_WITH_TOKEN"
log_message "$INFO_LVL" "Updated remote URL to use the provided token."
fi
# Checkout the branch
run_command "git checkout $BRANCH_NAME" "Failed to checkout branch: $BRANCH_NAME"
# Pull the latest changes for the branch
OUTPUT=$(git pull origin "$BRANCH_NAME" 2>&1)
STATUS=$?
# Check if git pull failed
if [ $STATUS -ne 0 ]; then
log_message "$ERR_LVL" "Git pull failed for branch: $BRANCH_NAME, remote: $(git config --get remote.origin.url)"
exit "$EXIT_ERR"
fi
if echo "$OUTPUT" | grep -q "Already up to date."; then
log_message "$INFO_LVL" "No updates were made to branch: $BRANCH_NAME."
else
log_message "$INFO_LVL" "Branch $BRANCH_NAME was updated."
fi
exit "$EXIT_SUCCESS"