<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TeamCity &#8211; hackanooga</title>
	<atom:link href="/category/teamcity/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Confessions of a homelab hacker</description>
	<lastBuildDate>Tue, 12 Mar 2024 19:11:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>

<image>
	<url>/wp-content/uploads/2024/03/cropped-cropped-avatar-32x32.png</url>
	<title>TeamCity &#8211; hackanooga</title>
	<link>/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Automating CI/CD with TeamCity and Ansible</title>
		<link>/automating-ci-cd-with-teamcity-ansible/</link>
		
		<dc:creator><![CDATA[mikeconrad]]></dc:creator>
		<pubDate>Mon, 11 Mar 2024 13:37:47 +0000</pubDate>
				<category><![CDATA[Ansible]]></category>
		<category><![CDATA[Automation]]></category>
		<category><![CDATA[CI/CD]]></category>
		<category><![CDATA[TeamCity]]></category>
		<category><![CDATA[Blog Post]]></category>
		<guid isPermaLink="false">https://wordpress.hackanooga.com/?p=393</guid>

					<description><![CDATA[In part one of this series we are going to explore a CI/CD option you may not be familiar with but should definitely be on your radar. I used Jetbrains TeamCity for several months at my last company and really enjoyed my time with it. A couple of the things I like most about it [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In part one of this series we are going to explore a CI/CD option you may not be familiar with but should definitely be on your radar.  I used Jetbrains TeamCity for several months at my last company and really enjoyed my time with it.  A couple of the things I like most about it are:</p>



<ul class="wp-block-list">
<li>Ability to declare global variables and have them be passed down to all projects</li>



<li>Ability to declare variables that are made up of other variables</li>
</ul>



<p>I like to use private or self hosted Docker registries for a lot of my projects and one of the pain points I have had with some other solutions (well mostly Bitbucket) is that they don&#8217;t integrate well with these private registries and when I run into a situation where I am pushing an image to or pulling an image from a private registry it get&#8217;s a little messy.  TeamCity is nice in that I can add a connection to my private registry in my root project and them simply add that as a build feature to any projects that may need it.  Essentially, now I only have one place where I have to keep those credentials and manage that connection.</p>



<p></p>



<p>Another reason I love it is the fact that you can create really powerful build templates that you can reuse.  This became very powerful when we were trying to standardize our build processes.  For example, most of the apps we build are <code>.NET</code> backends and <code>React</code> frontends.  We built docker images for every project and pushed them to our private registry.  TeamCity gave us the ability to standardize the naming convention and really streamline the build process.  Enough about that though, the rest of this series will assume that you are using TeamCity.  This post will focus on getting up and running using Ansible.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Installation and Setup</h2>



<p>For this I will assume that you already have Ansible on your machine and that you will be installing TeamCity locally.  You can simply follow along with the installation guide <a href="https://www.jetbrains.com/help/teamcity/install-teamcity-server-on-linux-or-macos.html#Example%3A+Installation+using+Ubuntu+Linux" data-type="link" data-id="https://www.jetbrains.com/help/teamcity/install-teamcity-server-on-linux-or-macos.html#Example%3A+Installation+using+Ubuntu+Linux">here</a>.  We will be creating an Ansible playbook based on the following steps.  If you just want the finished code, you can find it on my Gitea instance <a href="https://git.hackanooga.com/mikeconrad/teamcity-ansible-scripts.git">here</a>:</p>



<h4 class="wp-block-heading">Step 1 : Create project and initial playbook</h4>



<p>To get started go ahead and create a new directory to hold our configuration:</p>



<pre class="wp-block-code"><code>mkdir ~/projects/teamcity-configuration-ansible 
touch install-teamcity-server.yml</code></pre>



<p>Now open up <code>install-teamcity-server.yml</code> and add a task to install Java 17 as it is a prerequisite.  You will need sudo for this task.  ***As of this writing TeamCity does not support Java 18 or 19.  If you try to install one of these you will get an error when trying to start TeamCity.</p>



<pre class="wp-block-code"><code>---
- name: Install Teamcity
  hosts: localhost
  become: true
  become_user: sudo

 # Add some variables to make our lives easier
  vars:
    java_version: "17"
    teamcity:
      installation_path: /opt/TeamCity
      version: "2023.11.4"
  
  tasks:
  - name: Install Java
    ansible.builtin.apt:
      name: openjdk-{{ java_version }}-jre-headless
      update_cache: yes
      state: latest
      install_recommends: no</code></pre>



<p></p>



<p>The next step is to create a dedicated user account.  Add the following task to <code>install-teamcity-server.yml</code></p>



<pre class="wp-block-code"><code>  - name: Add Teamcity User
    ansible.builtin.user:
      name: teamcity</code></pre>



<p>Next we will need to download the latest version of TeamCity.  2023.11.4 is the latest as of this writing.  Add the following task to your <code>install-teamcity-server.yml</code></p>



<pre class="wp-block-code"><code>  - name: Download TeamCity Server
    ansible.builtin.get_url:
      url: https://download.jetbrains.com/teamcity/TeamCity-{{teamcity.version}}.tar.gz
      dest: /opt/TeamCity-{{teamcity.version}}.tar.gz
      mode: '0770'
</code></pre>



<p>Now to install TeamCity Server add the following:</p>



<pre class="wp-block-code"><code>  - name: Install TeamCity Server
    ansible.builtin.shell: |
      tar xfz /opt/TeamCity-{{teamcity.version}}.tar.gz
      rm -rf /opt/TeamCity-{{teamcity.version}}.tar.gz
    args:
      chdir: /opt</code></pre>



<p>Now that we have everything set up and installed we want to make sure that our new <code>teamcity</code> user has access to everything they need to get up and running.  We will add the following lines:</p>



<pre class="wp-block-code"><code>  - name: Update permissions
    ansible.builtin.shell: chown -R teamcity:teamcity /opt/TeamCity</code></pre>



<p>This gives us a pretty nice setup.  We have TeamCity server installed with a dedicated user account.  The last thing we will do is create a <code>systemd</code> service so that we can easily start/stop the server.  For this we will need to add a few things.  </p>



<ol class="wp-block-list">
<li>A service file that tells our system how to manage TeamCity</li>



<li>A j2 template file that is used to create this service file</li>



<li>A handler that tells the system to run <code>systemctl daemon-reload</code> once the service has been installed. </li>
</ol>



<p>Go ahead and create a new templates folder with the following <code>teamcity.service.j2</code> file</p>



<pre class="wp-block-code"><code>&#91;Unit]
Description=JetBrains TeamCity
Requires=network.target
After=syslog.target network.target
&#91;Service]
Type=forking
ExecStart={{teamcity.installation_path}}/bin/runAll.sh start
ExecStop={{teamcity.installation_path}}/bin/runAll.sh stop
User=teamcity
PIDFile={{teamcity.installation_path}}/teamcity.pid
Environment="TEAMCITY_PID_FILE_PATH={{teamcity.installation_path}}/teamcity.pid"
&#91;Install]
WantedBy=multi-user.target</code></pre>



<p>Your project should now look like the following:</p>



<pre class="wp-block-code"><code>$: ~/projects/teamcity-ansible-terraform
 .
├── install-teamcity-server.yml
└── templates
    └── teamcity.service.j2

1 directory, 2 files</code></pre>



<p>That&#8217;s it!  Now you should have a fully automated installed of TeamCity Server ready to be deployed wherever you need it.  Here is the final playbook file, also you can find the most up to date version in my <a href="https://git.hackanooga.com/mikeconrad/teamcity-ansible-scripts.git">repo</a>:</p>



<pre class="wp-block-code"><code>---
- name: Install Teamcity
  hosts: localhost
  become: true
  become_method: sudo

  vars:
    java_version: "17"
    teamcity:
      installation_path: /opt/TeamCity
      version: "2023.11.4"

  tasks:
  - name: Install Java
    ansible.builtin.apt:
      name: openjdk-{{ java_version }}-jdk # This is important because TeamCity will fail to start if we try to use 18 or 19
      update_cache: yes
      state: latest
      install_recommends: no

  - name: Add TeamCity User
    ansible.builtin.user:
      name: teamcity

  - name: Download TeamCity Server
    ansible.builtin.get_url:
      url: https://download.jetbrains.com/teamcity/TeamCity-{{teamcity.version}}.tar.gz
      dest: /opt/TeamCity-{{teamcity.version}}.tar.gz
      mode: '0770'

  - name: Install TeamCity Server
    ansible.builtin.shell: |
      tar xfz /opt/TeamCity-{{teamcity.version}}.tar.gz
      rm -rf /opt/TeamCity-{{teamcity.version}}.tar.gz
    args:
      chdir: /opt

  - name: Update permissions
    ansible.builtin.shell: chown -R teamcity:teamcity /opt/TeamCity

  - name: TeamCity | Create environment file
    template: src=teamcity.service.j2 dest=/etc/systemd/system/teamcityserver.service
    notify:
      - reload systemctl
  - name: TeamCity | Start teamcity
    service: name=teamcityserver.service state=started enabled=yes

  # Trigger a reload of systemctl after the service file has been created.
  handlers:
    - name: reload systemctl
      command: systemctl daemon-reload</code></pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
