Running bash scripts

This page explains how to configure Cloud Build to run bash scripts within a build step. If you're new to Cloud Build, read the quickstarts and the Build configuration overview first.

You can run bash scripts within a build step to configure a number of workflows including:

  • Running multiple commands in one build step.
  • Reading from the filesystem.
  • Building in logic such as retries or conditionals.
  • Outputting to the log, for example, running echo $VARNAME.

To run bash scripts in a build step, you can either use the bash image, or any other image that has bash installed in it. Standard base images such as ubuntu and debian can run bash scripts. Some of the supported cloud builders such as the gcloud and the docker builders also have bash installed in them.

To find out if an image has bash installed in it, check the image's Dockerfile.

Running inline bash scripts

To run bash commands using the bash image, specify bash as the name of the build step, and the command in the args field:

YAML

steps:
- name: 'bash'
  args: ['echo', 'I am running a bash command']

JSON

{
  "steps": [
    {
      "name": "bash",
      "args": [
        "echo",
        "I am running a bash command"
       ]
    }
  ]
}

If the image you're using comes prepackaged with bash and if bash is the image's default entrypoint, specify the image in the name field and the bash command in the args field:

YAML

steps:
- name: ubuntu
  args:
  - '-set eEo pipefail'
  - '-c'
  - |-
    var=$(curl abc.com)
    if [var =="foo"]; do
      echo "it's a foo!"
    else
      echo "not foo"
    fi

JSON

{
  "steps": [
   {
     "name": "ubuntu",
     "args": [
       "-set eEo pipefail",
       "-c",
       "var=$(curl abc.com)\nif [var =="foo"]; do\n  echo "it's a foo!"\nelse\n  echo "not foo"\nfi"
     ]
   }
   ]
}

The -c flag in the code above is used to execute multi-line commands. Any string you pass after -c is treated as a command. For more information on running bash commands with -c, see the bash documentation.

If the image you're using comes prepackaged with bash but if bash is not the default entrypoint, add an entrypoint field pointing to bash. The following example uses bash to login to a private Docker registry:

YAML

steps:
- name: 'gcr.io/cloud-builders/docker'
  entrypoint: 'bash'
  args: ['-c', 'docker login --username=user-name --password=$$PASSWORD']
  secretEnv: ['PASSWORD']
secrets:
- kmsKeyName: projects/project-id/locations/global/keyRings/keyring-name/cryptoKeys/key-name
  secretEnv:
    PASSWORD: 'encrypted-password'

JSON

{
  "steps": [
  {
    "name": "gcr.io/cloud-builders/docker",
    "entrypoint": "bash",
    "args": [
      "-c",
      "docker login --username=user-name --password=$$PASSWORD"
     ],
     "secretEnv": [
       "PASSWORD"
      ]
  }
  ],
  "secrets": [
  {
    "kmsKeyName": "projects/project-id/locations/global/keyRings/keyring-name/cryptoKeys/key-name",
    "secretEnv": {
      "PASSWORD": "encrypted-password"
     }
  }
  ]
}

Running bash scripts on disk

If you have your bash script saved in a file, store the file along with your build source, and reference the script file within your build config file:

YAML

steps:
- name: 'bash'
  args: ['./myscript.bash']

JSON

{
  "steps": [
  {
    "name": "bash",
    "args": [
      "./myscript.bash"
     ]
  }
  ]
}

To use a bash script on file if bash is not the default entrypoint of the image you're using, add an entrypoint field pointing to bash:

YAML

steps:
- name: gcr.io/cloud-builders/gcloud
  entrypoint: 'bash'
  args:
- '-set eEo pipefail'
- '-c'
- |-
  ./tools/myScript.sh --foo
  ./tools/myOtherScript.sh --bar

JSON

{
  "steps": [
  {
    "name": "gcr.io/cloud-builders/gcloud",
    "entrypoint": "bash",
    "args": [
      "-set eEo pipefail",
      "-c",
      "./tools/myScript.sh --foo\n./tools/myOtherScript.sh --bar"
    ]
  }
  ]
}

What's next