Search code examples
regexbashgitlab-cigitlab-ci.yml

GitLab issue with porting bash script to CI Job


I have a bash script which I can use to test a tag against regex.

Attempted to port it as a Gitlab-CI job, but all tags are being recognized as invalid.

The bash script below works as expected for all cases

#!/bin/bash

VERSION_REGEX="^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)((-(alpha|beta|rc)\.(0|[1-9][0-9]*))|((-(0|[1-9][0-9]*))?-[0-9]{14}-[[:xdigit:]]{12}))?$"

# Iterate over the array and echo each version (array in examples below)
for CI_COMMIT_TAG in "${versions[@]}"; do
    if [[ "$CI_COMMIT_TAG" =~ $VERSION_REGEX ]]; then 
        echo "$CI_COMMIT_TAG"
    else 
        echo "Invalid tag format: $CI_COMMIT_TAG"
    fi
done

The following my attempt to port it as a Gitlab CI Job:

stages:
  - validate

variables:
  VERSION_REGEX: >
    ^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)((-(alpha|beta|rc)\.(0|[1-9][0-9]*))|((-(0|[1-9][0-9]*))?-[0-9]{14}-[[:xdigit:]]{12}))?$

validate_tag:
  stage: validate
  rules:
    - if: "$CI_COMMIT_TAG" # Run this job when a tag is created manually or added through the UI at Code > Tags
  script:
    - echo $VERSION_REGEX
    - |
      if [[ "$CI_COMMIT_TAG" =~ $VERSION_REGEX ]]; then 
        echo "Valid tag format: $CI_COMMIT_TAG"
      else 
        echo "Invalid tag format: $CI_COMMIT_TAG"
        exit 1
      fi

Versions tested against: (feel free to try here: https://onlinegdb.com/dJqjG-jj1)

# Declare the array with version strings
versions=(
    # Valid
    "v0.0.0"
    "v1.2.3"
    "v11.22.33"
    "v0.1.6-alpha.0"
    "v0.1.2-alpha.0"
    "v0.1.2-alpha.3"
    "v1.2.3-beta.45"
    "v23.34.45-rc.567"
    "v0.0.0-20170915032832-14c0d48ead0c"
    "v0.0.0-5-20170915032832-14c0d48ead0c"
    # Invalid
    "0"
    "V0"
    "v1"
    "v000"
    "v1.9"
    "v.1.9"
    "v1.90"
    "v1.090"
    "v00.00.00"
    "v01.02.03"
    "v1.9-alpha.2"
    "v0.1.2-alpha.03"
    "v1.2.3-beta-hardcoded.4"
    "v0.0.0-5-20170915032832-14c0d48eaxyz"
    "vX.Y.Z-C-20170915032832-14c0d48ead0c"
    "v0.0.0-5-20170915032832-14c0d48ead0c-modified"
)

Examples from the output of using the Gitlab Runner:

  • Expected Valid
$ echo $VERSION_REGEX
^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)((-(alpha|beta|rc)\.(0|[1-9][0-9]*))|((-(0|[1-9][0-9]*))?-[0-9]{14}-[[:xdigit:]]{12}))?$
$ if [[ "$CI_COMMIT_TAG" =~ $VERSION_REGEX ]]; then  # collapsed multi-line command
Invalid tag format: v0.1.6-alpha.0
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: exit code 1
  • Expected Invalid
$ echo $VERSION_REGEX
^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)((-(alpha|beta|rc)\.(0|[1-9][0-9]*))|((-(0|[1-9][0-9]*))?-[0-9]{14}-[[:xdigit:]]{12}))?$
$ if [[ "$CI_COMMIT_TAG" =~ $VERSION_REGEX ]]; then  # collapsed multi-line command
Invalid tag format: v0.1.6-alpha.invalid
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: exit code 1

Hate that the title is vague, willing to change it; also let me know what else I can provide to make it easier to identify the issue...


Solution

  • Resolved this by using grep instead to perform the check. Could not figure out how to resolve the native bash check, but for my use case, using coreutils is acceptable.

    script:
        - |
          result=$(echo "$CI_COMMIT_TAG" | grep -oE "$VERSION_REGEX")
          if [ -n "$result" ]; then
            echo "Valid tag format: $CI_COMMIT_TAG"
          else 
            echo "Invalid tag format: $CI_COMMIT_TAG"
            exit 1
          fi