name: package on: push: branches: [main] # Only a version change produces a release; other commits stay off this path. paths: [version] workflow_dispatch: permissions: contents: write # Releases are ordered: a second run waits instead of racing for the same tag. concurrency: group: package cancel-in-progress: false jobs: package: # Arch containers cannot be stored on the NTFS runner disk: pacman's local # database uses ':' in epoch directory names, which NTFS rejects. The package # is therefore built by makepkg on the Arch host itself. runs-on: arch-host env: REPO_TOKEN: ${{ secrets.REPO_TOKEN }} GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }} # makepkg writes the keyring it imports into; the job keeps it out of the # checkout and removes it with the runner's temporary directory. GNUPGHOME: ${{ runner.temp }}/gnupg steps: - uses: actions/checkout@v4 - name: Read the version file id: version run: | set -euo pipefail version="$(tr -d '[:space:]' < version)" if [ -z "$version" ]; then echo "::error file=version::the version file is empty" exit 1 fi echo "version=$version" >> "$GITHUB_OUTPUT" - name: Import the package signing key if: env.GPG_PRIVATE_KEY != '' run: | set -euo pipefail mkdir -p "$GNUPGHOME" chmod 700 "$GNUPGHOME" printf '%s' "$GPG_PRIVATE_KEY" | base64 -d | gpg --batch --import gpg --batch --list-secret-keys --keyid-format=long - name: Build the Arch package id: package run: | set -euo pipefail version='${{ steps.version.outputs.version }}' work="$RUNNER_TEMP/build" rm -rf "$work" mkdir -p "$work" cp PKGBUILD version "$work/" git archive --format=tar.gz --prefix="fws-diskcleaner-$version/" \ -o "$work/fws-diskcleaner-$version.tar.gz" HEAD # makepkg needs the PKGBUILD, the version file it reads and the source # tarball in one directory; the runner's checkout stays untouched. cd "$work" # The packager and the signing key live in makepkg's configuration, and # makepkg has no command-line flag for either. conf="$work/makepkg.conf" cp /etc/makepkg.conf "$conf" printf 'PACKAGER="%s"\n' 'BlackAngelTVdev ' >> "$conf" sign=() if gpg --batch --list-secret-keys 2>/dev/null | grep -q '^sec'; then gpgkey="$(gpg --batch --with-colons --list-secret-keys \ | awk -F: '/^fpr:/{print $10; exit}')" printf 'GPGKEY="%s"\n' "$gpgkey" >> "$conf" sign=(--sign) else echo "::warning::no signing key available, releasing an unsigned package" fi makepkg --config "$conf" --noconfirm --nodeps --force --cleanbuild "${sign[@]}" asset="$(ls -1 ./*.pkg.tar.zst | grep -v -- '-debug-' | head -1)" echo "file=$work/${asset##*/}" >> "$GITHUB_OUTPUT" echo "name=${asset##*/}" >> "$GITHUB_OUTPUT" if [ -f "${asset}.sig" ]; then echo "sig=$work/${asset##*/}.sig" >> "$GITHUB_OUTPUT" fi - name: Verify the signature if: steps.package.outputs.sig != '' env: PACKAGE_FILE: ${{ steps.package.outputs.file }} SIGNATURE: ${{ steps.package.outputs.sig }} run: | set -euo pipefail gpg --batch --verify "$SIGNATURE" "$PACKAGE_FILE" - name: Create the release and attach the artefacts env: GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} PACKAGE_FILE: ${{ steps.package.outputs.file }} PACKAGE_NAME: ${{ steps.package.outputs.name }} SIGNATURE: ${{ steps.package.outputs.sig }} run: | set -euo pipefail api="$GITHUB_SERVER_URL/api/v1/repos/$GITHUB_REPOSITORY" version='${{ steps.version.outputs.version }}' tag="v$version" auth="Authorization: token $GITEA_TOKEN" release_id="$(curl -sS -H "$auth" "$api/releases/tags/$tag" | jq -r 'if type == "object" then (.id // empty) else empty end')" if [ -z "$release_id" ]; then body="FWS-DiskCleaner $version Arch package: \`$PACKAGE_NAME\` Signature: \`$PACKAGE_NAME.sig\`, key \`fws-package-signing.asc\` Verify the download with: \`\`\` gpg --verify $PACKAGE_NAME.sig $PACKAGE_NAME \`\`\`" release_id="$(curl -sS --fail-with-body -X POST -H "$auth" -H 'Content-Type: application/json' \ -d "$(jq -n --arg tag "$tag" --arg name "FWS-DiskCleaner $version" \ --arg body "$body" '{tag_name: $tag, name: $name, body: $body, draft: false, prerelease: false}')" \ "$api/releases" | jq -r '.id')" fi # A re-run must not fail on its own previous asset, so an asset with the # same name is replaced rather than duplicated. previous="$(curl -sS -H "$auth" "$api/releases/$release_id/assets")" attach() { local file="$1" name="$2" id id="$(jq -r --arg n "$name" '.[] | select(.name == $n) | .id' <<<"$previous")" if [ -n "$id" ]; then curl -sS -X DELETE -H "$auth" "$api/releases/assets/$id" -o /dev/null fi curl -sS --fail-with-body -X POST -H "$auth" \ -F "attachment=@$file" \ "$api/releases/$release_id/assets?name=$name" -o /dev/null echo "attached $name" } attach "$PACKAGE_FILE" "$PACKAGE_NAME" if [ -n "$SIGNATURE" ]; then attach "$SIGNATURE" "$PACKAGE_NAME.sig" attach packaging/fws-package-signing.asc fws-package-signing.asc fi - name: Publish to repo.laxacube.ch env: REPO_TOKEN: ${{ secrets.REPO_TOKEN }} PACKAGE_FILE: ${{ steps.package.outputs.file }} PACKAGE_NAME: ${{ steps.package.outputs.name }} run: | set -euo pipefail if [ -z "${REPO_TOKEN:-}" ]; then echo "::warning::REPO_TOKEN is not set: the release exists but repo.laxacube.ch was not updated." exit 0 fi # A pacman version is immutable: clients cache the package by its # name, so a second build published under the same name leaves them # disagreeing and pacman reports the download as corrupted. Once the # repository serves the name there is nothing left to publish: a # re-run is a no-op, and a changed build needs a pkgrel bump so it # gets its own package name. url="https://repo.laxacube.ch/x86_64/$PACKAGE_NAME" local_sha256="$(sha256sum "$PACKAGE_FILE" | cut -d' ' -f1)" repo_sha256="" if curl -fsSL "$url" -o "$RUNNER_TEMP/repo.pkg" 2>/dev/null; then repo_sha256="$(sha256sum "$RUNNER_TEMP/repo.pkg" | cut -d' ' -f1)" fi if [ -n "$repo_sha256" ]; then if [ "$repo_sha256" = "$local_sha256" ]; then echo "$PACKAGE_NAME is already published to repo.laxacube.ch, nothing to do" else echo "::warning::$PACKAGE_NAME is already published to repo.laxacube.ch with sha256 $repo_sha256, this build is $local_sha256 and is not published" echo "::warning::bump pkgrel in PKGBUILD to publish this build under its own package name" fi exit 0 fi curl -sS --fail-with-body -X POST https://repo.laxacube.ch/api/packages \ -H "Authorization: Bearer $REPO_TOKEN" \ -H "X-Filename: $PACKAGE_NAME" \ -H 'Content-Type: application/octet-stream' \ --data-binary "@$PACKAGE_FILE" echo # The index is rebuilt server-side and the file is served through a # CDN, so a HEAD is not enough: the index and the file can disagree # for as long as the CDN caches the previous build under this name, # and the checksum is exactly what pacman verifies before installing. # A freshly accepted upload is not routable instantly, so an absent # file is retried before giving up; different bytes are a real # mismatch and fail at once, since that is what breaks pacman. expected="$local_sha256" code=000 for _ in $(seq 1 24); do code="$(curl -sS -o "$RUNNER_TEMP/served.pkg" -w '%{http_code}' "$url" || true)" if [ "$code" = 200 ]; then served="$(sha256sum "$RUNNER_TEMP/served.pkg" | cut -d' ' -f1)" if [ "$served" = "$expected" ]; then echo "published $url" exit 0 fi echo "::error::$url serves sha256 $served instead of $expected" echo "::error::the CDN is still caching another build under this name: purge it before re-running" exit 1 fi sleep 5 done if [ "$code" = 404 ]; then echo "::warning::$url is not visible yet; the repository accepted the upload, pacman will see it once the index is rebuilt" exit 0 fi echo "::error::$url answered HTTP $code after the upload" exit 1