Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional statements in buildspec.yml #2

Closed
c2xbrhdj6u4qs15 opened this issue Jan 10, 2018 · 5 comments
Closed

Conditional statements in buildspec.yml #2

c2xbrhdj6u4qs15 opened this issue Jan 10, 2018 · 5 comments

Comments

@c2xbrhdj6u4qs15
Copy link

Hi,

Would somebody be able to provide an example on how to use Conditional statements such as "if else statements" in a buildspec.yml.

Thanks in advance

@ktwbc
Copy link

ktwbc commented May 10, 2018

   build:
     commands:
       - echo "${CODEBUILD_BUILD_ARN}"
       - |
         if expr "${CODEBUILD_BUILD_ARN}" : ".*build/MyProjectDev-" >/dev/null; then
           yarn run build-dev;
         fi
       - |
         if expr "${CODEBUILD_BUILD_ARN}" : ".*build/MyProject-" >/dev/null; then
           yarn run build-prod;
         fi

This is in a 0.2 version of buildspec.yml but we're just testing the ARN from the Codebuild process itself for a substring, and based on that running a different build command. This uses multiline capability of the yaml file

Example Arn that we're evaluating
arn:aws:codebuild:us-east-1:123456789012:build/MyProject-build:7a2a906c-9e4b-4ac5-93a8-1fb5b447b5f0

@payne911
Copy link

payne911 commented Mar 30, 2020

@ktwbc works great, but is it possible to get an if within an if ?
And what about comments within those if statements ?

I'm trying to get different path of execution based on whether or not it is a PROD environment build.

      # SonarScanner for non-prod environments
      - |
        if [ "$BUILD_ENV" = "notprod" ] ; then
          # Downloading SonarScanner
          wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.2.0.1873-linux.zip ;
          unzip ./sonar-scanner-cli-4.2.0.1873-linux.zip ;
          export PATH=$PATH:/sonar-scanner-cli-4.2.0.1873-linux/bin/ ;
          # Executing SonarScanner
          mvn sonar:sonar -Dsonar.login=$SonarLogin -Dsonar.host.url=$SonarHostUrl -Dsonar.projectKey=$SonarProjectKey ;
          # Printing the Result Report
          sleep 5 ;
          curl http://ec2-xxx.ca-central-1.compute.amazonaws.com/api/qualitygates/project_status?projectKey=$SonarProjectKey >result.json ;
          cat result.json ;
          # Conditional failure of the build
          if [ $(jq -r '.projectStatus.status' result.json) = ERROR ] ; then
            $CODEBUILD_BUILD_SUCCEEDING -eq 0 ;
          fi
        fi

@ktwbc
Copy link

ktwbc commented Mar 30, 2020

@payne911 honestly just save yourself some hassle and build out a regular bash script as a separate file and call it from buildspec.yml

Example if you put all your bash code into a file in your repo under /aws/myscript.sh then chmod a+x before you check it in, then all you have to do is:

pre_build:
commands:
- cp aws/* /usr/local/bin/

And now you can just call your script in the commands:
build:
commands:
- myscript.sh

now that you're in your own bash instead of their buildspec format it makes all that easier and you still have access to all the environment variables. One word of warning. setting any new variables like export MY_VAR= whatever in your separated bash won't be passed back to buildspec to be used later.

@payne911
Copy link

payne911 commented Mar 30, 2020

Okay.
What about modifying some variables such as $CODEBUILD_BUILD_SUCCEEDING within the script?
Thanks for your reply, by the way. :)

@omerfsen
Copy link

If you export it within (myscript.sh) script it will be inherited from codebuild bash shell too

myscript.sh:

#!/bin/bash
export CODEBUILD_BUILD_SUCCEEDING=true

source ./myscript.sh

echo ${CODEBUILD_BUILD_SUCCEEDING}

Use source instead of "."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants