name: Build pull request
on:
  pull_request:
    types: [opened, synchronize]

env:
  REPO: kerberos/agent

jobs:
  build:
    runs-on: ${{ matrix.runner }}
    permissions:
      contents: write
    strategy:
      matrix:
        include:
          - architecture: amd64
            runner: ubuntu-24.04
            dockerfile: Dockerfile
          - architecture: arm64
            runner: ubuntu-24.04-arm
            dockerfile: Dockerfile.arm64
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - uses: benjlevesque/short-sha@v2.1
        id: short-sha
        with:
          length: 7
      - name: Run Build
        run: |
          docker build -t ${{ matrix.architecture }} -f ${{ matrix.dockerfile }} .
          CID=$(docker create ${{matrix.architecture}})
          docker cp ${CID}:/home/agent ./output-${{matrix.architecture}}
          docker rm ${CID}
      - name: Strip binary
        run: tar -cf agent-${{matrix.architecture}}.tar -C output-${{matrix.architecture}} . && rm -rf output-${{matrix.architecture}}
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: agent-${{matrix.architecture}}.tar
          path: agent-${{matrix.architecture}}.tar

