Search code examples
bashdockershellgitlab-ci

Gitlab docker image script - if condition is not working


Written a below gitlab pipeline using python image and if condition works perfectly fine.

.chk-bracket:
  image: "python:3.7"
  before_script:
    - apt update
    - apt install jq -y 
    - pip install awscli
    
  script:
    - echo $CI_COMMIT_MESSAGE
    - |
      if [[ $CI_COMMIT_MESSAGE = *"_test"* ]]; then
        echo "testing"
      fi
  rules:
    - if: $CI_COMMIT_BRANCH =~ /^develop/ && $CI_COMMIT_MESSAGE =~ /.*build_test.*/

Later we refactored to use docker image instead of python image and the if condition started failing, it's not printing testing. Of course I simplified with the simple if condition in the original complex gitlab pipeline code

.chk-bracket:
  image: "docker:stable"
  before_script:
    - apk update
    - apk add py-pip jq bash
    - pip install awscli
  script:
    - echo $CI_COMMIT_MESSAGE
    - |
      if [[ $CI_COMMIT_MESSAGE = *"_test"* ]]; then
        echo "testing"
      fi
  rules:
    - if: $CI_COMMIT_BRANCH =~ /^develop/ && $CI_COMMIT_MESSAGE =~ /.*build_test.*/

Any clues to why this condition is not working in docker image?


Solution

  • This is because python:3.7 uses debian as its base image and docker:stable uses alpine as its base image, Alpine comes with ash as the default shell instead of bash. I am using below statements inside docker image for substring match.

            if [ -z "${CI_COMMIT_TAG##*"dev"*}" ]; then
              echo "Building dev Image..."
              docker build --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG . --build-arg VERSION=$CI_COMMIT_TAG
            fi