brno32
brno32

Reputation: 424

Can't run bash script in CodeBuild

I have the following buildspec.yml

version: 0.2

env:
  shell: bash
phases:
  install:
    runtime-versions:
      nodejs: 12
    commands:
      - source cicd/app_cicd.sh
      - npm_install

Where cicd/app_cicd.sh is

#!/bin/bash

function npm_install() {
    npm install
}

But the CodeBuild output shows

[Container] 2021/05/23 01:55:32 Phase complete: DOWNLOAD_SOURCE State: SUCCEEDED
[Container] 2021/05/23 01:55:32 Phase context status code:  Message: 
[Container] 2021/05/23 01:55:33 Entering phase INSTALL
[Container] 2021/05/23 01:55:33 Running command export CICD_ROOT=$(pwd)

[Container] 2021/05/23 01:55:33 Running command source cicd/app_cicd.sh

[Container] 2021/05/23 01:55:33 Running command npm_install
/codebuild/output/tmp/script.sh: line 4: npm_install: command not found

Given that I've specified to use bash in the buildspec.yml and my shell script includes a shebang, I'm not sure what's wrong. This is of course a contrived example

Upvotes: 4

Views: 3057

Answers (1)

Marcin
Marcin

Reputation: 238957

npm_install must be in same line as your source, otherwise they are executed fully independently. So your source does not carry over to the second command.

version: 0.2

env:
  shell: bash
phases:
  install:
    runtime-versions:
      nodejs: 12
    commands:
      - source cicd/app_cicd.sh && npm_install

Upvotes: 5

Related Questions