name: Build on push
run-name: Build on push
on:
  push:
    branches:
      - dev

jobs:
  prepare:
    runs-on: [runner]
    container: git.sh-edraft.de/sh-edraft.de/act-runner:latest
    steps:
      - name: Clone Repository
        uses: https://github.com/actions/checkout@v3
        with:
          token: ${{ secrets.CI_ACCESS_TOKEN }}

      - name: Get Date and Build Number
        run: |
          git fetch
          git tag
          DATE=$(date +'%Y.%m.%d')
          TAG_COUNT=$(git tag -l "${DATE}.*" | wc -l)
          BUILD_NUMBER=$(($TAG_COUNT + 1))
          BUILD_VERSION="${DATE}.${BUILD_NUMBER}-dev"
          echo "$BUILD_VERSION" > version.txt
          echo "VERSION $BUILD_VERSION"

      - name: Create Git Tag for Build
        run: |
          git config user.name "ci"
          git config user.email "dev@sh-edraft.de"
          echo "tag $(cat version.txt)"
          git tag $(cat version.txt)
          git push origin --tags
        
      - name: Upload build version artifact
        uses: actions/upload-artifact@v3
        with:
          name: version
          path: version.txt

  build-api:
    runs-on: [runner]
    needs: prepare
    container: git.sh-edraft.de/sh-edraft.de/act-runner:latest
    steps:
      - name: Clone Repository
        uses: https://github.com/actions/checkout@v3
        with:
          token: ${{ secrets.CI_ACCESS_TOKEN }}

      - name: Download build version artifact
        uses: actions/download-artifact@v3
        with:
          name: version
          path: version.txt

      - name: Build docker
        run: |
          cd api
          echo "VERSION = \"$(cat version.txt)\"" > version.py
          docker build --no-cache -t git.sh-edraft.de/sh-edraft.de/open-redirect-api:$(cat ../version.txt) .

      - name: Login to registry git.sh-edraft.de
        uses: https://github.com/docker/login-action@v1
        with:
          registry: git.sh-edraft.de
          username: ${{ secrets.CI_USERNAME }}
          password: ${{ secrets.CI_ACCESS_TOKEN }}

      - name: Push image
        run: |
          docker push git.sh-edraft.de/sh-edraft.de/open-redirect-api:$(cat version.txt)

  build-redirector:
    runs-on: [runner]
    needs: prepare
    container: git.sh-edraft.de/sh-edraft.de/act-runner:latest
    steps:
      - name: Clone Repository
        uses: https://github.com/actions/checkout@v3
        with:
          token: ${{ secrets.CI_ACCESS_TOKEN }}

      - name: Download build version artifact
        uses: actions/download-artifact@v3
        with:
          name: version
          path: version.txt

      - name: Build docker
        run: |
          cd api
          docker build --no-cache -f dockerfile_redirector -t git.sh-edraft.de/sh-edraft.de/open-redirect-redirector:$(cat ../version.txt) .

      - name: Login to registry git.sh-edraft.de
        uses: https://github.com/docker/login-action@v1
        with:
          registry: git.sh-edraft.de
          username: ${{ secrets.CI_USERNAME }}
          password: ${{ secrets.CI_ACCESS_TOKEN }}

      - name: Push image
        run: |
          docker push git.sh-edraft.de/sh-edraft.de/open-redirect-redirector:$(cat version.txt)

  build-web:
    runs-on: [runner]
    needs: prepare
    container: git.sh-edraft.de/sh-edraft.de/act-runner:latest
    steps:
      - name: Clone Repository
        uses: https://github.com/actions/checkout@v3
        with:
          token: ${{ secrets.CI_ACCESS_TOKEN }}

      - name: Download build version artifact
        uses: actions/download-artifact@v3
        with:
          name: version
          path: version.txt

      - name: Prepare web build
        run: |
          cd web
          npm ci

      - name: Build web
        run: |
          cd web
          npm run build

      - name: Build docker
        run: |
          cd web
          docker build --no-cache --build-arg VERSION=$(cat version.txt) -t git.sh-edraft.de/sh-edraft.de/open-redirect-web:$(cat ../version.txt) .

      - name: Login to registry git.sh-edraft.de
        uses: https://github.com/docker/login-action@v1
        with:
          registry: git.sh-edraft.de
          username: ${{ secrets.CI_USERNAME }}
          password: ${{ secrets.CI_ACCESS_TOKEN }}

      - name: Push image
        run: |
          docker push git.sh-edraft.de/sh-edraft.de/open-redirect-web:$(cat version.txt)
