github.com/hashicorp/packer@v1.14.3/website/content/guides/packer-on-cicd/build-virtualbox-image.mdx (about) 1 --- 2 page_title: Build a VirtualBox Image with Packer in TeamCity 3 description: >- 4 Use Packer to build VirtualBox images in TeamCity. Learn how to provision a bare-metal machine, install dependencies, and create and run a build in TeamCity. 5 --- 6 7 # Build a VirtualBox Image with Packer in TeamCity 8 9 This guide walks through the process of building a VirtualBox image using 10 Packer on a new TeamCity Agent. Before getting started you should have access 11 to a TeamCity Server. 12 13 The Packer VirtualBox builder requires access to VirtualBox. Virtualization is 14 not universally supported on cloud instances, so we recommend you run these 15 builds on either a bare metal server, or cloud instances which support nested 16 virtualization, such as Azure or GCP. This is also true for the 17 [VMware](/packer/plugins/builders/vmware) and the [QEMU](/packer/plugins/builders/qemu) 18 Packer builders. 19 20 We will use Chef's [Bento boxes](https://github.com/chef/bento) to provision an 21 Ubuntu image on VirtualBox. For this example, we will use the repository 22 directly, but you may also fork it for the same results. 23 24 ## 1. Provision a Bare-metal Machine 25 26 For the purposes of this example, we will run on a bare-metal instance from 27 [Packet](https://www.packet.net/). If you are a first time user of Packet, the 28 Packet team has provided HashiCorp the coupon code `hashi25` which you can use 29 for `$25` off to test out this guide and up to 30% if you decide to 30 reserve ongoing servers (email help@packet.net for details). You can use 31 a `baremetal_0` server type for testing, but for regular use, the `baremetal_1` 32 instance may be a better option. 33 34 There is also a [Packet 35 Provider](https://registry.terraform.io/providers/packethost/packet/latest/docs) in 36 Terraform you can use to provision the project and instance. 37 38 ```hcl 39 provider "packet" { } 40 41 resource "packet_project" "teamcity_agents" { 42 name = "TeamCity" 43 } 44 45 resource "packet_device" "agent" { 46 hostname = "teamcity-agent" 47 plan = "baremetal_0" 48 facility = "ams1" 49 operating_system = "ubuntu_16_04" 50 billing_cycle = "hourly" 51 project_id = "${packet_project.teamcity_project.id}" 52 } 53 ``` 54 55 ## 2. Install VirtualBox and TeamCity dependencies 56 57 VirtualBox must be installed on the new instance, and TeamCity requires the JDK 58 prior to installation. This guide uses Ubuntu as the Linux distribution, so you 59 may need to adjust these commands for your distribution of choice. 60 61 **Install Teamcity Dependencies** 62 63 ```shell-session 64 $ apt-get upgrade 65 $ apt-get install -y zip linux-headers-generic linux-headers-4.13.0-16-generic build-essential openjdk-8-jdk 66 ``` 67 68 **Install VirtualBox** 69 70 ```shell-session 71 $ curl -OL "https://download.virtualbox.org/virtualbox/5.2.2/virtualbox-5.2_5.2.2-119230~Ubuntu~xenial_amd64.deb" 72 $ dpkg -i virtualbox-5.2_5.2.2-119230~Ubuntu~xenial_amd64.deb 73 ``` 74 75 You can also use the [`remote-exec` 76 provisioner](/terraform/docs/provisioners/remote-exec) in 77 your Terraform configuration to automatically run these commands when 78 provisioning the new instance. 79 80 ## 3. Install Packer 81 82 The TeamCity Agent machine will also need Packer Installed. You can find the 83 latest download link from the [Packer 84 Download](/packer/downloads) page. 85 86 ```shell-session 87 $ curl -OL "https://releases.hashicorp.com/packer/1.1.2/packer_1.1.2_linux_amd64.zip" 88 $ unzip ./packer_1.1.2_linux_amd64.zip 89 ``` 90 91 Packer is installed at the `/root/packer` path which is used in subsequent 92 steps. If it is installed elsewhere, take note of the path. 93 94 ## 4. Install TeamCity Agent 95 96 This guide assume you already have a running instance of TeamCity Server. The 97 new TeamCity Agent can be installed by [downloading a zip file and installing 98 manually](https://confluence.jetbrains.com/display/TCD10//Setting+up+and+Running+Additional+Build+Agents#SettingupandRunningAdditionalBuildAgents-InstallingAdditionalBuildAgents), 99 or using [Agent 100 Push](https://confluence.jetbrains.com/display/TCD10//Setting+up+and+Running+Additional+Build+Agents#SettingupandRunningAdditionalBuildAgents-InstallingviaAgentPush). 101 Once it is installed it should appear in TeamCity as a new Agent. 102 103 Create a new Agent Pool for agents responsible for VirtualBox Packer builds and 104 assign the new Agent to it. 105 106 ## 5. Create a New Build in TeamCity 107 108 In TeamCity Server, create a new build. To use the upstream Bento repository, 109 we'll choose _From a repository URL_, and enter 110 `https://github.com/chef/bento.git` as the **Repository URL**. 111 112  113 114 Click **Proceed**. 115 116  117 118 And **Proceed** again. 119 120 We won't use the _Auto-detected Build Steps_. Instead, click _configure build 121 steps manually_. For the _runner type_, pick **Command Line**, and enter the 122 following values. Make sure to click _Show advanced options_, as we need to set 123 the working directory. 124 125  126 127 This will use the `build` command in Packer to build the image defined in 128 `ubuntu/ubuntu-16.04-amd64.json`. It assumes that the VCS repository you're 129 using is a fork of [Chef/Bento](https://github.com/chef/bento). Packer defaults 130 to building VirtualBox machines by launching a GUI that shows the console. 131 Since this will run in CI/CD, use the [`headless` 132 variable](/packer/plugins/builders/virtualbox/iso#headless) to instruct Packer to 133 start the machine without the console. Packer can build multiple image types, 134 so the [`-only=virtualbox-iso` 135 option](/packer/docs/commands/build#only-foo-bar-baz) instructs Packer to only 136 build the builds with the name `virtualbox-iso`. 137 138 ## 6. Run a build in TeamCity 139 140 The entire configuration is ready for a new build. Start a new run in TeamCity 141 by pressing “Run”. 142 143 The new run should be triggered and the virtual box image will be built. 144 145  146 147 Once complete, the build status should be updated to complete and successful. 148 149 