Sometimes you might want to connect different machines during your build in a project. For example, to trigger a deployment script on a remote machine via SSH.
Let’s learn how that’s done.
Secure Shell (SSH) is widely used for various purposes due to its security, versatility, and efficiency. Here are several reasons to use SSH
Go to Edit configuration | Build Steps to have a quick look at your build steps.
Imagine you want to add a command line build step that executes a deployment. To do so, TeamCity connects to a remote server via SSH and executes a couple of commands on that server, e.g. to download a file from Amazon S3 and execute it. In order for the SSH connection to work, you need to specify an SSH key.
The question here is: How does the SSH key get to your build agent if it’s not pre-baked into your agent image?
What we want to do is to get rid of the command line build step. To do that, click on the Runner type drop-down menu. Here, you’ll notice two options: SSH Exec and SSH Upload.
SSH Exec is convenient for when you want to execute something on a remote machine. SSH Upload helps you to securely upload files to a server.
Let’s choose the option SSH Exec and add the target and commands for the runner.
Here, under Authentication method, you have a few choices to choose where your SSH key comes from.
Uploaded key: The key is uploaded to the TeamCity server. The server will encrypt it, send it to the build agent for the build, and then delete it after the build is finished.
Default private key and Custom private key: Choose this option if you have a default private key pre-installed in specific locations on your build agent.
You can also choose a username-password combination or even have a running SSH agent. Read more about SSH Exec in our documentation.
In this tutorial, we’re going to go with the Uploaded keyoption. We’ll set the username to teamcity-deploy and we’ll skip setting the passphrase. We’ll then go ahead and upload the SSH key.
To upload the SSH key, let’s go to Buildpipelines project | SSH Keys. After that, we’ll select the private key for our server. Click Upload SSH Key and select a file from your computer.
After that, go back to Build steps | SSH Exec and select the private key that you have just uploaded. Then hit Save and run the build.
Once the build finishes running, open up the Build Log, and you’ll see the output from the SSH Exec build step. Here, we can see that TeamCity executed the command that you input into the build step configuration. It will also show you the result of the echo ‘running deploy.sh...’
command.
That’s about it!
This tutorial shows you how to use TeamCity to run the command-line scripts that are the essential foundation of your build pipelines.
In this tutorial, we’ll take a closer look at how to work with artifacts in TeamCity in order to get data out of your builds.
TeamCity bundles a great number of features that will supercharge your builds. In this tutorial, we’ll explore how to use specific runners and why you’ll want to use them.