-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtagGen
More file actions
executable file
·156 lines (116 loc) · 4.41 KB
/
tagGen
File metadata and controls
executable file
·156 lines (116 loc) · 4.41 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
usageHelp() {
echo "Usage: $0 --repository <repository> [--target-branch <branch>] [--source-dir <dir>] [--target-dir <dir>]"
echo " --repository: The GitHub repository in the format 'user/repo'."
echo " --target-branch: Optional. The branch to which changes should be pushed. Defaults to 'deployment'."
echo " --source-dir: Optional. The directory where files should be stored. Defaults to 'cd'."
echo " --target-dir: Optional. The directory or file paths that should be copied to the target-dir. Defaults to '../.temp/**'."
echo " example: ./ci/tagGen --repository \${{ github.repository }} --source-dir cd --target-dir .temp/**"
}
# Default values
TARGET_BRANCH="deployment"
SOURCE_DIR="cd/"
TARGET_DIR="../.temp/**"
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--repository) REPOSITORY="$2"; shift ;;
--target-branch) TARGET_BRANCH="$2"; shift ;;
--source-dir) SOURCE_DIR="$2"; shift ;;
--target-dir) TARGET_DIR="$2"; shift ;;
*) usageHelp; exit 1 ;;
esac
shift
done
# Check if the repository argument is provided
if [ -z "$REPOSITORY" ]; then
usageHelp
exit 1
fi
CURRENT_BRANCH=$(git branch --show-current)
CURRENT_PATH=$(pwd)
# Extract the project name from the REPOSITORY variable
PROJECT_NAME=$(echo "$REPOSITORY" | cut -d'/' -f2)
echo "[TAG-GENERATOR] initializing..."
echo " REPOSITORY=$REPOSITORY"
echo " TARGET_BRANCH=$TARGET_BRANCH"
echo " TARGET_DIR=$TARGET_DIR"
echo " SOURCE_DIR=$SOURCE_DIR"
git config --global user.name "[TAG-GENERATOR] - deployment"
git config --global user.email "eliasmeireles@gmail.com"
loadingLastCommitInfo() {
echo "Current branch: $(git branch --show-current)"
LAST_COMMIT_MSG=$(git log -1 --pretty=%B)
echo "Last commit message: $LAST_COMMIT_MSG"
LAST_COMMIT_HASH=$(git rev-parse --short HEAD)
}
git config set advice.skippedCherryPicks false || true
copyingFiles() {
echo "Copying files from $TARGET_DIR to $SOURCE_DIR"
mkdir -p ${PROJECT_NAME}/${SOURCE_DIR}/
cp -r $TARGET_DIR ${PROJECT_NAME}/${SOURCE_DIR}/
echo "Files in $TARGET_DIR"
ls -lah "$TARGET_DIR"
echo "Files in $SOURCE_DIR"
ls -lah ${PROJECT_NAME}/${SOURCE_DIR}
cd "$PROJECT_NAME" || exit 1
}
saveUntrackedFiles() {
git stash || true
}
echo "Loading last commit info"
loadingLastCommitInfo
echo "Cloning repository"
git clone "git@github.com:${REPOSITORY}.git"
TARGET_BRANCH="$TARGET_BRANCH/$CURRENT_BRANCH"
cd "$PROJECT_NAME" || exit 1
git fetch --all
createNewBranch() {
echo "Creating a new branch: $TARGET_BRANCH"
git checkout -b "$TARGET_BRANCH"
git branch --set-upstream-to=origin/"$TARGET_BRANCH" || true
echo "Branch $TARGET_BRANCH created"
}
# Fetch and switch to the target branch, or create it if it does not exist
git checkout "$TARGET_BRANCH" -f || createNewBranch
#echo "Update local branch with the latest changes from the remote"
# Update local branch with the latest changes from the remote
#git pull --rebase origin "$TARGET_BRANCH" || true
#echo "Rebasing current branch with origin/$CURRENT_BRANCH"
## Rebase with current branch to ensure it’s up-to-date
#git rebase origin/"$CURRENT_BRANCH" || {
# echo "Rebase conflict detected. Resolving conflicts by keeping local changes."
# # Resolve conflicts by keeping local changes
# git rebase --abort
# git rebase --strategy-option=ours origin/"$CURRENT_BRANCH"
#}
cd ..
echo "Saving untracked files"
copyingFiles
git diff || true
# Create a commit message with user info and link to commit changes
COMMIT_MESSAGE="[${CURRENT_BRANCH}-${LAST_COMMIT_HASH}] Application Deployment
${LAST_COMMIT_MSG}
"
echo "$COMMIT_MESSAGE"
# Add, commit, and push the changes
git add ./"${SOURCE_DIR}"
git commit -m "$COMMIT_MESSAGE" || git commit --allow-empty -m "$COMMIT_MESSAGE"
# Push changes, handle non-fast-forward issues
#git push origin "$TARGET_BRANCH" || (git pull --rebase origin "$TARGET_BRANCH" && git push origin "$TARGET_BRANCH")
git push origin "$TARGET_BRANCH" --force
TAG="${CURRENT_BRANCH}-${LAST_COMMIT_HASH}"
TAG_MESSAGE="${CURRENT_BRANCH}-${LAST_COMMIT_HASH}
- Tag generated by [TAG-GENERATOR] action
${LAST_COMMIT_MSG}
"
echo "$TAG_MESSAGE"
echo "Creating a new tag: $TAG"
git tag -a "$TAG" -m "$TAG_MESSAGE"
git push origin "$TAG"
# Output the tagGen
echo "TAG=$TAG" >> "$GITHUB_ENV"
# Log the generated tagGen name
echo "Generated tag name: $TAG"