name: Docker image build

on:
  workflow_dispatch:
  push:
    paths:
      - VERSION
    branches:
      - 'main'

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - uses: actions/checkout@v3

      - name: Fetch versions in the repo
        id: fetch-versions
        run: |
          echo "jinux_version=$( cat VERSION )" >> "$GITHUB_OUTPUT"
          echo "rust_version=$( grep -m1 -o 'nightly-[0-9]\+-[0-9]\+-[0-9]\+' rust-toolchain.toml )" >> "$GITHUB_OUTPUT"

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./tools/docker/Dockerfile.ubuntu22.04
          platforms: linux/amd64
          push: true
          tags: jinuxdev/jinux:${{ steps.fetch-versions.outputs.jinux_version }}
          build-args: |
            "JINUX_RUST_VERSION=${{ steps.fetch-versions.outputs.rust_version }}"
