22

With Snap-CI going away I've been trying to get our builds working on AWS CodeBuild. I have my buildspec.yml built out, but changing directories doesn't seem to work.

version: 0.1

phases:
  install:
    commands:
      - apt-get update -y
      - apt-get install -y node
      - apt-get install -y npm
  build:
    commands:
      - cd MyDir  //Expect to be in MyDir now
      - echo `pwd` //Shows /tmp/blablabla/ instead of /tmp/blablabla/MyDir
      - npm install //Fails because I'm not in the right directory
      - bower install
      - npm run ci
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  files:
    - MyDir/MyFile.war
  discard-paths: yes

It seems like this should be fairly simple, but so far I haven't had any luck getting this to work.

Zipper
  • 7,034
  • 8
  • 49
  • 66

2 Answers2

51

If you change the buildspec.yml version to 0.2 then the shell keeps its settings. In version: 0.1 you get a clean shell for each command.

030
  • 10,842
  • 12
  • 78
  • 123
Eric Nord
  • 4,674
  • 3
  • 28
  • 56
23

Each command in CodeBuild runs in a separate shell against the root of your source (access root of your source from CODEBUILD_SRC_DIR environment variable).

Your possible options are

  • Short circuit the commands to run under the same shell: Works when you have relatively simple buildspec (like yours).

commands: - cd MyDir && npm install && bower install - cd MyDir && npm run ci

  • Move your commands from buildspec to a script and have more control (useful for more complicated build logic).

commands: - ./mybuildscipt.sh

Let me know if any of these work for you.

-- EDIT --

CodeBuild has since launched buildspec v0.2 where this work around is no longer required.

awsnitin
  • 626
  • 3
  • 6
  • I've just run into the same issue.. Unfortunately cd'ing on every line does not help: ```[Container] 2017/07/02 12:46:55 Command did not exit successfully cd dealer-ai && npm install exit status 1``` ```[Container] 2017/07/02 12:46:55 Phase complete: INSTALL Success: false``` ```[Container] 2017/07/02 12:46:55 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: cd dealer-ai && npm install. Reason: exit status 1``` – Ilya Krasnov Jul 02 '17 at 12:48
  • 2
    CodeBuild has since launched buildspec v0.2 where this work around is no longer required. http://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec-ref-versions – awsnitin Jul 03 '17 at 16:15