You've already forked composer-build-action
156 lines
6.7 KiB
Bash
Executable File
156 lines
6.7 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# This is to serve as a Plugin for Drone to enable uploading of generic packages to Gitea e.g Ansible Roles
|
|
|
|
set -eo pipefail
|
|
shopt -s nullglob
|
|
|
|
# check to see if this file is being run or sourced from another script
|
|
_is_sourced() {
|
|
# https://unix.stackexchange.com/a/215279
|
|
[ "${#FUNCNAME[@]}" -ge 2 ] &&
|
|
[ "${FUNCNAME[0]}" = '_is_sourced' ] &&
|
|
[ "${FUNCNAME[1]}" = 'source' ]
|
|
}
|
|
|
|
# logging functions
|
|
action_log() {
|
|
local type="$1"
|
|
shift
|
|
# accept argument string or stdin
|
|
local text="$*"
|
|
if [ "$#" -eq 0 ]; then text="$(cat)"; fi
|
|
local dt
|
|
dt="$(date -D 'YYYY-MM-DD hh:mm[:ss]')"
|
|
printf '%s [%s] [gitea-composer-uploader]: %s\n' "$dt" "$type" "$text"
|
|
}
|
|
action_note() {
|
|
action_log INF "$@"
|
|
}
|
|
action_warn() {
|
|
action_log WRN "$@" >&2
|
|
}
|
|
action_error() {
|
|
action_log ERR "$@" >&2
|
|
}
|
|
|
|
# Verify that the minimally required password settings are set for operation.
|
|
function verify_minimum_env {
|
|
if [ -z "$username" ]; then
|
|
action_warn "username is required for plugin operation"
|
|
fi
|
|
if [ -z "$baseurl" ]; then
|
|
action_warn "gitea_baseurl setting is required for plugin operation"
|
|
fi
|
|
if [ -z "$owner" ]; then
|
|
action_warn "gitea_owner setting is required for plugin operation"
|
|
fi
|
|
if [ -z "$access_token" ]; then
|
|
action_warn "gitea_token setting is required for plugin operation"
|
|
fi
|
|
if [ -z "$version" ]; then
|
|
action_warn "gitea_version setting is required for plugin operation"
|
|
fi
|
|
if [ -z "$username" ] ||
|
|
[ -z "$baseurl" ] ||
|
|
[ -z "$version" ] ||
|
|
[ -z "$access_token" ]; then
|
|
action_error <<-'EOF'
|
|
You need to specify one/all of the following settings:
|
|
- username
|
|
- access_token
|
|
- baseurl
|
|
EOF
|
|
fi
|
|
action_note "Sufficient configuration"
|
|
|
|
}
|
|
|
|
function delete_file {
|
|
no_prefix_version="${version##v}"
|
|
for file in ${upload_file}; do
|
|
#action_note "curl -s -o /dev/null -w '%{http_code}' --user \"${username}:${access_token}\" -X DELETE ${baseurl}/api/packages/${owner:-$username}/composer/${owner:-$username}%2F${repo_name}/${no_prefix_version}"
|
|
response=$(curl -s -o /dev/null -w "%{http_code}" --user "${username}:${access_token}" -X DELETE "${baseurl}/api/v1/packages/${owner:-$username}/composer/${owner:-$username}%2F${repo_name}/${no_prefix_version}")
|
|
if [ "${response}" == 204 ] || [ "${response}" == 200 ]; then
|
|
action_note "Deleted package version ${version} for ${owner:-$username}/${repo_name}"
|
|
elif [ "${response}" == 404 ]; then
|
|
action_error "Not Found: Odd I cannot locate your package! [bug]"
|
|
else
|
|
action_error "Response code was ${response}"
|
|
fi
|
|
done
|
|
}
|
|
|
|
function process_upload_file {
|
|
for file in ${upload_file}; do
|
|
#action_note "curl -s -o /dev/null -w '%{http_code}' --user \"${username}:${access_token}\" --upload-file ${file} \"${baseurl}/api/packages/${owner:-$username}/composer?version=${version}\""
|
|
response=$(curl -s -o /dev/null -w "%{http_code}" --user "${username}:${access_token}" --upload-file "${file}" "${baseurl}/api/packages/${owner:-$username}/composer?version=${version}")
|
|
if [ "${response}" == 409 ]; then
|
|
action_error "Conflict: File already exists"
|
|
if [ "${FILE_OVERWRITE:=$overwrite_files}" == 'true' ]; then
|
|
# Delete file as already exists
|
|
delete_file
|
|
response=$(curl -s -o /dev/null -w "%{http_code}" --user "${username}:${access_token}" --upload-file "${file}" "${baseurl}/api/packages/${owner:-$username}/composer?version=${version}")
|
|
if [ "${response}" == 409 ]; then
|
|
action_error "Conflict: File already exists"
|
|
elif [ "${response}" == 201 ]; then
|
|
action_note "File uploaded successfully"
|
|
elif [ "${response}" == 400 ]; then
|
|
action_error "Bad Request: Version likely already exists"
|
|
fi
|
|
else
|
|
action_error 'Unable to upload file. Maybe toggle overwrite_files setting to true :)'
|
|
exit 1
|
|
fi
|
|
elif [ "${response}" == 201 ]; then
|
|
action_note "File uploaded successfully"
|
|
elif [ "${response}" == 400 ]; then
|
|
action_warn "Bad Request: Version likely already exists"
|
|
if [ "${FILE_OVERWRITE:=$overwrite_files}" == 'true' ]; then
|
|
# Delete file as already exists
|
|
delete_file
|
|
response=$(curl -s -o /dev/null -w "%{http_code}" --user "${username}:${access_token}" --upload-file "${file}" "${baseurl}/api/packages/${owner:-$username}/composer?version=${version}")
|
|
if [ "${response}" == 409 ]; then
|
|
action_error "Conflict: File already exists"
|
|
elif [ "${response}" == 201 ]; then
|
|
action_note "File uploaded successfully"
|
|
elif [ "${response}" == 400 ]; then
|
|
action_error "Bad Request: Version likely already exists"
|
|
exit 1
|
|
fi
|
|
else
|
|
action_error 'Unable to upload file. Maybe toggle overwrite_files setting to true :)'
|
|
exit 1
|
|
fi
|
|
fi
|
|
done
|
|
|
|
}
|
|
|
|
function build_package(){
|
|
mkdir -p build
|
|
zip -r "build/Package.zip" \
|
|
. \
|
|
-x '.semrel/*' \
|
|
-x '.generated-go-semantic-release-changelog.md' \
|
|
-x './vendor/*' \
|
|
-x './tests/*' \
|
|
-x './build/*' \
|
|
-x './.git/*' \
|
|
-x './.idea/*' \
|
|
-x './.github/*' \
|
|
-x './scripts/*'
|
|
}
|
|
|
|
_main() {
|
|
action_note "Starting"
|
|
# verify_minimum_env "$@"
|
|
# process_upload_file "$@"
|
|
build_package "$@"
|
|
}
|
|
|
|
# If we are sourced from elsewhere, don't perform any further actions
|
|
if ! _is_sourced; then
|
|
_main "$@"
|
|
fi
|