gitlabgitlab-ci

GitLab CI "if invalid expression syntax" when using CI/CD inputs


I'm working on an GitLab pipeline that should accept an input (multiple in the future, but for now it's just one) before running jobs manually.

I'm trying to run the jobs only if the "rules: if:" conditions match - but on the if clause, I'm getting this validation error: This GitLab CI configuration is invalid: jobs:deploy-demo-api-a:rules:rule if invalid expression syntax. I'm struggle to find what's different from this to other projects, enclosing the if syntax in 'imagine if clause here' doesn't help.

And (not as important): I cannot leave the default for input "demo-api-a-release" empty, because I'm getting This GitLab CI configuration is invalid: jobs:deploy-demo-api-a:needs:need ref can't be blank. as a validation error. My workaround is to use empty in this case, but is there a way that I can use an empty string here?

# Handles deploying apps to Azure Web App
spec:
  inputs:
    demo-api-a-release:
      type: string
      default: 'empty'
      description: "Release tag or version for demo-api-a. Leave empty for no release."
---
stages:
  - deploy

variables:
  AZURE_CLIENT_ID: $AZURE_CLIENT_ID
  AZURE_TENANT_ID: $AZURE_TENANT_ID
  AZURE_CLIENT_SECRET: $AZURE_CLIENT_SECRET
  AZURE_SUBSCRIPTION_ID: $AZURE_SUBSCRIPTION_ID

# Handles the actual deployment to Azure
# Only run when input "demo-api-a-release" is not null or empty
deploy-demo-api-a:
  stage: deploy
  image: mcr.microsoft.com/azure-cli
  rules:
    - if: $[[ inputs.demo-api-a-release ]] === "empty" || $[[ inputs.demo-api-a-release ]] == ""
      when: never
      allow_failure: true
    - when: manual
  needs:  # Get the artifact for the specified DEMO_API_A_RELEASE
    - project: demo-group/demo-child-a
      job: create-artifact
      ref: $[[ inputs.demo-api-a-release ]]
      artifacts: true
  script:
    - az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET --tenant $AZURE_TENANT_ID
    - az account set --subscription $AZURE_SUBSCRIPTION_ID
    - az webapp deploy --resource-group "gitlab-poc" --name "demo-api-a" --src-path build.zip

Solution

  • Any occurence of $[[ inputs.demo-api-a-release ]] will be directly replaced with the value of the spec input. In your example this fragment:

        - if: $[[ inputs.demo-api-a-release ]] == "empty"
    

    Would produce:

        - if: empty == "empty"
    

    Since the first empty is unquoted, it results in invalid configuration. Instead it'll have to look something like this:

        - if: '"$[[ inputs.demo-api-a-release ]]" == "empty"'