144 lines
4.7 KiB
YAML
144 lines
4.7 KiB
YAML
name: Build
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- "**"
|
|
tags:
|
|
- "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:
|
|
name: Test and build
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Check out source
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Go
|
|
uses: actions/setup-go@v5
|
|
with:
|
|
go-version-file: go.mod
|
|
cache: false
|
|
|
|
- name: Test
|
|
run: go test -race ./...
|
|
|
|
- name: Vet
|
|
run: go vet ./...
|
|
|
|
- name: Build release archives
|
|
shell: bash
|
|
env:
|
|
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"
|
|
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
|
|
for target in \
|
|
linux/amd64 linux/arm64 \
|
|
darwin/amd64 darwin/arm64 \
|
|
windows/amd64 windows/arm64
|
|
do
|
|
target_os="${target%/*}"
|
|
target_arch="${target#*/}"
|
|
archive="bdrclone-${package_version}-${target_os}-${target_arch}"
|
|
executable="bdrclone"
|
|
if [[ "$target_os" == "windows" ]]; then
|
|
executable="bdrclone.exe"
|
|
fi
|
|
|
|
package_dir="dist/$archive"
|
|
mkdir -p "$package_dir"
|
|
CGO_ENABLED=0 GOOS="$target_os" GOARCH="$target_arch" \
|
|
go build -trimpath -ldflags "-s -w $version_flags" \
|
|
-o "$package_dir/$executable" ./cmd/bdrclone
|
|
cp README.md "$package_dir/"
|
|
tar -C dist -czf "dist/$archive.tar.gz" "$archive"
|
|
rm -rf "$package_dir"
|
|
done
|
|
|
|
cd dist
|
|
sha256sum ./*.tar.gz > SHA256SUMS
|
|
|
|
- name: Publish Gitea package
|
|
if: startsWith(github.ref, 'refs/tags/v') || inputs.publish_version != ''
|
|
shell: bash
|
|
env:
|
|
GITEA_TOKEN: ${{ secrets.PACKAGE_TOKEN }}
|
|
GITEA_SERVER_URL: ${{ github.server_url }}
|
|
GITEA_OWNER: ${{ github.repository_owner }}
|
|
GITEA_REPOSITORY: ${{ github.repository }}
|
|
PACKAGE_INTERNAL_URL: ${{ vars.PACKAGE_INTERNAL_URL }}
|
|
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_server="${PACKAGE_INTERNAL_URL:-$GITEA_SERVER_URL}"
|
|
gitea_host="${GITEA_SERVER_URL#*://}"
|
|
gitea_host="${gitea_host%%/*}"
|
|
package_url="$package_server/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 \
|
|
--retry 5 --retry-all-errors --retry-delay 3 \
|
|
--connect-timeout 20 --max-time 300 \
|
|
--request PUT \
|
|
--header "Authorization: token $GITEA_TOKEN" \
|
|
--header "Host: $gitea_host" \
|
|
--upload-file "$package_file" \
|
|
"$package_url/$(basename "$package_file")"
|
|
done
|
|
|
|
curl --fail-with-body --silent --show-error \
|
|
--retry 5 --retry-all-errors --retry-delay 3 \
|
|
--connect-timeout 20 --max-time 300 \
|
|
--request POST \
|
|
--header "Authorization: token $GITEA_TOKEN" \
|
|
--header "Host: $gitea_host" \
|
|
"$package_server/api/v1/packages/$GITEA_OWNER/generic/bdrclone/-/link/$repository_name"
|
|
|
|
- name: Upload temporary build artifacts
|
|
if: ${{ !startsWith(github.ref, 'refs/tags/v') && inputs.publish_version == '' }}
|
|
continue-on-error: true
|
|
uses: actions/upload-artifact@v3
|
|
with:
|
|
name: bdrclone-build-${{ github.run_number }}
|
|
path: |
|
|
dist/*.tar.gz
|
|
dist/SHA256SUMS
|
|
if-no-files-found: error
|
|
retention-days: 30
|