'AWS buildspec adding condition based commands throws error

I'm trying to add some conditional statements with aws buildspec file to my .NET Framework application.

Using Windows image server to build the application

I tried many methods but it stills throws an error.

phases: 
  pre_build:
    commands:
     #- .\build-env.bat
     - |
        IF [ "$BUILD_ENV" = "DEV" ] (
            echo $BUILD_ENV
        )

I'm getting below error message when I run the pipeline.

[Container] 2022/02/20 03:52:25 Running command IF [ "$BUILD_ENV" = "DEV" ] ( echo $BUILD_ENV )

At C:\codebuild\output\tmp\script.ps1:5 char:3

  • IF [ "$BUILD_ENV" = "DEV" ] (
  • ~ Missing '(' after 'IF' in if statement. At C:\codebuild\output\tmp\script.ps1:5 char:5
  • IF [ "$BUILD_ENV" = "DEV" ] (
  • ~
    

Missing type name after '['.

  • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx ception
  • FullyQualifiedErrorId : MissingOpenParenthesisInIfStatement

[Container] 2022/02/20 03:52:25 Command did not exit successfully IF > [ "$BUILD_ENV" = "DEV" ] ( echo $BUILD_ENV ) exit status 1 [Container] 2022/02/20 03:52:25 Phase complete: PRE_BUILD State: FAILED [Container] 2022/02/20 03:52:25 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: IF [ > "$BUILD_ENV" = "DEV" ] ( echo $BUILD_ENV ) . Reason: exit status 1

Please advice what I did wrong here.

Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source