kind: pipeline
type: kubernetes
name: Container & Helm chart

trigger:
  event:
    exclude:
    - tag

steps:
- name: Semantic Release (Dry-run)
  image: bv11-cr01.bessems.eu/proxy/library/node:20-slim
  pull: always
  commands:
  - |
    apt-get update
  - |
    apt-get install -y --no-install-recommends \
      git-core \
      ca-certificates
  - |
    npm install \
      semantic-release \
      @semantic-release/commit-analyzer \
      @semantic-release/exec \
  - |
    export GIT_CREDENTIALS=$${GIT_USERNAME}:$${GIT_APIKEY}
  - |
    npx semantic-release \
      --package @semantic-release/exec \
      --package semantic-release \
      --branches ${DRONE_BRANCH} \
      --tag-format "v\$${version}" \
      --dry-run \
      --plugins @semantic-release/commit-analyzer,@semantic-release/exec \
      --analyzeCommits @semantic-release/commit-analyzer \
      --verifyRelease @semantic-release/exec \
      --verifyReleaseCmd 'echo "$${nextRelease.version}" > .version'
  environment:
    GIT_APIKEY:
      from_secret: git_apikey
    GIT_USERNAME: djpbessems

- name: Container image (Harbor)
  depends_on:
  - Semantic Release (Dry-run)
  image: gcr.io/kaniko-project/executor:debug
  pull: always
  commands:
  - |
    export IMAGE_TAG=$(cat .version 2> /dev/null) && [ -n "$IMAGE_TAG" ] || (echo 'No release tag - exiting'; exit 0)
  - |
    wget https://github.com/jqlang/jq/releases/download/jq-1.6/jq-linux64 -qO /bin/jq
    chmod +x /bin/jq
  - |
    /bin/jq '.auths["https://bv11-cr01.bessems.eu/v2/"].auth="'"$(echo -n $HARBOR_USERNAME':'$HARBOR_PASSWORD | base64)"'"' --null-input > /kaniko/.docker/config.json
  - |
    /kaniko/executor \
      --dockerfile=Dockerfile \
      --context=dir://. \
      --destination=bv11-cr01.bessems.eu/library/json-server:latest \
      --destination=bv11-cr01.bessems.eu/library/json-server:$IMAGE_TAG
  environment:
    HARBOR_USERNAME:
      from_secret: harbor_username
    HARBOR_PASSWORD:
      from_secret: harbor_password

- name: Container image (docker.io)
  depends_on:
  - Semantic Release (Dry-run)
  image: gcr.io/kaniko-project/executor:debug
  pull: always
  commands:
  - |
    export IMAGE_TAG=$(cat .version 2> /dev/null) && [ -n "$IMAGE_TAG" ] || (echo 'No release tag - exiting'; exit 0)
  - |
    wget https://github.com/jqlang/jq/releases/download/jq-1.6/jq-linux64 -qO /bin/jq
    chmod +x /bin/jq
  - |
    /bin/jq '.auths["https://index.docker.io/v1/"].auth="'"$(echo -n $DOCKER_USERNAME':'$DOCKER_PASSWORD | base64)"'"' --null-input > /kaniko/.docker/config.json
  - |
    /kaniko/executor \
      --dockerfile=Dockerfile \
      --context=dir://. \
      --destination=docker.io/djpbessems/json-server:latest \
      --destination=docker.io/djpbessems/json-server:$IMAGE_TAG
  environment:
    DOCKER_USERNAME:
      from_secret: docker_username
    DOCKER_PASSWORD:
      from_secret: docker_password

- name: Helm chart (Gitea)
  depends_on:
  - Semantic Release (Dry-run)
  image: bv11-cr01.bessems.eu/proxy/alpine/helm
  pull: always
  commands:
  - |
    export CHART_VERSION=$(cat .version 2> /dev/null) && [ -n "$CHART_VERSION" ] || (echo 'No release tag - exiting'; exit 0)
    sed -i 's/{{ chart_version }}/'$CHART_VERSION'/g' charts/json-server/Chart.yaml
    sed -i 's/tag: latest/tag: '$CHART_VERSION'/g' charts/json-server/values.yaml
  - |
    helm package ./charts/json-server
  - |
    helm plugin install https://github.com/chartmuseum/helm-push
  - |
    helm repo add \
      --username $GIT_USERNAME \
      --password $GIT_APIKEY \
      spamasaurus \
      https://code.spamasaurus.com/api/packages/$GIT_USERNAME/helm
  - |
    helm cm-push \
      json-server-*.tgz \
      spamasaurus
  environment:
    GIT_APIKEY:
      from_secret: git_apikey
    GIT_USERNAME: djpbessems

- name: Semantic Release
  depends_on:
  - Container image (Harbor)
  - Container image (docker.io)
  - Helm chart (Gitea)
  image: bv11-cr01.bessems.eu/proxy/library/node:20-slim
  pull: always
  commands:
  - |
    export VALID_TAG=$(cat .version 2> /dev/null) && [ -n "$VALID_TAG" ] || (echo 'No release tag - exiting'; exit 0)
  - |
    apt-get update
  - |
    apt-get install -y --no-install-recommends \
      git-core \
      ca-certificates
  - |
    npm install \
      semantic-release \
      @semantic-release/changelog \
      @semantic-release/commit-analyzer \
      @semantic-release/git \
      @semantic-release/release-notes-generator
  - |
    export GIT_CREDENTIALS=$${GIT_USERNAME}:$${GIT_APIKEY}
  - |
    npx semantic-release \
      --branches ${DRONE_BRANCH} \
      --tag-format "v\$${version}" \
      --plugins @semantic-release/commit-analyzer,@semantic-release/release-notes-generator,@semantic-release/changelog,@semantic-release/git
  environment:
    GIT_APIKEY:
      from_secret: git_apikey
    GIT_USERNAME: djpbessems