Linux下持续集成环境Teamcity搭建

In any software development project a Continuous Integration/ Continuous Deployment DevOps environment is essential. A recent project I elected to make use of TeamCity & Octopus Deploy all on an Azure stack.

In this post I twill detail the steps on how to install and configure Team City 2017.x on a Ubuntu 16.X server.

Install Database software

Team City is able to store TeamCity stores build history, users, build results and some run time data to a number of Relation Database Management Systems(RDBMS) including

  • Postrgre SQL
  • MySQL
  • MS SQL
  • Oracle
  • Default Internal DB(HSQL)

In my particular case I make use of Postgres SQL, Install Postgres SQL on ubuntu

Download & Install Team city

Download the latest TeamCity for linux from Jetbrains.

1
wget https://data.services.jetbrains.com/products/download?code=TC&platform=linux

After the download completes unpack the file

1
tar -xzf TeamCity-2017.1.1.tar.gz

We will install TeamCity to the opt so we need to move the TeamCity Directory to /opt/ and set permissions to the user running the TeamCity Application.

1
2
3
4
sudo mkdir /opt/jetbrains
sudo mv Teamcity /opt/jetbrains/teamcity
cd /opt/jetbrains/teamcity
sudo chown -R $user /opt/jetbrains/teamcity
  • $user the owner of teamcity & teamagent

Configure TeamCity to start automatically with the correct use by creating a script to start and stop TeamCity. We’ll create a simple bash script using the nano text editor.

1
sudo nano /etc/init.d/teamcity

If you are going to running the team city service under username other than the one you are currently installing it with then you will need to replace $user in the code snippet with that username.

Copy and paste the following ocde into the editor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/sh
### BEGIN INIT INFO
# Provides: TeamCity autostart
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start teamcity daemon at boot time
# Description: Enable service provided by daemon.
# /etc/init.d/teamcity - startup script for teamcity
### END INIT INFO


# Ensure you enter the right user name that TeamCity will run under
USER="ubuntu"

export TEAMCITY_DATA_PATH="/opt/jetbrains/teamcity/.BuildServer"

case $1 in

start)
start-stop-daemon --start -c $USER --exec /opt/jetbrains/teamcity/bin/runAll.sh start
;;
stop)
start-stop-daemon --start -c $USER --exec /opt/jetbrains/teamcity/bin/runAll.sh stop
;;
esac

exit 0

Change the permissions required to execute the script and add it to the starup to nesure Team City is started whenever the server is start/restart

1
2
sudo chmod +x /etc/init.d/teamcity
sudo update-rc.d teamcity defaults

Download database driver

The database drivers are usually not included in the TeamCity set up files. We will need to download the PostGres Driver seperately and place them in the /opt/jetbrains/teamcity/.BuildServer/lib/jdbc folder.

Change into the directory

1
cd /opt/jetbrains/teamcity/.BuildServer/lib/jdbc

Download the driver

1
wget https://jdbc.postgresql.org/download/postgresql-9.5.xxxx.jar

We are now ready to start Team City and begin the configuration in the Team City UI

1
sudo /etc/init.d/teamcity start

Navigate to http://[Server name or IP]:8111 and verify that you see “TeamCity First Start” page.

Congratulations TeamCity is installed continue the steps on the web page to configure it.

Teamcity build agent installation

  • If you install TeamCity bundled with a Tomcat servlet container, or opt to install an agent for Windows, both the server and one build agent named Default Agent are installed on the same machine. This is not a recommended setup for production purposes because of security concerns and since the build procedure can slow down the responsiveness of the webUI and overall TeamCity server functioning. If you need more build agents, perform the procedure described below.
  • If you need the agent to run a operating system different from the TeamCity server, perform the procedure described below.
  • For production installations, it is recommended to adjust the Agent’s JVM parameters to include the -server option.

Download build agent archive from your TeamCity server

1
wget http://your_teamcity.server.com:8111/update/buildAgent.zip

Unzip it in separate directory

1
2
3
4
cd /opt/jetbrains/
sudo mkdir buildagent
sudo unzip buildAgent.zip -d buildagent
sudo chown -R $user ../buildagent

Copy agent’s settings file

1
sudo cp conf/buildAgent.dist.properties conf/buildAgent.properties

Edit this file

1
sudo nano conf/buildAgent.properties

There are fields which you must update

1
2
3
4
5
6
7
8
9
10
# your TeamCity server address:
serverUrl=http://your_teamcity.server.com:8111/


# buildAgent's name, which would displayed on Agents page in your TeamCity server UI:
name=ubuntu


# this field leave blank, agent fill it after first connect to server:
authorizationToken=

Make bin files executable

1
sudo chmod u+x bin/*.sh

Build agent usage

1
2
3
4
5
6
7
8
$ ./bin/agent.sh

JetBrains TeamCity Build Agent
Usage:
./bin/agent.sh start - to start build agent in background
./bin/agent.sh stop - to stop build agent after current build finish
./bin/agent.sh run - to start build agent in the current console
./bin/agent.sh stop force - to stop build agent terminating currently running build

Add build agent to rc.local for autostart after system reboot

1
/opt/jetbrains/buildagent/bin/agent.sh start/stop

The <agent home>/launcher/conf/wrapper.conf file can also be used to alter the agent JVM parameters.

The user account used to run build agent service must have enough rights to start/stop the agent service, as described above.

Automatic Agent Start under Linux

To run agent automatically on the machine boot under Linux, configure daemon process with the agent.sh start command to start it and agent.sh stop command to stop it. Refer to an example procedure below

  1. Navigate to the services start/stop services scripts directory
1
cd /etc/init.d/
  1. Open the build agent service script
1
sudo vim buildagent
  1. Paste the following into the file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh
### BEGIN INIT INFO
# Provides: TeamCity Build Agent
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start build agent daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
#Provide the correct user name:
USER="ubuntu"

case "$1" in
start)
su - $USER -c "cd /opt/jetbrains/buildagent/bin ; ./agent.sh start"
;;
stop)
su - $USER -c "cd /opt/jetbrains/buildagent/bin ; ./agent.sh stop"
;;
*)
echo "usage start/stop"
exit 1
;;

esac

exit 0
  1. Set the permissions to execute the file
1
sudo chmod +x buildagent
  1. Make links to start the agent service on the machine boot and on restarts using the appropriate tool
  • For Debian/Ubuntu
1
sudo update-rc.d buildagent defaults
  • For Red Hat/CentOS
1
sudo chkconfig buildagent on
  1. At the end, starting the Build Agent, and Team City will pick it up
1
sudo /etc/init.d/buildagent start

Referrence