runlikers 0.1.2

Reverse-engineer docker run command line arguments based on running containers
Documentation
name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

permissions:
  contents: write
  packages: write

env:
  CARGO_TERM_COLOR: always
  REGISTRY: ghcr.io

jobs:
  build:
    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-24.04
            target: x86_64-unknown-linux-gnu
            cross: false
          - os: ubuntu-24.04
            target: x86_64-unknown-linux-musl
            cross: true
          - os: ubuntu-24.04
            target: aarch64-unknown-linux-musl
            cross: true
          - os: ubuntu-24.04
            target: aarch64-unknown-linux-gnu
            cross: true
          - os: ubuntu-24.04
            target: armv7-unknown-linux-musleabihf
            cross: true
          - os: macos-14
            target: x86_64-apple-darwin
            cross: false
          - os: macos-14
            target: aarch64-apple-darwin
            cross: false
          - os: windows-2022
            target: x86_64-pc-windows-msvc
            cross: false
          - os: windows-2022
            target: x86_64-pc-windows-gnu
            cross: false

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install NASM (Windows MSVC)
        if: matrix.target == 'x86_64-pc-windows-msvc'
        uses: ilammy/setup-nasm@v1

      - name: Install cross (for cross-compilation)
        if: matrix.cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Install musl tools (Linux musl)
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: |
          sudo apt-get update
          sudo apt-get install -y musl-tools

      - name: Build (native)
        if: ${{ !matrix.cross }}
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build (cross)
        if: matrix.cross
        run: cross build --release --target ${{ matrix.target }}

      - name: Set binary name
        id: binary
        shell: bash
        run: |
          if [[ "${{ matrix.target }}" == *"windows"* ]]; then
            echo "name=runlikers.exe" >> "$GITHUB_OUTPUT"
          else
            echo "name=runlikers" >> "$GITHUB_OUTPUT"
          fi

      - name: Rename artifact
        id: rename
        shell: bash
        run: |
          BIN="target/${{ matrix.target }}/release/${{ steps.binary.outputs.name }}"
          ARTIFACT="runlikers-${{ github.ref_name }}-${{ matrix.target }}"
          if [[ "${{ matrix.target }}" == *"windows"* ]]; then
            ARTIFACT="${ARTIFACT}.exe"
          fi
          cp "$BIN" "$ARTIFACT"
          echo "artifact_name=${ARTIFACT}" >> "$GITHUB_OUTPUT"

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.target }}
          path: ${{ steps.rename.outputs.artifact_name }}
          if-no-files-found: error

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout (for changelog + tags)
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts/

      - name: Prepare release assets
        shell: bash
        run: |
          mkdir -p release-assets
          find artifacts/ -type f | while read f; do
            cp "$f" release-assets/$(basename "$f")
          done
          cd release-assets
          sha256sum * > sha256sums.txt
          ls -la

      - name: Generate changelog
        id: changelog
        run: |
          PREV_TAG=$(git tag --sort=-creatordate | grep -v "${{ github.ref_name }}" | head -1)
          if [ -n "$PREV_TAG" ]; then
            echo "## What's Changed (${PREV_TAG} -> ${{ github.ref_name }})" > /tmp/changelog.md
            echo "" >> /tmp/changelog.md
            git log --oneline "${PREV_TAG}..HEAD" | sed 's/^/- /' >> /tmp/changelog.md
          else
            echo "## Initial Release" > /tmp/changelog.md
            echo "" >> /tmp/changelog.md
            git log --oneline --max-count=30 | sed 's/^/- /' >> /tmp/changelog.md
          fi
          echo "changelog<<EOF" >> "$GITHUB_OUTPUT"
          cat /tmp/changelog.md >> "$GITHUB_OUTPUT"
          echo "EOF" >> "$GITHUB_OUTPUT"

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          body: |
            ## runlikers ${{ github.ref_name }}
            ${{ steps.changelog.outputs.changelog }}
          files: release-assets/*
          draft: false
          prerelease: ${{ contains(github.ref_name, '-') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  # ---- Publish to crates.io ----
  publish:
    name: Publish to crates.io
    needs: release
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Check if version already published
        id: check
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          PACKAGE="$(grep -m1 '^name' Cargo.toml | sed 's/.*\"\(.*\)\"/\1/')"
          STATUS=$(curl -s -o /dev/null -w "%{http_code}" "https://crates.io/api/v1/crates/${PACKAGE}")
          if [ "$STATUS" = "404" ]; then
            echo "published=false" >> "$GITHUB_OUTPUT"
            exit 0
          fi
          EXISTS=$(curl -s "https://crates.io/api/v1/crates/${PACKAGE}" | \
            python3 -c "import sys,json; d=json.load(sys.stdin); print(str(any(v.get('num')=='${VERSION}' for v in d.get('versions',[]))).lower())" 2>/dev/null || echo "false")
          echo "published=${EXISTS}" >> "$GITHUB_OUTPUT"

      - name: Publish
        if: steps.check.outputs.published != 'true'
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish

  # ---- Docker image (multi-arch) ----
  docker:
    name: Docker Image
    needs: build
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ github.repository }}
          tags: |
            type=semver,pattern=v{{version}}
            type=semver,pattern=v{{major}}.{{minor}}
            type=semver,pattern=v{{major}}
            type=sha
            type=raw,value=latest,enable=${{ github.ref_type == 'tag' }}

      # ghcr.io - using docker/build-push-action
      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push to ghcr.io
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

      # Docker Hub - using buildx.sh
      - name: Build dockerfile (without push)
        run: |
          chmod +x ./buildx.sh
          ./buildx.sh --push false --repo ${{ secrets.DOCKER_USERNAME }} --tag ${{ github.ref_name }}

      - name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build dockerfile (with push)
        run: |
          chmod +x ./buildx.sh
          ./buildx.sh --push true --repo ${{ secrets.DOCKER_USERNAME }} --tag ${{ github.ref_name }}
          ./buildx.sh --push true --repo ${{ secrets.DOCKER_USERNAME }} --tag latest