# -*- coding: utf-8 -*-
# vim: ft=yaml
---
include:
  - local: templates/Rules.yaml
  - local: templates/Runners/eole-docker.yaml
  - local: templates/Git.yaml
  - local: templates/Docker.yaml
  - local: templates/Semantic-release.yaml

stages:
  - lint
  - build
  - release

.docker-matrix:
  parallel:
    matrix:
      - IMAGE_NAME:
          - commitlint
          - helm
          - semantic-release-gitlab

###############################################################################
# `lint` stage: `commitlint`
###############################################################################
commitlint: {extends: '.git:commitlint'}

###############################################################################
# `build` stage: `*-docker-build`
###############################################################################
# Suffix all jobs to avoid conflict with other jobs names
build-docker:
  extends:
    - .docker-matrix
    - .docker:image:build

###############################################################################
# `release` stage: `new-release`, `tag *`
###############################################################################
# Create the release versions on `$STABLE_BRANCH`
new release: {extends: '.semantic-release:stable'}

# Suffix all jobs to avoid conflict with other jobs names
.tag-docker:
  extends:
    - .docker-matrix
    - .docker:image:tag

## tag contribution branches with a more stable name than `git-${CI_COMMIT_SHORT_SHA}`
tag contrib branch:
  extends:
    - .tag-docker
    - .on-branches
  variables:
    # `feature/foo-bar_quux` → `feature-foo-bar-quux`
    IMAGE_TAG: $CI_COMMIT_REF_SLUG

## dev images
tag dev:
  extends:
    - .tag-docker
    - .on-dev
  variables:
    IMAGE_TAG: dev

## testing images
tag testing:
  extends:
    - .tag-docker
    - .on-testing
  variables:
    IMAGE_TAG: testing

## stable images
.tag-stable:
  extends: .tag-docker
  variables:
    SOURCE_TAG: testing

tag release:
  extends: .tag-stable

tag major:
  extends: .tag-stable
  before_script:
    - export RELEASE_PREFIX=${RELEASE_PREFIX:-release/}
    - export RELEASE=${CI_COMMIT_TAG#${RELEASE_PREFIX}}
    - export IMAGE_TAG=${RELEASE%%.*}

tag minor:
  extends:
    - .tag-stable
  before_script:
    - export RELEASE_PREFIX=${RELEASE_PREFIX:-release/}
    - export RELEASE=${CI_COMMIT_TAG#${RELEASE_PREFIX}}
    - export IMAGE_TAG=${RELEASE%.${RELEASE##*.}}

tag stable:
  extends: .tag-stable
  variables:
    IMAGE_TAG: stable

tag latest:
  extends: .tag-stable
  variables:
    IMAGE_TAG: latest