diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 62aa3cd..dc981f9 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -8,9 +8,15 @@ on: - "v*" pull_request: workflow_dispatch: + inputs: + publish_version: + description: Version to publish to the Gitea package registry + required: false + type: string permissions: contents: read + packages: write jobs: build: @@ -38,14 +44,23 @@ jobs: BUILD_REF: ${{ github.ref }} BUILD_REF_NAME: ${{ github.ref_name }} BUILD_SHA: ${{ github.sha }} + PUBLISH_VERSION: ${{ inputs.publish_version }} run: | set -euo pipefail package_version="snapshot-${BUILD_SHA:0:8}" version_flags="" if [[ "$BUILD_REF" == refs/tags/v* ]]; then - package_version="${BUILD_REF_NAME//\//-}" - version_flags="-X main.version=$BUILD_REF_NAME" + package_version="$BUILD_REF_NAME" + elif [[ -n "$PUBLISH_VERSION" ]]; then + package_version="$PUBLISH_VERSION" + fi + if [[ "$package_version" != snapshot-* ]]; then + if [[ ! "$package_version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+([.-][0-9A-Za-z.-]+)?$ ]]; then + echo "Invalid release version: $package_version" >&2 + exit 1 + fi + version_flags="-X main.version=$package_version" fi mkdir -p dist @@ -84,3 +99,33 @@ jobs: dist/SHA256SUMS if-no-files-found: error retention-days: 30 + + - name: Publish Gitea package + if: startsWith(github.ref, 'refs/tags/v') || inputs.publish_version != '' + shell: bash + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + GITEA_SERVER_URL: ${{ github.server_url }} + GITEA_OWNER: ${{ github.repository_owner }} + GITEA_REPOSITORY: ${{ github.repository }} + BUILD_REF_NAME: ${{ github.ref_name }} + PUBLISH_VERSION: ${{ inputs.publish_version }} + run: | + set -euo pipefail + + package_version="${PUBLISH_VERSION:-$BUILD_REF_NAME}" + repository_name="${GITEA_REPOSITORY#*/}" + package_url="$GITEA_SERVER_URL/api/packages/$GITEA_OWNER/generic/bdrclone/$package_version" + + for package_file in dist/*.tar.gz dist/SHA256SUMS; do + curl --fail-with-body --silent --show-error \ + --request PUT \ + --header "Authorization: token $GITEA_TOKEN" \ + --upload-file "$package_file" \ + "$package_url/$(basename "$package_file")" + done + + curl --fail-with-body --silent --show-error \ + --request POST \ + --header "Authorization: token $GITEA_TOKEN" \ + "$GITEA_SERVER_URL/api/v1/packages/$GITEA_OWNER/generic/bdrclone/-/link/$repository_name"