5.0 KiB · text History 3208487
# The vgl CLI, packaged as .deb and .rpm (cli/Dockerfile) and published to
# the forge's own apt and rpm repositories on every push to master, as
# gitgud's `gg` is (its build.yml `cli` job).
#
# cli/Dockerfile builds the crate twice, once per packaging family, since
# a Rust binary is linked against the glibc of whatever compiled it, and
# runs the CLI's fmt/clippy/test gate before packaging anything.
#
# No secrets: the per-job token is authorized to publish to this
# repository's own package registries (and to write the build cache).
name: cli
on:
  push:
    branches: [master]
    paths:
      - "cli/**"
      - "LICENSE"
      - ".forgejo/workflows/cli.yml"
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: false

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

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

      # --cache-to writes to the registry, with the same per-job token the
      # image workflow pushes with.
      - uses: docker/login-action@v3
        with:
          registry: ${{ vars.CI_REGISTRY }}
          username: ci
          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 (set CI_FORGE_URL)." >&2
            exit 1
          fi
          echo "FORGE_URL=$forge_url" >> "$GITHUB_ENV"

      - name: Build the .deb and .rpm
        env:
          COMMIT: ${{ github.sha }}
          SUITES: trixie forky
        run: |
          set -euo pipefail

          # The commit's date, not the clock: the same commit builds the
          # same version, and the .deb and .rpm of one run agree.
          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' cli/Cargo.toml | head -1)
          host=${FORGE_URL#*://}

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

          ls -l cli-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 cli-dist/vgl_*+"${suite}"_*.deb)
            echo "publishing $package to $suite"
            curl --fail-with-body -sS \
              -u "ci:$TOKEN" \
              -H "Content-Type: application/vnd.debian.binary-package" \
              --data-binary "@$package" \
              "$FORGE_URL/pkg/deb/$REPOSITORY/upload?dist=$suite&component=main"
            echo
          done

      # One rpm repository, no suites: the package's EVR orders it.
      - name: Publish to the rpm repository
        env:
          REPOSITORY: ${{ github.repository }}
          TOKEN: ${{ github.token }}
        run: |
          set -euo pipefail
          for package in cli-dist/vgl-*.rpm; do
            echo "publishing $package"
            curl --fail-with-body -sS \
              -u "ci:$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/vigil.list
          sudo apt update && sudo apt install vgl

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

          sudo dnf install vgl
          EOF