diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e69de29 diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..163668a --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +GITEA_INSTANCE_BASE_URL="https://gitea.example.com" +GITEA_ACCESS_TOKEN="access_token" +GITEA_OWNER="owner" +GITEA_REPOSITORY="repository" +GITEA_PACKAGE_REGISTRY="composer" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e97fa76 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +/.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0153ecb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM cybercinch/base-alpine-bash:latest + +COPY scripts/docker-entrypoint.sh /entrypoint.sh + +ENV upload_file=build/Package.zip +CMD ["/entrypoint.sh"] \ No newline at end of file diff --git a/README.md b/README.md index 6ff09f3..1fe12e4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,16 @@ -# composer-build-action +

+ github gitea actions +

+# Build Composer package + +[![License](https://img.shields.io/github/license/rosven9856/gitea-package-action)](https://github.com/rosven9856/gitea-package-action/blob/master/LICENSE) + +This action will create a .zip archive to upload to Packagist repo. + + +## Example usage + +```yaml + steps: + - uses: https://hub.cybercinch.nz/cybercinch/composer-build-action@mains diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..f42a18c --- /dev/null +++ b/action.yml @@ -0,0 +1,8 @@ +name: "Gitea Composer Build package" +description: "Create a composer package from source" +runs: + using: "docker" + image: "Dockerfile" +branding: + icon: 'package' + color: 'green' \ No newline at end of file diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..75d4a76 --- /dev/null +++ b/compose.yml @@ -0,0 +1,9 @@ +services: + composer-uploader: + container_name: composer-build-package + build: + context: ./ + dockerfile: Dockerfile + working_dir: /build + volumes: + - ./:/build diff --git a/scripts/docker-entrypoint.sh b/scripts/docker-entrypoint.sh new file mode 100755 index 0000000..ce50014 --- /dev/null +++ b/scripts/docker-entrypoint.sh @@ -0,0 +1,155 @@ +#!/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