name: Docker image

on:
  push:
  workflow_dispatch:
  repository_dispatch:

jobs:
  docker-image:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - run: docker build -f Dockerfile -t btwiuse/pub:${{ github.sha }} .
      name: docker build

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

    - run: docker push btwiuse/pub:${{ github.sha }}
      name: docker push

    - run: |
        docker tag btwiuse/pub:${{ github.sha }} btwiuse/pub
        docker push btwiuse/pub
      if: github.ref == 'refs/heads/main'
      name: docker push latest

  push-devcontainer:
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
      with:
        fetch-depth: 0
        submodules: recursive

    - run: |
        ls -la
        pwd
        env
      name: probe env

    - name: build and push devcontainer
      uses: docker://btwiuse/arch:golang
      with:
        entrypoint: bash
        args: -c "sudo -E make devcontainer"
      env:
        GITHUB_TOKEN: ${{ secrets.ACTIONS_GITHUB_TOKEN }}
        DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
        DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
