Skip to main content

Run a script in a Build stage

You can use a CI Run step to run scripts and commands in Build stages in CI pipelines. Here are a few examples of ways you could use Run steps:

  • Pull the Docker image for Maven and then run the mvn package command with that tool.
  • Run tests with pytest.
  • Run a simple echo or sleep command.
  • Set up an environment or specify a version, such as sudo xcode-select -switch /Applications/Xcode_14.1.0.app.

Depending on the stage's build infrastructure, a Run step can use binaries that exist in the build environment or pull an image, such as a public or private Docker image, that contains the required binaries.

Requirements

This topic assumes you are familiar with the following:

You need a CI pipeline to which you can add the Run step.

Prepare your pipeline

You need a CI pipeline with a Build stage that is connected to your codebase. If you haven't created a pipeline before, try one of the CI pipeline tutorials.

To add a Build stage to an existing pipeline:

  1. Go to the pipeline you want to edit.
  2. In the Pipeline Studio, select Add Stage, and then select Build.
  3. Enter a Stage Name, enable Clone Codebase, and then select Set Up Stage.
  4. Select the Infrastructure tab and set up the build infrastructure.

To check codebase configuration for existing pipelines, select Codebase while viewing the pipeline in the Pipeline Studio. For more information about codebase configuration, go to Edit Codebase Configuration.

Add the Run step

  1. Go to the Build stage in the pipeline where you want to add the Run step.

  2. Select Add Step, select Add Step again, and then select Run in the Step Library.

  3. Enter a Name and optional Description.

  4. Depending on the stage's build infrastructure, specify the Container Registry and Image containing the binaries that the step needs to run your script. For example, a cURL script may require a cURL image, such as curlimages/curl:7.73.0. For information about when these fields are required and how to specify images, go to the Run step settings reference.

  5. Select the Shell type and input your script in the Command field.

  6. Populate other Run step settings as necessary. For example:

    • If your script runs tests, you might specify Report Paths.
    • If your script requires a token or secret, you might need to supply the token as an Environment Variable.
    • If you script produces an output variable value, you must declare the variable name in Output Variables.
  7. Select Apply Changes to save the step, and then select Save to save the pipeline.

Run the Pipeline

Select Run to run the pipeline. Depending on your codebase configuration, you may need to specify a branch or tag.

While the build runs, you can observe the step logs on the Build details page.

After the pipeline runs, you can view test reports on the Tests tab of the Build details page.

tip

For an example of a Run step that runs tests and produces test reports, go to the Code coverage with CodeCov in Harness CI tutorial.