name: Docker Build And Push To Docker Hub

on:
  push:
    branches: [ main ]
    paths: 
      - 'Dockerfile'
      - 'cmd.sh'
      - 'entrypoint.sh'
    tags:
      - v**
jobs:
  build:
    name: Build Docker image
    runs-on: ubuntu-latest
    env:
      TAGS: latest
    steps:
      - uses: actions/checkout@v2
        name: Check out code

      - name: Set appname
        run: |
          repo=${{github.event.repository.name}}
          arrayrepo=(${repo//-/ })
          APP_NAME=${arrayrepo[1]}
          echo "APP_NAME=${APP_NAME}" >> $GITHUB_ENV
        
      - name: Get all tags when release a tag
        if: contains(github.ref_name, 'v')
        run: |
         tmptag=${{ github.ref_name}}
         arraytag=(${tmptag//./ })
         TAGS=${arraytag[0]},${arraytag[0]}.${arraytag[1]},${{ github.ref_name}},latest
         echo "TAGS=${TAGS}" >> $GITHUB_ENV
         
      - uses: mr-smithers-excellent/docker-build-push@v5
        name: Build & push Docker image
        with:
          image: websoft9dev/${{env.APP_NAME}}
          tags: ${{env.TAGS}}
          registry: docker.io
          dockerfile: Dockerfile
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
