Skip to content

Commit 515e772

Browse files
committed
Publish to GitHub when committing git tags
1 parent e5b4baa commit 515e772

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

.gitlab-ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# https://docs.gitlab.com/ce/ci/yaml/
22

3+
stages:
4+
- test
5+
- publish
6+
37
.build:
48
script:
59
- make info
@@ -54,3 +58,34 @@ bt:win-x64:
5458
expire_in: 1 day
5559
paths:
5660
- objectbox-generator-Windows.zip
61+
62+
publish:
63+
tags: [ gh-cli ]
64+
stage: publish
65+
dependencies:
66+
- bt:linux-x64
67+
- bt:mac
68+
- bt:win-x64
69+
only:
70+
variables:
71+
- $CI_COMMIT_TAG =~ /^[vV][0-9]+\.[0-9]+\..*/ # only built for version tags (e.g., v1.2.3)
72+
script:
73+
- ci/send-to-gchat.sh "$GOOGLE_CHAT_WEBHOOK_CI" --thread $CI_COMMIT_SHA "*Releasing ObjectBox Generator...* $CI_JOB_NAME from $CI_COMMIT_TAG ($CI_COMMIT_BRANCH $CI_COMMIT_SHORT_SHA)..."
74+
- mkdir artifacts
75+
- mv objectbox-generator-*.zip artifacts/
76+
- ls -alh artifacts
77+
- |
78+
if [[ ${CI_COMMIT_TAG:-} =~ ^[vV] ]]; then
79+
package_version="${CI_COMMIT_TAG:1}" # cut first 2 chars
80+
else
81+
echo "Inconsistent conditions and filter rules; please edit this file. Commit tag: ${CI_COMMIT_TAG:-}"
82+
exit 1
83+
fi
84+
echo "Package version: $package_version"
85+
ci/publish-to-github.sh ${OBX_GENERATOR_RELEASE_SCRIPT_ADDITIONAL_PARAM} "$package_version"
86+
- ci/send-to-gchat.sh "$GOOGLE_CHAT_WEBHOOK_CI" --thread $CI_COMMIT_SHA "*Released ObjectBox Generator* $CI_JOB_NAME from $CI_COMMIT_TAG ($CI_COMMIT_BRANCH $CI_COMMIT_SHORT_SHA)"
87+
after_script:
88+
- |
89+
if [[ "$CI_JOB_STATUS" == "failed" ]]; then
90+
ci/send-to-gchat.sh "$GOOGLE_CHAT_WEBHOOK_CI" --thread $CI_COMMIT_SHA "*Release failed for ObjectBox Generator* $CI_JOB_NAME from $CI_COMMIT_TAG ($CI_COMMIT_BRANCH $CI_COMMIT_SHORT_SHA)"
91+
fi

ci/publish-to-github.sh

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
function printUsage() {
5+
echo "usage: $(basename "$0") [options] tag"
6+
echo
7+
echo "Pushes ObjectBox Generator binaries to a GitHub release (draft)"
8+
echo " tag: the release version (e.g. 1.2.3); no 'v' prefix is required"
9+
echo
10+
echo "Options:"
11+
echo " --force-release-upload: upload to an existing release (non-draft) if it exists"
12+
echo " --linux-x64: upload only the Linux artifact (useful for testing)"
13+
echo " --dry-run: do not upload any artifacts"
14+
15+
exit 1
16+
}
17+
18+
# Default values
19+
force_release_upload=false
20+
linux_x64_only=false
21+
dry_run=false
22+
23+
# Parse command line arguments; e.g. override defaults
24+
while test $# -gt 0; do
25+
case "$1" in
26+
-h|help|--help|usage)
27+
printUsage
28+
;;
29+
--force-release-upload)
30+
force_release_upload=true
31+
shift
32+
;;
33+
--linux-x64) # partial artifact upload: this is useful to test the script locally
34+
linux_x64_only=true
35+
shift
36+
;;
37+
--dryrun|--dry-run)
38+
dry_run=true
39+
shift
40+
;;
41+
"") # ignore any empty parameters ("artifacts" that can happen depending on how the caller constructs the parameters)
42+
shift
43+
;;
44+
*)
45+
break
46+
;;
47+
esac
48+
done
49+
50+
if [[ "$#" -ne "1" ]]; then
51+
echo "$# arguments provided ('$*'), expected 1 "
52+
printUsage
53+
fi
54+
55+
echo "Options: linux_x64_only=$linux_x64_only, dry_run=$dry_run"
56+
57+
# start in the repo root directory
58+
cd "$(dirname "$0")/.."
59+
60+
github_tag=$1
61+
github_repo="https://github.com/objectbox/objectbox-generator"
62+
63+
if ! [[ $github_tag == v* ]]; then
64+
github_tag="v$github_tag"
65+
fi
66+
67+
echo "Checking GitHub authentication status..."
68+
gh auth status
69+
70+
echo "Checking if release ${github_tag} exists..."
71+
# https://cli.github.com/manual/gh_release_view
72+
if gh release view --repo "${github_repo}" "${github_tag}"; then
73+
echo "" # gh may have not ended with a new line
74+
is_draft_json=$(gh release view --repo "${github_repo}" "${github_tag}" --json isDraft)
75+
echo "Existing release \"${github_tag}\" found; draft status: $is_draft_json"
76+
if [[ $is_draft_json == *"\"isDraft\":true"* ]]; then
77+
echo "Existing release draft identified: will try to upload artifacts."
78+
echo "Note: existing artifact will not be overwritten; delete them manually beforehand if required."
79+
elif [[ $is_draft_json == *"\"isDraft\":false"* ]]; then
80+
echo "WARN: Existing release is not a draft"
81+
if [ "$force_release_upload" = true ]; then
82+
echo "WARN: Upload to existing release is forced; continuing..."
83+
else
84+
echo "WARN: Aborting upload to avoid messing up existing releases."
85+
echo "WARN: Situation must be resolved manually, or force the upload via argument."
86+
exit 1
87+
fi
88+
else
89+
echo "ERROR: Must this script be updated? Found unknown draft status: $is_draft_json"
90+
exit 1
91+
fi
92+
else
93+
echo "No release found for '${github_tag}'; creating a new release draft..."
94+
gh release create --draft --repo "${github_repo}" "${github_tag}" \
95+
--title "ObjectBox Generator ${github_tag}" \
96+
--notes "See CHANGELOG.md for details."
97+
echo "Uploading artifacts to new release draft..."
98+
fi
99+
100+
if [ "$dry_run" = true ]; then
101+
echo "Dry run: skipping artifact preparation & upload"
102+
exit 0
103+
fi
104+
105+
# Explicitly specify all files instead of using a wildcard glob to avoid unintended uploads (and catch missing files).
106+
# These artifacts are built by the build jobs in GitLab CI. See .gitlab-ci.yml for details on how to build.
107+
candidate_files=(
108+
objectbox-generator-Linux.zip
109+
objectbox-generator-macOS.zip
110+
objectbox-generator-Windows.zip
111+
)
112+
files_upload=()
113+
for f in "${candidate_files[@]}"; do
114+
if [ -f "${f}" ]; then
115+
files_upload+=("${f}")
116+
elif [ -f "artifacts/${f}" ]; then
117+
files_upload+=("artifacts/${f}")
118+
else
119+
echo "ERROR: File not found: ${f} (also not in artifacts/${f})"
120+
exit 1
121+
fi
122+
if [ "$linux_x64_only" = true ]; then break; fi # Linux is the first array item
123+
done
124+
125+
echo "${#files_upload[@]} files to upload:"
126+
for file_upload in "${files_upload[@]}"; do
127+
ls -lh "$file_upload"
128+
done
129+
130+
# https://cli.github.com/manual/gh_release_upload
131+
gh release upload --repo "${github_repo}" "${github_tag}" "${files_upload[@]}"
132+
133+
echo "Done."

ci/send-to-gchat.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
if [[ "$#" -lt "2" ]]; then
5+
echo "Please supply at least 2 parameters: gchat-webhook-url [--thread threadID] text"
6+
echo "Text formatting: https://developers.google.com/chat/reference/message-formats/basic"
7+
exit 1
8+
fi
9+
10+
gchat_url=$1
11+
shift
12+
13+
if [[ "$1" == "--thread" ]]; then
14+
if [[ "$#" -lt "3" ]]; then
15+
echo "Not enough parameters supplied"
16+
exit 1
17+
fi
18+
#https://developers.google.com/chat/reference/rest/v1/spaces.messages/create
19+
gchat_url="$gchat_url&threadKey=$2"
20+
shift 2
21+
fi
22+
23+
#https://developers.google.com/chat/reference/rest/v1/spaces.messages
24+
gchat_json="{\"text\": \"$*\"}"
25+
26+
curl -X POST -H 'Content-Type: application/json' "$gchat_url" -d "${gchat_json}"

0 commit comments

Comments
 (0)