name: Docker

# This workflow is triggered on a push to a tag that follows semantic versioning
# e.g., v1.2.3, v2.0.0-rc1
on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+**'

jobs:
  # Build and push the Docker image
  build-and-push:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      # This step extracts the version number from the tag
      # e.g., if the tag is 'v1.2.3', this will output '1.2.3'
      - name: Extract release version
        id: release_version
        run: |
          echo "version=$(echo ${{ github.ref_name }} | sed -e 's/^v//')" >> $GITHUB_OUTPUT

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

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

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

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          platforms: linux/amd64,linux/arm64/v8
          push: true
          tags: |
            wealdtech/ethdo:${{ steps.release_version.outputs.version }}
            wealdtech/ethdo:latest
