Adding the CI/CD guide content

This commit is contained in:
Maciej Skierkowski 2017-12-08 10:31:00 -08:00
parent 42d0e6f920
commit 59172c5a2d
8 changed files with 149 additions and 1 deletions

View File

@ -7,3 +7,12 @@ description: |-
---
# Building Images in CI/CD
The following guides from our amazing partners show how to use their service to build images with Packer.
- [How to Build Immutable Infrastructure with Packer and CircleCI Workflows](https://docs.google.com/document/d/1hetlS94SpUQ979K-1At9hwn1oDNWHegBqHGNcIFLBoY/edit)
- [Using Packer and Ansible to Build Immutable Infrastructure](https://blog.codeship.com/packer-ansible/)
For the majority of the [Packer Builders](https://www.packer.io/docs/builders/index.html) can run in a container or VM, a common model used by most CI/CD services. However, the [QEMU builder](https://www.packer.io/docs/builders/qemu.html) for [KVM](https://www.linux-kvm.org/page/Main_Page) and [Xen](https://www.xenproject.org/) virtual machine images, [VirtualBox builder](https://www.packer.io/docs/builders/virtualbox.html) for OVA or OVF virtual machines and [VMWare builder](https://www.packer.io/docs/builders/vmware.html) for use with VMware products require running on a bare-metal machine.
[Building a VirtualBox Image with Packer in TeamCity](https://docs.google.com/document/d/1AQjn4PpApnZ6pf097HYZzZa4ZMspRATxo9wNj78hLLc/edit#)

View File

@ -7,3 +7,96 @@ description: |-
---
# Building a VirtualBox Image with Packer in TeamCity
This guide walks through the process of building a VirtualBox image using Packer on a new TeamCity Agent. Before getting started you should have access to a TeamCity Server.
The Packer VirtualBox builder requires access to VirtualBox which should run on a bare-metal machine as virtual machines should not run inside other virtual machines. This is also true for the [VMWare](https://www.packer.io/docs/builders/vmware.html) and the [QEMU](https://www.packer.io/docs/builders/qemu.html) Packer builders.
## 1. Provision a bare-metal machine
The Packer VirtualBox builder requires running on bare-metal (hardware). If you do not have access to a bare-metal machine, we recommend using [Packet.net](https://www.packet.net/) to obtain a new machine. If you are a first time user of Packet.net, the Packet.net team has provided HashiCorp the coupon code `hash25` which you can use for $25 off to test out this guide. You can use a `baremetal_0` for testing, but for regular use the `baremetal_1` instance may be a better option.
There is also a [Packet Provider](https://www.terraform.io/docs/providers/packet/index.html) in Terraform you can use to provision the project and instance.
```hcl
provider "packet" { }
resource "packet_project" "teamcity_agents" {
name = "TeamCity"
}
resource "packet_device" "agent" {
hostname = "teamcity-agent"
plan = "baremetal_0"
facility = "ams1"
operating_system = "ubuntu_16_04"
billing_cycle = "hourly"
project_id = "${packet_project.teamcity_project.id}"
}
```
## 2. Install VirtualBox and TeamCity Dependencies
VirtualBox must be installed on the new instance along and TeamCity requires the JDK prior to installation. This guide uses Ubuntu as the Linux distribution, so you may need to adjust these commands for your distribution of choice.
**Install Teamcity Dependencies**
```shell
apt-get upgrade
apt-get install -y zip linux-headers-generic linux-headers-4.13.0-16-generic build-essential openjdk-8-jdk
```
**Install VirtualBox**
```
curl -OL "http://download.virtualbox.org/virtualbox/5.2.2/virtualbox-5.2_5.2.2-119230~Ubuntu~xenial_amd64.deb"
dpkg -i virtualbox-5.2_5.2.2-119230~Ubuntu~xenial_amd64.deb
```
You can also use the [`remote-exec` provisioner](https://www.terraform.io/docs/provisioners/remote-exec.html) in your terraform configuration to automatically run these commands when provisioning the new instance.
## 3. Install Packer
The TeamCity Agent machine will also need Packer Installed. You can find the latest download link from the [Packer Download](https://www.packer.io/downloads.html) page.
```shell
curl -OL "https://releases.hashicorp.com/packer/1.1.2/packer_1.1.2_linux_amd64.zip"
unzip ./packer_1.1.2_linux_amd64.zip
```
Packer is installed at the `/root/packer` path which is used in subsequent steps. If it is installed elsewhere, take note of the path.
## 4. Install TeamCity Agent
This guide assume you already have a running instance of TeamCity Server. The new TeamCity Agent can be installed by [downloading a zip file and installing manually](https://confluence.jetbrains.com/display/TCD10//Setting+up+and+Running+Additional+Build+Agents#SettingupandRunningAdditionalBuildAgents-InstallingAdditionalBuildAgents), or using [Agent Push](https://confluence.jetbrains.com/display/TCD10//Setting+up+and+Running+Additional+Build+Agents#SettingupandRunningAdditionalBuildAgents-InstallingviaAgentPush). Once it is installed it should appear in TeamCity as a new Agent.
Create a new Agent Pool for the agents which will be responsible for the VirtualBox Packer builds and the assign the new Agent to the new Agent Pool.
## 5. Create a new Build in TeamCity
In TeamCity Server create a new build and configure the Version Control Settings to download the Packer build configuration from the VCS repository.
Add one **Build Step: Command Line** to the build.
![TeamCity screenshot: New Build](./images/teamcity_new_build.png)
In the **Script content** field add the following:
```shell
#!/usr/bin/env bash
/root/packer build -only=virtualbox-iso -var "headless=true" ./packer.json
```
This assumes that `packer.json` is the Packer build configuration file in the root path of the VCS repository.
## 6. Run a build in TeamCity
The entire configuration is ready for a new build. Start a new run in TeamCity by pressing “Run”.
The new run should be triggered and the virtual box image will be built.
![TeamCity screenshot: Build log](./images/teamcity_build_log.png)
Once complete, the build status should be updated to complete and successful.
![TeamCity screenshot: Build log complete](./images/teamcity_build_log_complete.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 325 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 KiB

View File

@ -8,3 +8,8 @@ description: |-
# Building Immutable Infrastructure with Packer in CI/CD
This guide focuses on the following workflow for building immutable infrastructure. This workflow can be manual or automated and it can be implemented with a variety of technologies. The goal of this guide is to show how this workflow can be fully automated using Packer for building images from a CI/CD pipeline.
1. [Building Images using Packer in CI/CD](./building-image-in-cicd.html)
2. [Uploading the new image to S3](./uploading-images-to-artifact.html) for future deployment or use during development
3. Provision new instances with the images using Terraform Enterprise by [creating a new Terraform Enterprise runs](./triggering-tfe.html).

View File

@ -6,4 +6,29 @@ description: |-
...
---
# Triggering Terraform Enterprise runs
# Creating a Terraform Enterprise runs
Once an image is built and uploaded to an artifact store, the next step is to use this new image. In some cases the image will be downloaded by the dev team and used locally in development, like is often done with VirtualBox images with Vagrant. In most other cases, the new image will be used to provision new infrastructure. [Terraform](https://www.terraform.io/) is an open source tool that is ideal for provisioning the new infrastructure with the new image generated by Packer.
The following is a sample terraform configuration which provisions a new AWS EC2 instance. The `aws_ami_id` is a variable which will be provided when running `terraform plan` and `terraform apply`. This variable references the latest AMI generated with the Packer build in CI/CD.
```hcl
variable "aws_ami_id" { }
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "web" {
ami = "${var.aws_ami_id}"
instance_type = "t2.micro"
}
```
Terraform Enterprise should have a workspace with this terraform configuration and a placeholder variable `aws_ami_id`.
**Steps to create a new run from CI/CD after a Packer build is complete and uploaded**:
1. Add a new step to the CI/CD pipeline.
2. In the new step add a `curl` call to update the variables in the workspace using the [update variables API](https://www.terraform.io/docs/enterprise-beta/api/variables.html#update-variables) with the reference to the latest image. In the sample terraform configuration above, the “aws_ami_id” variable would be updated to the AMI ID of the latest image.
3. In that same step, add another `curl` call to [create a new run via the API](https://www.terraform.io/docs/enterprise-beta/api/run.html#create-a-run). A run performs a plan and apply on the last configuration version created and using the variables set in the workspace. In the previous step we update the variables, so the new run can be created using the previous configuration version.

View File

@ -7,3 +7,19 @@ description: |-
---
# Uploading Images to Artifact Stores
Once the image is generated it will be used by other parts of your operations workflow. For example, it is common to build VirtualBoxes with Packer to be used as base boxes in Vagrant.
On the agent machine install the [AWS Command Line Tool](https://aws.amazon.com/cli/). Since this is a one-time operation, this can be incorporated into the initial provisioning step when installing other dependencies.
```shell
pip install awscli
```
Add an additional **Build Step: Command Line** to the build and set the **Script content** field to the following:
```shell
awscli s3 cp . s3://bucket/ --exclude “*” --include “*.iso”
```
TeamCity provides a [Build Artifacts](https://confluence.jetbrains.com/display/TCD9/Build+Artifact) feature which can be used to store the newly generated image. Other CI/CD services also have similar build artifacts features built in, like [Circle CI Build Artifacts](https://circleci.com/docs/2.0/artifacts/). In addition to the built in artifact stores in CI/CD tools, there are also dedicated universal artifact storage services like [Artifactory](https://confluence.jetbrains.com/display/TCD9/Build+Artifact). All of these are great options for image artifact storage.