Home > front end >  Gitlab: How to check if a string is one of three values
Gitlab: How to check if a string is one of three values

Time:10-22

We'd like to ensue that a user enters a value from a list of valid options. Since we found no way to define a preset of valid values, we are currently trying do it with an if statement.

However we have trouble finding out the if syntax for a OR operator.

What we currently have:

variables:
    BUMP_TYPE: 'no-release-just-build'
...
  before_script:
      - >
        if [[ "$BUMP_TYPE" = "no-release-just-build"] || ["$BUMP_TYPE" = "patch" ]]; then
          echo "valid $BUMP_TYPE"
        else
          echo "invalid $BUMP_TYPE"
        fi

However this outputs: "invalid no-release-just-build" --> the else case

Why is "$VERSION" = "no-release-just-build" not true?

CodePudding user response:

A little Different approach, what I like to do - instead of bash scripting within the script - is a job in the pre stage based on rules like

Param-check:
  stage: .pre
  script:
    - echo "invalid version:$VERSION - please use ..."
    - exit 1
  rules:
    - if: $VERSION == "value1"
      when: never
    - if: '$VERSION == "value1" || $VERSION == "value2"'
      when: never
    - when: always

This way I keep my script and before blocks clean of validations

There is also the possibility to use regex for the rules, but I am on my mobile, and therefore writing regex is ultra hard - i also could not validate my script. But I think I show the general idea

  • Related