---
# --------------------------------------------------------------------------------------
# Global
# --------------------------------------------------------------------------------------
include:
  - project: '${CI_PROJECT_ROOT_NAMESPACE}/yocto/infrastructure/gitlab-ci'
    ref: ${GITLAB_CI_REVISION}
    file:
      - build-pipeline.yml
      - common.yml

stages:
  - Build
  - Test

# --------------------------------------------------------------------------------------
# Stage: Build
# --------------------------------------------------------------------------------------
.build: &build
  - cd ${CI_PROJECT_DIR}
  - VERSION=$(cd .repo/manifests && git describe --tags)
  - cat .repo/manifests/default.xml
  - find foo -name "[0-9]*.txt" -printf '%P\n' | sort -V > files-foo-$VERSION.txt
  - cat files-foo-$VERSION.txt
  - find bar -name "[0-9]*.txt" -printf '%P\n' | sort -V > files-bar-$VERSION.txt
  - cat files-bar-$VERSION.txt
  - FOO_FILES=$(cat files-foo-$VERSION.txt | wc -l)
  - BAR_FILES=$(cat files-bar-$VERSION.txt | wc -l)
  - DIFF=$((BAR_FILES-FOO_FILES))
  - (($DIFF >= -1 && $DIFF <= 1))

build:files:
  stage: Build
  extends:
    - .buildbase
  tags:
    - infrastructure
  needs: []
  timeout: 2m
  variables:
    GIT_STRATEGY: none
    LOG_PREFIX: "CI:build:"
  before_script:
    - !reference [.docker_check]
    - !reference [.setup_ssh]
    - !reference [.repo_checkout]
  script:
    - *build
  artifacts:
    paths:
      - files-*

build:echo:
  stage: Build
  tags:
    - infrastructure
  needs: []
  timeout: 2m
  image: ${CI_IMAGE_PYTHON}
  script:
    - printenv
    - echo "Build successful"

build:check-foo-branch:
  stage: Build
  extends:
    - .buildbase
  tags:
    - infrastructure
  needs: []
  timeout: 2m
  variables:
    GIT_STRATEGY: none
  before_script:
    - !reference [.docker_check]
    - !reference [.setup_ssh]
    - !reference [.repo_checkout]
  script: |
    echo "repo branch: $MASTER_BRANCH"
    echo "foo branch:  $(cat foo/branch.txt)"
    if [[ "$MASTER_BRANCH" != $(cat foo/branch.txt) ]]; then
      echo "ERROR: Branches do not match!"
      exit 1
    fi
  artifacts: null
  cache: null

# --------------------------------------------------------------------------------------
# Stage: Test
# --------------------------------------------------------------------------------------
.test_simple:
  stage: Test
  rules:
    - when: manual
      allow_failure: true
  tags:
    - infrastructure
  image: ${CI_IMAGE_PYTHON}
  script:
    - exit ${RETURNCODE}

test:pass:
  extends:
    - .test_simple
  variables:
    RETURNCODE: 0

test:fail:
  extends:
    - .test_simple
  variables:
    RETURNCODE: 1
