stages:
  - test
  - release
  - build

workflow:
  rules:
    - if: '$CI_MERGE_REQUEST_EVENT_TYPE == "detached"'
      when: never
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
    - when: always

services:
  - name: ghcr.io/griefed/gitlab-ci-cd:2.0.9
    alias: docker

image: ghcr.io/griefed/gitlab-ci-cd:2.0.9

test docker:
  stage: test
  image: griefed/gitlab-ci-cd:2.2.1
  services:
    - name: docker:dind
      alias: docker
  variables:
    project_name: $CI_PROJECT_NAME
    SEMANTIC_RELEASE_PACKAGE: $CI_PROJECT_NAME
  before_script:
    - docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker login -u "$DOCKERHUB_USER" -p "$GITHUB_TOKEN" ghcr.io
    - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
    - docker buildx create --use --name grfdbuilder
  script:
    - docker buildx build
      --push
      --no-cache
      --platform linux/amd64,linux/arm64
      --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:port-develop-$CI_COMMIT_SHORT_SHA"
      --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:port-develop-$CI_COMMIT_SHORT_SHA"
      --file Dockerfile.port .
    - docker buildx build
      --push
      --no-cache
      --platform linux/amd64,linux/arm64
      --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:proxy-develop-$CI_COMMIT_SHORT_SHA"
      --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:proxy-develop-$CI_COMMIT_SHORT_SHA"
      --file Dockerfile.proxy .
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_TITLE =~ /^RELEASE:.+$/

release:
  needs: ['test docker']
  stage: release
  image: griefed/gitlab-ci-cd:2.2.1
  services:
    - name: docker:dind
      alias: docker
  variables:
    project_name: $CI_PROJECT_NAME
    SEMANTIC_RELEASE_PACKAGE: $CI_PROJECT_NAME
  script:
    - npx semantic-release
  only:
    - master
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_TITLE =~ /^RELEASE:.+$/

build:
  stage: build
  image: griefed/gitlab-ci-cd:2.2.1
  services:
    - name: docker:dind
      alias: docker
  variables:
    project_name: $CI_PROJECT_NAME
    SEMANTIC_RELEASE_PACKAGE: $CI_PROJECT_NAME
  before_script:
    - docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker login -u "$DOCKERHUB_USER" -p "$GITHUB_TOKEN" ghcr.io
    - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
    - docker buildx create --use --name grfdbuilder
  script:
    - docker buildx build
      --push
      --no-cache
      --platform linux/amd64,linux/arm64
      --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:port"
      --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:port"
      --file Dockerfile.port .
    - docker buildx build
      --push
      --no-cache
      --platform linux/amd64,linux/arm64
      --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:proxy"
      --tag "ghcr.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:proxy"
      --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:latest"
      --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:latest"
      --file Dockerfile.proxy .
  only:
    - tags

Check Packages:on-schedule:
  only:
    - schedules
  before_script:
    - |-
      echo "Preparing package versions comparison."
      # Check and, if necessary, update git user and mail
      if [[ "$(git config --list | grep user.name)" != "user.name=$GIT_USER" ]];then
        git config --global user.name $GIT_USER
      fi
      if [[ "$(git config --list | grep user.email)" != "user.email=$GIT_MAIL" ]];then
        git config --global user.email $GIT_MAIL
      fi

      # Clean system of potentially interrupting images
      docker image rm -f $DOCKERHUB_USER/$DOCKERHUB_REPO:latest
      docker image rm -f $DOCKERHUB_REPO
      rm -rf /tmp/$CI_PROJECT_PATH
      mkdir -p /tmp/$CI_PROJECT_PATH
      echo "Preparations complete."
  script:
    - |-
      echo "Comparing package versions." && \
      # Clone the repository
      git clone $CI_PROJECT_URL.git /tmp/$CI_PROJECT_PATH && \
      cd /tmp/$CI_PROJECT_PATH && \

      if [ ! -s "package_versions.txt" ];then
        echo "No package_versions.txt available..." && \

        # Gather package information from latest build
        docker run --rm --entrypoint /bin/sh -v /tmp/$CI_PROJECT_PATH:/tmp $DOCKERHUB_USER/$DOCKERHUB_REPO:latest -c '\
          apk info -v > /tmp/package_versions.txt && \
          sort -o /tmp/package_versions.txt  /tmp/package_versions.txt && \
          chmod 777 /tmp/package_versions.txt' && \

        # Checkout our branch
        git checkout -f $CI_DEFAULT_BRANCH && \

        wait && \

        # Add and commit new file to repository
        git add package_versions.txt && \
        git commit -m 'chore: Add list of package versions.' && \

        # Push the changes to the remote
        git push "https://$GIT_USER:$GITLAB_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git" --all && \

        # Nice
        echo "package_versions.txt added."

      elif [ -s "package_versions.txt" ];then
        echo "Local package_versions.txt available..." && \

        # Build local image for new package versions list
        docker build --no-cache --tag $DOCKERHUB_REPO . && \

        # Get packages from newly build local image
        docker run --rm --entrypoint /bin/sh -v /tmp/$CI_PROJECT_PATH:/tmp $DOCKERHUB_REPO -c '\
          apk info -v > /tmp/package_versions_new.txt && \
          sort -o /tmp/package_versions_new.txt  /tmp/package_versions_new.txt && \
          chmod 777 /tmp/package_versions_new.txt' && \

        # Get checksum of old packages
        OLD_CHECKSUM=$(md5sum package_versions.txt | cut -f1 -d" ") && \

        # Get checksum of new packages
        NEW_CHECKSUM=$(md5sum package_versions_new.txt | cut -f1 -d" ")



        # If new checksum is not the same as old checksum, we have new versions
        if [ "${OLD_CHECKSUM}" != "${NEW_CHECKSUM}" ]; then

          echo "Checksums differ. Updating..." && \

          # Checkout our branch
          git checkout -f $CI_DEFAULT_BRANCH && \

          # Copy the new package versions list to repository
          mv -f package_versions_new.txt package_versions.txt && \

          wait && \

          # Add and commit new file to repository
          git add package_versions.txt && \
          git commit -m 'build: Update installed packages in Docker container.' && \

          # Push the changes to the remote
          git push "https://$GIT_USER:$GITLAB_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git" --all && \

          # Nice
          echo "Packages updated."
        else
          echo "No package updates available."
        fi

      fi
      echo "Comparison complete."
  after_script:
    - |-
      echo "Cleaning up."
      docker image rm -f $DOCKERHUB_USER/$DOCKERHUB_REPO:latest
      docker image rm -f $DOCKERHUB_REPO
      rm -rf /tmp/$CI_PROJECT_PATH
      echo "Done."
