How to Run Command-Line Scripts

This tutorial shows you how to use TeamCity to run the command-line scripts that are the essential foundation of your build pipelines.

We’ve created a GitHub repository for demonstration purposes:

github.com/marcobehlerjetbrains/buildpipelines. Feel free to fork it and follow along with the instructions.

The repo has 2 microservices: Authorization-Service and Calculator-Service. You can copy the URL, go to your TeamCity services, and create a new project from the repository URL. Since this is a public repository, you don’t have to enter any passwords or access tokens.

tutorials-img

In the next step, TeamCity will connect to the repo and create a project from it. It will also show you the default branch, which is the main branch that’s going to be pulled every 60 seconds for any changes, and trigger builds.

You can also specify which other branches to monitor using wildcards.

tutorials-img

Setting up build configuration in TeamCity

A build configuration in TeamCity is a job. When you set up a project, TeamCity will scan the files in your repository and autodetect build steps for you.

tutorials-img

In this tutorial, we won’t continue with the autodetected build steps because we have our own command-line script to run. However, it’s worth noting that TeamCity can scan a project’s source VCS repository and autodetect suitable build steps in Node.js, Kotlin, Python, Ant, NAnt, Gradle, Maven, MSBuild, Visual Studio solution files, PowerShell, Xcode project files, Rake, and IntelliJ IDEA projects.

Configuring build steps

To continue with manual build step configuration, click this link:

tutorials-img

Since we want to run a command-line script, let’s choose Command Line from the drop-down menu.

tutorials-img

In the next step, let’s choose the Run Custom Script option from the drop-down menu and paste our build script in the appropriate field. We’re going to run the mvn clean package command. Just like many other tools, all our build agents have this package installed on them by default.

mvn clean package will compile the Java sources, run some tests, and create a .jar file.

In the deployment step, we’re running the AWS command-line tool, and then copying that .jar file to a private S3 bucket. Then we just have to hit Save.

tutorials-img

Before we hit the Run button, we need to specify the credentials for our AWS S3 bucket: an access key ID and the access key secret. One of the ways you can do this is by using Parameters in TeamCity.

Adding parameters

To do that, click Parameters | Add New Parameter. For now, we’re going to go with one environment variable. Enter the name, which is AWS_ACCESS_KEY_ID. You’ll also need to select the kind of the new parameter – Environment variable (env.) in our case – and paste the actual access key ID.

Please note: The access key that we’re using is only for demonstration purposes, so you won’t be able to reuse this exact key.

tutorials-img

We also want to edit the parameter and set the type to Password. This means that TeamCity will hide the parameter value not just in the UI, but also in log messages and everywhere else.

tutorials-img

You will then see that the access key ID is already masked.

tutorials-img

Similarly, we can add another environment variable: the secret access key. After that, we should be able to run the build.

To do that, click the Run button which will redirect you to the overview page. While TeamCity is running the build, it will display the build logs, explaining what’s happening in every detail.

tutorials-img

Once your build is finished running, TeamCity will display the build status and data, such as when the build was triggered and by whom, how long it took to run, and what build agent was used.

tutorials-img

Because we’re using command-line scripts, the only effective output we have is the build log. If we decided to use a specific TeamCity runner instead (e.g. the Maven runner), we’d also get a test report, coverage report, and many other features, such as parallel tests – all out of the box.

Accessing build logs

To access the build log, click the Build Log tab in your build results output page. TeamCity makes reading the logs easy! Instead of forcing you to download the full build log onto your machine (which you can still do) and then opening it with Notepad++, TeamCity will collapse the build log and make it easily searchable with Ctrl+F.

tutorials-img

That’s all for now! Watch other tutorials to learn how to make use of TeamCity-specific features, like test reports or code coverage reports.

Happy building!

For more information, see TeamCity's command line documentation.