9

I want the some of the variables should be required before executing the pipeline in gitlab. Can I achieve it. I have below gitlab-ci.yml

stages:          # Runs First - Anything that needs to run prior to your scripts running
  - deploy
  - Script

variables:
  Domain:
    value: "dom"
  HOST_NAME:
    value: ""  
  JAVA_FILES_WITH_ARGS:
    value: ""
  

I want to make mandatory(JAVA_FILES_WITH_ARGS, HOST_NAME) before run the pipeline how can I achieve it. it will not start the pipeline without these two variable values.

2
  • 1
    stackoverflow.com/questions/69674002/… might be the right approach Commented Oct 27, 2021 at 8:00
  • Simon how to give custom error mesage instead "No stages and Pipeline" when it allow not to proceed Commented Oct 27, 2021 at 9:46

2 Answers 2

4

The Answer: is

rules:
 - if: '$JAVA_FILES_WITH_ARGS != "" && $HOST_NAME != ""'
   allow_failure: true
Sign up to request clarification or add additional context in comments.

2 Comments

@Kappacake i've made the suggested edits
1

I usually prefer to do input validation in a before_script. If you use rules in a job, the job won't be shown in the GUI when a rule evaluates to false. Using a before_script offers a more flexible approach, because you can do much more than checking for empty strings.

stages:
  - deploy
  - Script

variables:
  DOMAIN: "dom"
  HOST_NAME: ""
  JAVA_FILES_WITH_ARGS: ""
  FILE_PATH: "/tmp/file.txt"
  TXT_RED: "\e[91m"
  TXT_CLEAR: "\e[0m"

.validate-input: &validate-input
  - |
    # Pipeline input validation
    if [ -z "${HOST_NAME}" ]; then
      echo -e "${TXT_RED}HOST_NAME cannot be empty${TXT_CLEAR}"
      exit 1
    fi

    if [ -z "${JAVA_FILES_WITH_ARGS}" ]; then
      echo -e "${TXT_RED}JAVA_FILES_WITH_ARGS cannot be empty${TXT_CLEAR}"
      exit 1
    fi

    if [[ ! -f "${FILE_PATH}" ]]; then
      echo -e "${TXT_RED}FILE_PATH file not found${TXT_CLEAR}"
    fi

my_job:
  allow_failure: false
  before_script:
    - *validate-input
  script:
    echo -e "Hello World!"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.