2024-05-14 07:40:38 +02:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
say() {
|
|
|
|
if [ -n "$2" ]; then
|
|
|
|
printf "🤖 \e[32mgitea-package\e[0m \e[36m[⚒️ %s]\e[0m: %s \n" "$2" "$1"
|
|
|
|
else
|
|
|
|
printf "🤖 \e[32mgitea-package\e[0m: %s \n" "$1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
sayE() {
|
|
|
|
if [ -n "$2" ]; then
|
|
|
|
printf "🤖 \e[31mgitea-package\e[0m \e[36m[⚒️ %s]\e[0m: %s \n" "$2" "$1" 1>&2
|
|
|
|
else
|
|
|
|
printf "🤖 \e[31mgitea-package\e[0m: %s \n" "$1" 1>&2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
sayW() {
|
|
|
|
if [ -n "$2" ]; then
|
|
|
|
printf "🤖 \e[33mgitea-package\e[0m \e[36m[⚒️ %s]\e[0m: %s \n" "$2" "$1" 1>&2
|
|
|
|
else
|
|
|
|
printf "🤖 \e[33mgitea-package\e[0m: %s \n" "$1" 1>&2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-05-17 09:57:41 +02:00
|
|
|
# TODO: Disabled for now. No default help output needed if just running as
|
|
|
|
# Woodpecker CI plugin.
|
|
|
|
#
|
|
|
|
#showHelp() {
|
|
|
|
# cat << HELP
|
|
|
|
# 🤖 gitea-package Woodpecker-CI Plugin
|
|
|
|
#
|
|
|
|
# Syntax:
|
|
|
|
#HELP
|
|
|
|
#}
|
2024-05-14 07:40:38 +02:00
|
|
|
|
|
|
|
showENV() {
|
|
|
|
env | sort
|
|
|
|
}
|
|
|
|
|
|
|
|
showSettings() {
|
|
|
|
say "PLUGIN_OWNER: $PLUGIN_OWNER" "showSettings"
|
|
|
|
say "PLUGIN_PACKAGE_NAME: $PLUGIN_PACKAGE_NAME" "showSettings"
|
|
|
|
say "PLUGIN_PACKAGE_VERSION: $PLUGIN_PACKAGE_VERSION" "showSettings"
|
|
|
|
say "PLUGIN_FILE_NAME: $PLUGIN_FILE_NAME" "showSettings"
|
2024-05-14 10:24:21 +02:00
|
|
|
say "PLUGIN_FILE_SOURCE: $PLUGIN_FILE_SOURCE" "showSettings"
|
2024-05-14 07:40:38 +02:00
|
|
|
say "PLUGIN_UPDATE: $PLUGIN_UPDATE" "showSettings"
|
|
|
|
}
|
|
|
|
|
2024-05-14 10:24:21 +02:00
|
|
|
testArtifact() {
|
|
|
|
tout=$(curl --silent --output /dev/null --write-out "%{http_code}" \
|
|
|
|
"$CI_FORGE_URL/api/packages/$PLUGIN_OWNER/generic/$PLUGIN_PACKAGE_NAME/$PLUGIN_PACKAGE_VERSION/$PLUGIN_FILE_NAME")
|
|
|
|
|
|
|
|
if [ "$tout" = "200" ]; then
|
|
|
|
echo "true"
|
|
|
|
else
|
|
|
|
echo "false"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteArtifact() {
|
|
|
|
dout=$(curl --silent --output /dev/null --write-out "%{http_code}" \
|
|
|
|
--user "$PLUGIN_USER:$PLUGIN_PASSWORD" -X DELETE \
|
|
|
|
"$CI_FORGE_URL/api/packages/$PLUGIN_OWNER/generic/$PLUGIN_PACKAGE_NAME/$PLUGIN_PACKAGE_VERSION/$PLUGIN_FILE_NAME")
|
|
|
|
|
|
|
|
if [ "$dout" = "204" ]; then
|
|
|
|
say "Old package file deleted" "deleteArtifact"
|
|
|
|
elif [ "$dout" = "404" ]; then
|
|
|
|
sayE "The package or file was not found." "deleteArtifact"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
sayE "Unknown curl response! ($dout)" "deleteArtifact"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-05-14 07:40:38 +02:00
|
|
|
uploadArtifact() {
|
2024-05-17 09:57:41 +02:00
|
|
|
say "Testing if the given artifact already exists in the package registry..." "uploadArtifact"
|
2024-05-14 10:24:21 +02:00
|
|
|
fexist=$(testArtifact)
|
|
|
|
if [ "$fexist" = "true" ]; then
|
|
|
|
if [ -n "$PLUGIN_UPDATE" ]; then
|
2024-05-17 09:57:41 +02:00
|
|
|
sayW "🚧 Given file already exists. Removing remote file..." "uploadArtifact"
|
2024-05-14 10:24:21 +02:00
|
|
|
deleteArtifact
|
|
|
|
else
|
2024-05-17 09:57:41 +02:00
|
|
|
sayW "🛑 File already exists in the package registry." "uploadArtifact"
|
2024-05-14 10:24:21 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2024-05-17 09:57:41 +02:00
|
|
|
say "Starting file upload... ($PLUGIN_FILE_SOURCE)" "uploadArtifact"
|
2024-05-14 10:24:21 +02:00
|
|
|
cout=$(curl --silent --output /dev/null --write-out "%{http_code}" \
|
|
|
|
--user "$PLUGIN_USER:$PLUGIN_PASSWORD" \
|
|
|
|
--upload-file "$PLUGIN_FILE_SOURCE" \
|
|
|
|
"$CI_FORGE_URL/api/packages/$PLUGIN_OWNER/generic/$PLUGIN_PACKAGE_NAME/$PLUGIN_PACKAGE_VERSION/$PLUGIN_FILE_NAME")
|
2024-05-17 09:57:41 +02:00
|
|
|
say "Curl http response code: $cout" "uploadArtifact"
|
2024-05-14 10:24:21 +02:00
|
|
|
|
|
|
|
if [ "$cout" = "201" ]; then
|
2024-05-17 09:57:41 +02:00
|
|
|
say "Upload sucessfully finished. ✅" "uploadArtifact"
|
2024-05-14 10:24:21 +02:00
|
|
|
exit 0
|
|
|
|
elif [ "$cout" = "400" ]; then
|
2024-05-17 09:57:41 +02:00
|
|
|
sayE "Upload failed! (Bad Request) 💣" "uploadArtifact"
|
2024-05-14 10:24:21 +02:00
|
|
|
exit 1
|
|
|
|
elif [ "$cout" = "409" ]; then
|
2024-05-17 09:57:41 +02:00
|
|
|
sayE "File already exists in package version! 🛑" "uploadArtifact"
|
2024-05-14 10:24:21 +02:00
|
|
|
else
|
2024-05-17 09:57:41 +02:00
|
|
|
sayE "Unknown upload response! ($cout) 💣" "uploadArtifact"
|
2024-05-14 10:24:21 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2024-05-14 07:40:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
|
|
|
if [ -n "$PLUGIN_DEBUG" ]; then
|
2024-05-17 09:57:41 +02:00
|
|
|
sayW "🐞 Debug mode enabled."
|
2024-05-14 07:40:38 +02:00
|
|
|
showSettings
|
2024-05-14 10:24:21 +02:00
|
|
|
#sayW "Available ENV vars:"
|
|
|
|
#showENV
|
2024-05-14 07:40:38 +02:00
|
|
|
fi
|
2024-05-14 10:24:21 +02:00
|
|
|
uploadArtifact
|
2024-05-14 07:40:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|