github.com/ratanraj/packer@v1.3.2/website/source/guides/packer-on-cicd/build-virtualbox-image.html.md (about)

     1  ---
     2  layout: guides
     3  sidebar_current: guides-packer-on-cicd-build-virtualbox
     4  page_title: Build a VirtualBox Image with Packer 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](/docs/builders/vmware.html) and the [QEMU](/docs/builders/qemu.html)
    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://www.terraform.io/docs/providers/packet/index.html) 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
    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  ```
    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](https://www.terraform.io/docs/provisioners/remote-exec.html) 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](https://www.packer.io/downloads.html) page.
    85  
    86  ```shell
    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  ![TeamCity screenshot: New Build](/assets/images/guides/teamcity_create_project_from_url-1.png)
   113  
   114  Click **Proceed**.
   115  
   116  ![TeamCity screenshot: New Build](/assets/images/guides/teamcity_create_project_from_url-2.png)
   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  ![TeamCity screenshot: Build Step](/assets/images/guides/teamcity_build_configuration.png)
   126  
   127  
   128  This will use the `build` command in Packer to build the image defined in
   129  `ubuntu/ubuntu-16.04-amd64.json`. It assumes that the VCS repository you're
   130  using is a fork of [Chef/Bento](https://github.com/chef/bento). Packer defaults
   131  to building VirtualBox machines by launching a GUI that shows the console.
   132  Since this will run in CI/CD, use the [`headless`
   133  variable](/docs/builders/virtualbox-iso.html#headless) to instruct Packer to
   134  start the machine without the console. Packer can build multiple image types,
   135  so the [`-only=virtualbox-iso`
   136  option](/docs/commands/build.html#only-foo-bar-baz) instructs Packer to only
   137  build the builds with the name `virtualbox-iso`.
   138  
   139  ## 6. Run a build in TeamCity
   140  
   141  The entire configuration is ready for a new build. Start a new run in TeamCity
   142  by pressing “Run”.
   143  
   144  The new run should be triggered and the virtual box image will be built.
   145  
   146  ![TeamCity screenshot: Build log](/assets/images/guides/teamcity_build_log.png)
   147  
   148  Once complete, the build status should be updated to complete and successful.
   149  
   150  ![TeamCity screenshot: Build log complete](/assets/images/guides/teamcity_build_log_complete.png)