Skip to content

Commit 760c33c

Browse files
committed
[ci] Rework build script and release
* Uses now own github-releases-api.sh to create the release on github. * For releases, the project is built only once now * Only at the end, the github release is published
1 parent 9e3d725 commit 760c33c

File tree

5 files changed

+284
-87
lines changed

5 files changed

+284
-87
lines changed

.travis.yml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,6 @@ before_script:
1919

2020
script: "source .travis/build.sh"
2121

22-
23-
before_deploy: true
24-
deploy:
25-
provider: releases
26-
api_key: ${GITHUB_OAUTH_TOKEN}
27-
file_glob: true
28-
file:
29-
- net.sourceforge.pmd.eclipse.p2updatesite/target/net.sourceforge.pmd.eclipse.p2updatesite-*.zip
30-
skip_cleanup: true
31-
on:
32-
tags: true
33-
repo: pmd/pmd-eclipse-plugin
34-
condition: "${TRAVIS_SECURE_ENV_VARS} = true"
35-
after_deploy: source .travis/release.sh
36-
37-
38-
3922
notifications:
4023
email:
4124
recipients:

.travis/build.sh

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/bin/bash
22
set -e
33

4+
source .travis/logger.sh
5+
source .travis/github-releases-api.sh
6+
7+
VERSION=$(./mvnw -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.5.0:exec | tail -1)
8+
49
echo "Performing build steps"
510
echo "TRAVIS_REPO_SLUG: $TRAVIS_REPO_SLUG"
611
echo "TRAVIS_PULL_REQUEST_SLUG: $TRAVIS_PULL_REQUEST_SLUG"
@@ -11,16 +16,63 @@ echo "TRAVIS_BRANCH: $TRAVIS_BRANCH"
1116
echo "TRAVIS_TAG: $TRAVIS_TAG"
1217
echo "TRAVIS_ALLOW_FAILURE: $TRAVIS_ALLOW_FAILURE"
1318

19+
if [ "${TRAVIS_REPO_SLUG}" != "pmd/pmd-eclipse-plugin" ] || [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then
20+
21+
log_info "This is a pull-request build"
22+
./mvnw verify
23+
24+
elif [ "${TRAVIS_REPO_SLUG}" = "pmd/pmd-eclipse-plugin" ] && [ "${TRAVIS_PULL_REQUEST}" = "false" ] && [ "${TRAVIS_SECURE_ENV_VARS}" = "true" ]; then
25+
26+
if [[ "${VERSION}" != *-SNAPSHOT && "${TRAVIS_TAG}" != "" ]]; then
27+
log_info "This is a release build for tag ${TRAVIS_TAG} (version: ${VERSION})"
28+
29+
# create a draft github release
30+
gh_releases_createDraftRelease "${TRAVIS_TAG}" "$(git show-ref --hash ${TRAVIS_TAG})"
31+
GH_RELEASE="$RESULT"
32+
33+
# Deploy the update site to bintray
34+
./mvnw clean verify -Prelease-composite
35+
36+
# Deploy to github releases
37+
gh_release_uploadAsset "$GH_RELEASE" "net.sourceforge.pmd.eclipse.p2updatesite/target/net.sourceforge.pmd.eclipse.p2updatesite-${VERSION}.zip"
1438

15-
./mvnw verify
39+
# extract the release notes
40+
RELEASE_NAME="PMD For Eclipse ${VERSION} ($(date -u +%d-%B-%Y))"
41+
BEGIN_LINE=$(grep -n "^## " ReleaseNotes.md|head -1|cut -d ":" -f 1)
42+
END_LINE=$(grep -n "^## " ReleaseNotes.md|head -2|tail -1|cut -d ":" -f 1)
43+
END_LINE=$((END_LINE - 1))
1644

45+
RELEASE_BODY="A new PMD for Eclipse plugin version has been released.
46+
It is available via the update site: https://dl.bintray.com/pmd/pmd-eclipse-plugin/updates/
1747
18-
if [ "${TRAVIS_PULL_REQUEST}" = "false" ] && [ "${TRAVIS_SECURE_ENV_VARS}" = "true" ]; then
19-
# Uploading the update site to Bintray
20-
./mvnw verify -DskipTests -Psnapshot-properties -Prelease-composite
21-
# Cleanup old snapshots
22-
(
23-
cd net.sourceforge.pmd.eclipse.p2updatesite
24-
./cleanup-bintray-snapshots.sh
25-
)
48+
$(cat ReleaseNotes.md|head -$END_LINE|tail -$((END_LINE - BEGIN_LINE)))
49+
"
50+
51+
gh_release_updateRelease "$GH_RELEASE" "$RELEASE_NAME" "$RELEASE_BODY"
52+
53+
# Publish release
54+
gh_release_publishRelease "$GH_RELEASE"
55+
56+
elif [[ "${VERSION}" == *-SNAPSHOT ]]; then
57+
log_info "This is a snapshot build (version: ${VERSION})"
58+
59+
# Uploading the update site to Bintray
60+
./mvnw verify -DskipTests -Psnapshot-properties -Prelease-composite
61+
# Cleanup old snapshots
62+
(
63+
cd net.sourceforge.pmd.eclipse.p2updatesite
64+
./cleanup-bintray-snapshots.sh
65+
)
66+
67+
else
68+
# other build. Can happen during release: the commit with a non snapshot version is built, but not from the tag.
69+
log_info "This is some other build, probably during release: commit with a non-snapshot version on branch master..."
70+
# we stop here - no need to execute further steps
71+
exit 0
72+
fi
73+
74+
else
75+
log_info "This is neither a pull request nor a push. Not executing any build."
76+
exit 1
2677
fi
78+

.travis/github-releases-api.sh

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
#
2+
# The functions here require the following scripts:
3+
# .travis/logger.sh
4+
#
5+
# The functions here require the following environment variables:
6+
# GITHUB_OAUTH_TOKEN
7+
#
8+
9+
#
10+
# Creates a new release on github with the given tag and target_commit.
11+
# The release is draft and not published.
12+
#
13+
# $RESULT = release json string
14+
#
15+
# See: https://developer.github.com/v3/repos/releases/#create-a-release
16+
#
17+
function gh_releases_createDraftRelease() {
18+
local tagName="$1"
19+
local targetCommitish="$2"
20+
21+
log_debug "$FUNCNAME: Creating new draft release for tag=$tagName and commit=$targetCommitish"
22+
23+
local request=$(cat <<-EOF
24+
{
25+
"tag_name": "${tagName}",
26+
"target_commitish": "${targetCommitish}",
27+
"name": "${tagName}",
28+
"draft": true
29+
}
30+
EOF
31+
)
32+
33+
log_debug "POST https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases"
34+
log_info "Creating gihtub draft release"
35+
RESULT=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \
36+
-H "Content-Type: application/json" \
37+
-X POST \
38+
--data "${request}" \
39+
"https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases")
40+
log_debug " -> response: $RESULT"
41+
42+
log_success "Created draft release with id $(echo $RESULT | jq --raw-output ".url")"
43+
}
44+
45+
#
46+
# Gets the latest release, if it is a draft and returns with 0.
47+
# Returns with 1, if the latest release is not a draft - meaning, there is no
48+
# draft release (yet?).
49+
#
50+
# RESULT = release json string
51+
#
52+
# See: https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository
53+
#
54+
function gh_releases_getLatestDraftRelease() {
55+
log_debug "$FUNCNAME"
56+
log_debug "GET https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases?per_page=1"
57+
RESULT=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \
58+
"https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases?per_page=1" | jq ".[0]")
59+
log_debug " -> response: $RESULT"
60+
local draft=$(echo $RESULT | jq ".draft")
61+
if [ "$draft" != "true" ]; then
62+
RESULT=""
63+
log_error "Could not find draft release!"
64+
return 1
65+
fi
66+
log_info "Found draft release: $(echo $RESULT | jq --raw-output ".url")"
67+
}
68+
69+
#
70+
# Deletes a release.
71+
#
72+
# See: https://developer.github.com/v3/repos/releases/#delete-a-release
73+
#
74+
function gh_release_deleteRelease() {
75+
local release="$1"
76+
77+
gh_release_getIdFromData "$release"
78+
local releaseId="$RESULT"
79+
log_debug "$FUNCNAME id=$releaseId"
80+
log_debug "DELETE https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/$releaseId"
81+
log_info "Deleting github release $releaseId"
82+
local response
83+
response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \
84+
-X DELETE \
85+
"https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/$releaseId")
86+
log_debug " -> response: $response"
87+
log_success "Deleted release with id $releaseId"
88+
}
89+
90+
#
91+
# Determines the release id from the given JSON release data.
92+
#
93+
# RESULT = "the release id"
94+
#
95+
function gh_release_getIdFromData() {
96+
local release="$1"
97+
98+
RESULT=$(echo $release | jq --raw-output ".id")
99+
}
100+
101+
#
102+
# Uploads a asset to an existing release.
103+
#
104+
# See: https://developer.github.com/v3/repos/releases/#upload-a-release-asset
105+
#
106+
function gh_release_uploadAsset() {
107+
local release="$1"
108+
local filename="$2"
109+
local name=$(basename $filename)
110+
111+
gh_release_getIdFromData "$release"
112+
local releaseId="$RESULT"
113+
log_debug "$FUNCNAME: releaseId=$releaseId file=$filename name=$name"
114+
115+
local uploadUrl=$(echo "$release" | jq --raw-output ".upload_url")
116+
uploadUrl="${uploadUrl%%\{\?name,label\}}"
117+
uploadUrl="${uploadUrl}?name=${name}"
118+
log_debug "POST $uploadUrl"
119+
log_info "Uploading $filename to github release $releaseId"
120+
local response
121+
response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \
122+
-H "Content-Type: application/zip" \
123+
--data-binary "@$filename" \
124+
-X POST \
125+
"${uploadUrl}")
126+
log_debug " -> response: $response"
127+
log_success "Uploaded release asset $filename for release $releaseId"
128+
}
129+
130+
#
131+
# Updates the release info: name and body.
132+
# The body is escaped to fit into JSON, so it is allowed for the body to be
133+
# a multi-line string.
134+
#
135+
# See: https://developer.github.com/v3/repos/releases/#edit-a-release
136+
#
137+
function gh_release_updateRelease() {
138+
local release="$1"
139+
local name="$2"
140+
local body="$3"
141+
142+
gh_release_getIdFromData "$release"
143+
local releaseId="$RESULT"
144+
log_debug "$FUNCNAME releaseId=$releaseId name=$name"
145+
146+
body="${body//'\'/\\\\}"
147+
body="${body//$'\r'/}"
148+
body="${body//$'\n'/\\r\\n}"
149+
body="${body//'"'/\\\"}"
150+
151+
local request=$(cat <<-EOF
152+
{
153+
"name": "${name}",
154+
"body": "${body}"
155+
}
156+
EOF
157+
)
158+
159+
log_debug "PATCH https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/${releaseId}"
160+
log_debug " -> request: $request"
161+
log_info "Updating github release $releaseId"
162+
local response
163+
response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \
164+
-H "Content-Type: application/json" \
165+
--data "${request}" \
166+
-X PATCH \
167+
"https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/${releaseId}")
168+
log_debug " -> response: $response"
169+
log_success "Updated release with id=$releaseId"
170+
}
171+
172+
#
173+
# Publish a release by setting draft="false".
174+
# Note: This will send out the notification emails if somebody
175+
# watched the releases.
176+
#
177+
# See: https://developer.github.com/v3/repos/releases/#edit-a-release
178+
#
179+
function gh_release_publishRelease() {
180+
local release="$1"
181+
182+
gh_release_getIdFromData "$release"
183+
local releaseId="$RESULT"
184+
log_debug "$FUNCNAME releaseId=$releaseId"
185+
186+
local request='{"draft":false}'
187+
log_debug "PATCH https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/${releaseId}"
188+
log_debug " -> request: $request"
189+
log_info "Publishing github release $releaseId"
190+
local response
191+
response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \
192+
-H "Content-Type: application/json" \
193+
--data "${request}" \
194+
-X PATCH \
195+
"https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/${releaseId}")
196+
log_debug " -> response: $response"
197+
local htmlUrl=$(echo "$response" | jq --raw-output ".html_url")
198+
log_success "Published release with id=$releaseId at $htmlUrl"
199+
}
200+

.travis/logger.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
COL_GREEN="\e[32m"
4+
COL_RED="\e[31m"
5+
COL_RESET="\e[0m"
6+
COL_YELLOW="\e[33;1m"
7+
8+
function log_error() {
9+
echo -e "${COL_RED}[ERROR ] $*${COL_RESET}"
10+
}
11+
12+
function log_info() {
13+
echo -e "${COL_YELLOW}[INFO ] $*${COL_RESET}"
14+
}
15+
16+
function log_success() {
17+
echo -e "${COL_GREEN}[SUCCESS] $*${COL_RESET}"
18+
}
19+
20+
function log_debug() {
21+
true
22+
#echo -e "[DEBUG ] $*"
23+
}

.travis/release.sh

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)