Skip to content

Commit

Permalink
Uploads a jar file to assets (#5)
Browse files Browse the repository at this point in the history
* Uploads a jar file to assets on released.
  • Loading branch information
hiwakaba authored Jan 30, 2020
1 parent 0cfc59c commit 9e5a064
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ jobs:
if: github.event_name == 'release'
run: mvn -B deploy --file pom.xml

- name: Upload a jar file to asset
env:
GITHUB_TOKEN: ${{ github.token }}
if: github.event_name == 'release'
run: mvn -B exec:exec --file pom.xml -Pupload
116 changes: 116 additions & 0 deletions .github/workflows/upload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/sh
#
# The MIT License
#
# Copyright 2018 Yahoo Japan Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# AUTHOR: Hirotaka Wakabayashi
# CREATE: Fri, 14 Sep 2018
# REVISION:
#

# Sets the default locale. LC_ALL has precedence over other LC* variables.
unset LANG
unset LANGUAGE
LC_ALL=en_US.utf8
export LC_ALL

# Sets PATH. setup_*.sh uses useradd command
PATH=${PATH}:/usr/sbin:/sbin

# an unset parameter expansion will fail
set -u

# umask 022 is enough
umask 022

# environments
SRCDIR=$(cd $(dirname "$0") && pwd)
DEBUG=1
if test "${DEBUG}" -eq 1; then
TAG="$(basename $0) -s"
else
TAG=$(basename $0)
fi

# GITHUB_TOKEN should be defined.
if test -z "${GITHUB_TOKEN}"; then
logger -t ${TAG} -p user.error "No GITHUB_TOKEN variable defined."
exit 1
fi

# API SPEC
# https://developer.github.com/v3/repos/releases/#get-the-latest-release
TEMP_FILE=$(mktemp)
curl -sH "Authorization: token ${GITHUB_TOKEN}" "https://api.github.com/repos/hiwakaba/k2hdkc_java/releases/latest" -o ${TEMP_FILE}
if test "${?}" != "0"; then
logger -t ${TAG} -p user.error "GitHub API get-the-latest-release returned error."
rm -f ${TEMP_FILE}
exit 1
fi
UPLOAD_URL=$(cat ${TEMP_FILE} | python3 -c "import sys, json; print(json.load(sys.stdin)['upload_url'])")
if test -z "${UPLOAD_URL}"; then
logger -t ${TAG} -p user.error "UPLOAD_URL is empty(or undefined), which should be defined."
rm -f ${TEMP_FILE}
exit 1
fi
TAG_NAME=$(cat ${TEMP_FILE} | python3 -c "import sys, json; print(json.load(sys.stdin)['tag_name'])" | perl -ne '/v(.*)/; print $1;')
if test -z "${TAG_NAME}"; then
logger -t ${TAG} -p user.error "TAG_NAME is empty(or undefined), which should be defined."
rm -f ${TEMP_FILE}
exit 1
fi

# Get the jar file in local target directory.
TARGET_FILE="./target/k2hdkc-${TAG_NAME}.jar"
if ! test -f "${TARGET_FILE}"; then
logger -t ${TAG} -p user.error "${TARGET_FILE} doesn't exist, which should exist."
rm -f ${TEMP_FILE}
exit 1
fi
ASSET_FILE_NAME=$(basename ${TARGET_FILE})
if test -z "${ASSET_FILE_NAME}"; then
logger -t ${TAG} -p user.error "ASSET_FILE_NAME is empty(or undefined), which should be defined."
rm -f ${TEMP_FILE}
exit 1
fi
UPLOAD_URL_WITH_QUERY=$(echo ${UPLOAD_URL} | perl -pe "s|{\?name,label}|?name=${ASSET_FILE_NAME}|g")
if test -z "${UPLOAD_URL_WITH_QUERY}"; then
logger -t ${TAG} -p user.error "UPLOAD_URL_WITH_QUERY is empty(or undefined), which should be defined."
rm -f ${TEMP_FILE}
exit 1
fi
logger -t ${TAG} -p user.debug "TARGET_FILE:${TARGET_FILE} ASSET_FILE_NAME:${ASSET_FILE_NAME} UPLOAD_URL_WITH_QUERY:${UPLOAD_URL_WITH_QUERY}"

# API SPEC
# https://developer.github.com/v3/repos/releases/#upload-a-release-asset
# Note: curl exits with zero if the jar file already exists.
curl -s --request PATCH -L# --data-binary @"${TARGET_FILE}" -H "Authorization: token ${GITHUB_TOKEN}" -H "Content-Type: application/octet-stream" ${UPLOAD_URL_WITH_QUERY}
RET=${?}
if test "${RET}" != "0"; then
logger -t ${TAG} -p user.error "GitHub API upload-a-release-asset returned error."
else
logger -t ${TAG} -p user.info "GitHub API upload-a-release-asset success"
fi

rm -f ${TEMP_FILE}
exit ${RET}

6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
k2hdkc-java (1.1.3) stable; urgency=low

* Uploads a jar file to assets

-- Hirotaka Wakabayashi <[email protected]> Thu, 30 Jan 2020 08:07:00 +0000

k2hdkc-java (1.1.2) stable; urgency=low

* Re-implements the execute method
Expand Down
30 changes: 29 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>k2hdkc</artifactId>
<packaging>jar</packaging>

<version>1.1.2</version>
<version>1.1.3</version>
<name>k2hdkc</name>
<description>An official java driver for k2hdkc, which is a highly available and scalable distributed KVS clustering system.</description>
<url>https://github.com/yahoojapan/k2hdkc_java</url>
Expand Down Expand Up @@ -243,5 +243,33 @@
</plugins>
</build>
</profile>
<profile>
<id>upload</id>
<activation>
<property>
<name>assets</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin.version}</version>
<executions>
<execution>
<id>upload_jar</id>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>.github/workflows/upload.sh</executable>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

0 comments on commit 9e5a064

Please sign in to comment.