Home > database >  YAML, cmd based shell, if else conditions is not working
YAML, cmd based shell, if else conditions is not working

Time:01-10

I am writing CICD in the YAML file of GitHub, therein one instance I need to compare the branch names where a pull request is being triggered to determine which environment the other underlying process thereafter should use to the do MsBuild and other tasks.

Below is the comparison in YAML:

 - name: build
   shell: cmd
   run: |
        
        if not x%GITHUB_REF_NAME:RSDEV_=%==x%GITHUB_REF_NAME% (
            SET PLATFORM=Qa
           ) else if not x%GITHUB_REF_NAME:Feature_=%==x%GITHUB_REF_NAME% (
            SET PLATFORM=Qa
          ) else if not x%GITHUB_REF_NAME:Release_=%==x%GITHUB_REF_NAME%(
            SET PLATFORM=Uat
          ) else (
            echo "Check something went wrong"
            exit 1
          )

However, when I ran pull-request, it says

The syntax of the command is incorrect

Am I following the correct way for branch comparison like release_** and so on cmd?

CodePudding user response:

Btw, I was able to figure out the reason behind the error: The syntax of the command is incorrect

There was an indentation error in the else block:

- name: build
   shell: cmd
   run: |
        
        if not x%GITHUB_REF_NAME:RSDEV_=%==x%GITHUB_REF_NAME% (
            SET PLATFORM=Qa
        ) else if not x%GITHUB_REF_NAME:Feature_=%==x%GITHUB_REF_NAME% (
            SET PLATFORM=Qa
        ) else if not x%GITHUB_REF_NAME:Release_=%==x%GITHUB_REF_NAME%(
            SET PLATFORM=Uat
        ) else (
            echo "Check something went wrong"
            exit 1
        )

[Courtesy Edit]

The correct syntax in cmd.exe:

        if /i not "%GITHUB_REF_NAME:RSDEV_=%" == "%GITHUB_REF_NAME%" (
            SET "PLATFORM=Qa"
        ) else if /i not "%GITHUB_REF_NAME:Feature_=%" == "%GITHUB_REF_NAME%" (
            SET "PLATFORM=Qa"
        ) else if /i not "%GITHUB_REF_NAME:Release_=%" == "%GITHUB_REF_NAME%" (
            SET "PLATFORM=Uat"
        ) else (
            echo Check something went wrong
            exit 1
        )
  • Related