|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2020 The OpenEBS Authors. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +set -e |
| 17 | + |
| 18 | +if [ "$#" -ne 3 ]; then |
| 19 | + echo "Error: Unable to create a new release. Missing required input." |
| 20 | + echo "Usage: $0 <github org/repo> <tag-name> <branch-name>" |
| 21 | + echo "Example: $0 kmova/bootstrap v1.0.0 master" |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +C_GIT_URL=$(echo "https://api.github.com/repos/$1/releases") |
| 26 | +C_GIT_TAG_NAME=$2 |
| 27 | +C_GIT_TAG_BRANCH=$3 |
| 28 | + |
| 29 | +if [ -z ${GIT_NAME} ]; |
| 30 | +then |
| 31 | + echo "Error: Environment variable GIT_NAME not found. Please set it to proceed."; |
| 32 | + echo "GIT_NAME should be a valid GitHub username."; |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +if [ -z ${GIT_TOKEN} ]; |
| 37 | +then |
| 38 | + echo "Error: Environment variable GIT_TOKEN not found. Please set it to proceed."; |
| 39 | + echo "GIT_TOKEN should be a valid GitHub token associated with GitHub username."; |
| 40 | + echo "GIT_TOKEN should be configured with required permissions to create new release."; |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +RELEASE_CREATE_JSON=$(echo \ |
| 45 | +{ \ |
| 46 | + \"tag_name\":\"${C_GIT_TAG_NAME}\", \ |
| 47 | + \"target_commitish\":\"${C_GIT_TAG_BRANCH}\", \ |
| 48 | + \"name\":\"${C_GIT_TAG_NAME}\", \ |
| 49 | + \"body\":\"Release created via $0\", \ |
| 50 | + \"draft\":false, \ |
| 51 | + \"prerelease\":false \ |
| 52 | +} \ |
| 53 | +) |
| 54 | + |
| 55 | +#delete the temporary response file that might |
| 56 | +#have been left around by previous run of the command |
| 57 | +#using a fixed name means that this script |
| 58 | +#is not thread safe. only one execution is permitted |
| 59 | +#at a time. |
| 60 | +TEMP_RESP_FILE=temp-curl-response.txt |
| 61 | +rm -rf ${TEMP_RESP_FILE} |
| 62 | + |
| 63 | +response_code=$(curl -u ${GIT_NAME}:${GIT_TOKEN} \ |
| 64 | + -w "%{http_code}" \ |
| 65 | + --silent \ |
| 66 | + --output ${TEMP_RESP_FILE} \ |
| 67 | + --url ${C_GIT_URL} \ |
| 68 | + --request POST --header 'content-type: application/json' \ |
| 69 | + --data "$RELEASE_CREATE_JSON") |
| 70 | + |
| 71 | +#When embedding this script in other scripts like travis, |
| 72 | +#success responses like 200 can mean error. rc_code maps |
| 73 | +#the responses to either success (0) or error (1) |
| 74 | +rc_code=0 |
| 75 | + |
| 76 | +#Github returns 201 Created on successfully creating a new release |
| 77 | +#201 means the request has been fulfilled and has resulted in one |
| 78 | +#or more new resources being created. |
| 79 | +if [ $response_code != "201" ]; then |
| 80 | + echo "Error: Unable to create release. See below response for more details" |
| 81 | + #The GitHub error response is pretty well formatted. |
| 82 | + #Printing the body gives all the details to fix the errors |
| 83 | + #Sample response when the branch already exists looks like this: |
| 84 | + #{ |
| 85 | + # "message": "Validation Failed", |
| 86 | + # "errors": [ |
| 87 | + # { |
| 88 | + # "resource": "Release", |
| 89 | + # "code": "already_exists", |
| 90 | + # "field": "tag_name" |
| 91 | + # } |
| 92 | + # ], |
| 93 | + # "documentation_url": "https://developer.github.com/v3/repos/releases/#create-a-release" |
| 94 | + #} |
| 95 | + rc_code=1 |
| 96 | +else |
| 97 | + #Note. In case of success, lots of details of returned, but just |
| 98 | + #knowing that creation worked is all that matters now. |
| 99 | + echo "Successfully tagged $1 with release tag ${C_GIT_TAG_NAME} on branch ${C_GIT_TAG_BRANCH}" |
| 100 | +fi |
| 101 | +cat ${TEMP_RESP_FILE} |
| 102 | + |
| 103 | +#delete the temporary response file |
| 104 | +rm -rf ${TEMP_RESP_FILE} |
| 105 | + |
| 106 | +exit ${rc_code} |
0 commit comments