5.1 KiB · text History 6c56e6c
# The .deb and .rpm, built by packaging/Dockerfile and published to this
# repository's apt and rpm registries on the forge — as the gitgud CLI is.
name: packages
on:
  push:
    branches: [master]
    tags: ['v*']
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}

jobs:
  packages:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v4

      - uses: docker/setup-buildx-action@v3

      # `--cache-to` writes to the registry: the per-job token may push
      # under this repo's own path.
      - uses: docker/login-action@v3
        with:
          registry: ${{ vars.CI_REGISTRY }}
          username: gitgud-actions
          password: ${{ github.token }}

      - name: Resolve the forge URL
        env:
          CI_FORGE_URL: ${{ vars.CI_FORGE_URL }}
        run: |
          set -euo pipefail
          forge_url=${CI_FORGE_URL:-${GITHUB_SERVER_URL:-}}
          if [ -z "$forge_url" ]; then
            echo "No forge URL available." >&2
            exit 1
          fi
          echo "FORGE_URL=$forge_url" >> "$GITHUB_ENV"

      # A tag `vX.Y.Z` is a release: version X.Y.Z, rpm release 1. Anything
      # else is a CI build stamped with the commit's date and hash, sorting
      # before the next release (`~ci…` in Debian, `0.ci…` in rpm).
      - name: Build the .deb and .rpm
        env:
          COMMIT: ${{ github.sha }}
          REF: ${{ github.ref }}
          SUITES: trixie forky
        run: |
          set -euo pipefail
          commit_date=$(git show -s --format=%cd --date=format:%Y%m%d%H%M%S "$COMMIT" 2>/dev/null \
            || date -u +%Y%m%d%H%M%S)
          epoch=$(git show -s --format=%ct "$COMMIT" 2>/dev/null || date -u +%s)
          short=$(echo "$COMMIT" | cut -c1-12)
          version=$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -1)

          case "$REF" in
            refs/tags/v*)
              tag=${REF#refs/tags/v}
              if [ "$tag" != "$version" ]; then
                echo "tag v$tag doesn't match Cargo.toml's version $version" >&2
                exit 1
              fi
              pkg_version="$version"
              rpm_release="1"
              ;;
            *)
              pkg_version="${version}~ci${commit_date}.g${short}"
              rpm_release="0.ci${commit_date}.g${short}"
              ;;
          esac

          host=${FORGE_URL#*://}

          docker buildx build \
            --target packages \
            --build-arg PKG_VERSION="$pkg_version" \
            --build-arg RPM_RELEASE="$rpm_release" \
            --build-arg MAINTAINER="Scropple CI <noreply@${host%%/*}>" \
            --build-arg SOURCE_DATE_EPOCH="$epoch" \
            --build-arg SUITES="$SUITES" \
            --cache-from type=registry,ref=${{ vars.CI_REGISTRY }}/${{ github.repository }}:buildcache \
            --cache-to type=registry,ref=${{ vars.CI_REGISTRY }}/${{ github.repository }}:buildcache,mode=max,ignore-error=true \
            --output type=local,dest=./dist \
            --file packaging/Dockerfile .

          ls -l dist

      - name: Publish to the apt repository
        env:
          REPOSITORY: ${{ github.repository }}
          TOKEN: ${{ github.token }}
          SUITES: trixie forky
        run: |
          set -euo pipefail
          for suite in $SUITES; do
            package=$(echo dist/*+"${suite}"_*.deb)
            echo "publishing $package to $suite"
            curl --fail-with-body -sS \
              -u "gitgud-actions:$TOKEN" \
              -H "Content-Type: application/vnd.debian.binary-package" \
              --data-binary "@$package" \
              "$FORGE_URL/pkg/deb/$REPOSITORY/upload?dist=$suite&component=main"
            echo
          done

      - name: Publish to the rpm repository
        env:
          REPOSITORY: ${{ github.repository }}
          TOKEN: ${{ github.token }}
        run: |
          set -euo pipefail
          for package in dist/*.rpm; do
            echo "publishing $package"
            curl --fail-with-body -sS \
              -u "gitgud-actions:$TOKEN" \
              -H "Content-Type: application/octet-stream" \
              --data-binary "@$package" \
              "$FORGE_URL/pkg/rpm/$REPOSITORY/upload"
            echo
          done

      - name: How to install it
        env:
          REPOSITORY: ${{ github.repository }}
          OWNER: ${{ github.repository_owner }}
        run: |
          cat <<EOF
          # Debian / Ubuntu — \$(lsb_release -cs) is trixie or forky
          curl -fsSL $FORGE_URL/pkg/_keys/deb/$OWNER | sudo tee /etc/apt/keyrings/$OWNER.asc >/dev/null
          echo "deb [signed-by=/etc/apt/keyrings/$OWNER.asc] $FORGE_URL/pkg/deb/$REPOSITORY \$(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/scropple.list
          sudo apt update && sudo apt install scropple

          # Fedora / RHEL / openSUSE — /etc/yum.repos.d/scropple.repo
          [scropple]
          name=scropple
          baseurl=$FORGE_URL/pkg/rpm/$REPOSITORY
          enabled=1
          repo_gpgcheck=1
          gpgcheck=0
          gpgkey=$FORGE_URL/pkg/_keys/rpm/$OWNER

          sudo dnf install scropple
          EOF