Skip to content
Snippets Groups Projects
.gitlab-ci.yml 6.32 KiB
Newer Older
Griefed's avatar
Griefed committed
  - test
Griefed's avatar
Griefed committed
  - release
  - build

services:
  - name: docker.griefed.dev/griefed/gitlab-ci-cd:2.2.12
Griefed's avatar
Griefed committed
    alias: docker
image: docker.griefed.dev/griefed/gitlab-ci-cd:2.2.12
Griefed's avatar
Griefed committed
workflow:
  rules:
    - if: '$CI_MERGE_REQUEST_EVENT_TYPE == "detached"'
      when: never
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
    - when: always

test quasar:
  stage: test
  cache:
    paths:
      - node_modules/
  before_script:
    - npm install
    - rm -Rf dist
  script:
    - quasar build

Griefed's avatar
Griefed committed
test docker:
  stage: test
  variables:
    project_name: $CI_PROJECT_NAME
    SEMANTIC_RELEASE_PACKAGE: $CI_PROJECT_NAME
  before_script:
    - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
    - docker buildx create --use --name grfdbuilder
  script:
    - docker buildx build --no-cache --platform linux/amd64,linux/arm/v7,linux/arm64
      --build-arg BRANCH_OR_TAG=$CI_COMMIT_REF_NAME
      --build-arg HOSTER=$CI_SERVER_HOST
      --file Dockerfile .
  rules:
    - if: '$CI_SERVER_HOST == "git.griefed.de"'

release:
  needs: ['test docker','test quasar']
  stage: release
  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
  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 "$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/arm/v7,linux/arm64
      --build-arg BRANCH_OR_TAG=$CI_COMMIT_REF_NAME
      --build-arg HOSTER=$CI_SERVER_HOST
      --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_TAG"
      --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:latest"
      --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_TAG"
      --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:latest"
      --file Dockerfile .
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Griefed's avatar
Griefed committed
  stage: docs
  cache:
    paths:
      - node_modules/
  before_script:
    - npm install
    - rm -Rf dist
  script:
    - quasar build
    - cp -Rf dist/spa/* public/
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
Griefed's avatar
Griefed committed
  artifacts:
    paths:
      - public
Griefed's avatar
Griefed committed
    expire_in: 1 week

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."