Repository navigation
Expand file tree
/
Copy pathgitlab.bit.tag-export
More file actions
102 lines (82 loc) · 2.7 KB
/
Copy pathgitlab.bit.tag-export
File metadata and controls
102 lines (82 loc) · 2.7 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
#!/bin/bash
# Check if $BIT_CONFIG_ACCESS_TOKEN is set
if [[ -z "$BIT_CONFIG_USER_TOKEN" && -z "$BIT_CONFIG_ACCESS_TOKEN" ]]; then
echo "Error: BIT_CONFIG_ACCESS_TOKEN environment variable is not set. It is required!"
exit 1
fi
# Assign BIT_CONFIG_ACCESS_TOKEN to BIT_CONFIG_USER_TOKEN
if [[ -z "$BIT_CONFIG_USER_TOKEN" ]]; then
BIT_CONFIG_USER_TOKEN="$BIT_CONFIG_ACCESS_TOKEN"
export BIT_CONFIG_USER_TOKEN
fi
# Load variables from ws.env file
source /tmp/ws.env
# Setting git configuration
git config --global --add safe.directory "${CI_PROJECT_DIR}"
# Check environment variable for GitLab token
if [[ -z "$GITLAB_TOKEN" ]]; then
echo "Error: GITLAB_TOKEN environment variable is required."
exit 1
fi
# Verify if CI_PROJECT_ID is set
if [[ -z "$CI_PROJECT_ID" ]]; then
echo "Error: Run this task in GitLab."
exit 1
fi
# Retrieve the MR ID from the latest commit
commit_sha=$(git rev-parse HEAD)
response=$(curl --silent --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/repository/commits/$commit_sha/merge_requests")
# Check the API call for MR retrieval
if [[ $? -ne 0 ]]; then
echo "Error: Failed to fetch data from GitLab API for commit: $commit_sha."
exit 1
fi
if echo "$response" | jq '. | type' | grep -q "array" && [[ "$(echo "$response" | jq 'length')" -gt 0 ]]; then
MR_ID=$(echo "$response" | jq '.[0].iid')
else
MR_ID=""
fi
# Extract version information from commit or MR
keywords=("patch" "major" "minor" "pre-release")
version="Unknown"
if [[ -n "$MR_ID" ]]; then
MR_DETAILS=$(curl --silent --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/merge_requests/$MR_ID")
MR_TITLE=$(echo "$MR_DETAILS" | jq -r '.title')
MR_LABELS=$(echo "$MR_DETAILS" | jq -r '.labels[]')
for label in $MR_LABELS; do
if [[ " ${keywords[*]} " == *"$label"* ]]; then
version="$label"
break
fi
done
if [[ "$version" == "Unknown" ]]; then
for keyword in "${keywords[@]}"; do
if [[ "$MR_TITLE" == *"[${keyword}]"* ]]; then
version="$keyword"
break
fi
done
fi
fi
if [[ "$version" == "Unknown" ]]; then
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
for keyword in "${keywords[@]}"; do
if [[ "$COMMIT_MESSAGE" == *"[${keyword}]"* ]]; then
version="$keyword"
break
fi
done
fi
# Display the determined version
echo "Determined Version: $version"
command="bit tag -m 'CI' --build"
if [[ "$version" != "Unknown" ]]; then
command+=" --$version"
fi
if [[ " $@ " == *"--persist"* ]]; then
command+=" --persist"
fi
# Run the Bit command
echo "Executing: $command"
$command
bit export