# Best practices for automatically building images in a CI/CD pipeline?

**URL:** <https://hub.mender.io/t/best-practices-for-automatically-building-images-in-a-ci-cd-pipeline/1866>\
**Category:** General Discussions\
**Created:** [April 30, 2020, 11:07pm UTC](https://hub.mender.io/t/best-practices-for-automatically-building-images-in-a-ci-cd-pipeline/1866 "2020-04-30T23:07:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![krystof](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/krystof/32/615_2.png) [@krystof](https://hub.mender.io/u/krystof)\
**Post date:** [April 30, 2020, 11:07pm UTC](https://hub.mender.io/t/best-practices-for-automatically-building-images-in-a-ci-cd-pipeline/1866/1 "2020-04-30T23:07:23Z")

</div>

Hi,

I’m looking for any best practices for automatically building images as part of a CI/CD pipeline.

Here’s how I envision things working:

- I push a new commit to a repo containing the manifest files for my application (what packages are needed, versions of my containers etc.)
- a CI/CD pipeline is triggered (for example, a GitHub Action)
- this CI/CD pipeline builds a new image in a systematic, reproducible way based on the manifests in the repo (I would like this process to be similar to Docker builds containers from Dockerfiles)
- the CI/CD pipeline runs some tests on the resulting image
- if all tests pass, the pipeline creates a new image and pushes it to my Mender server

Have any of you implemented a pipeline like this? How?

I could see some solutions ways to do it using tools like Ansible, Puppet, Chef, Saltstack etc… but I don’t quite have the complete picture figured out. I would love to learn from your experience.

For context, here’s some information about my project:

- ubuntu server-based
- runs a bunch of containers
- also runs some services on bare metal

Thanks!

---

<div class="post-metadata">

**Author:** ![samlewis](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/samlewis/32/393_2.png) [@samlewis](https://hub.mender.io/u/samlewis)\
**Post date:** [April 30, 2020, 11:48pm UTC](https://hub.mender.io/t/best-practices-for-automatically-building-images-in-a-ci-cd-pipeline/1866/2 "2020-04-30T23:48:25Z")

</div>

Assuming you’re using Yocto, [kas](https://github.com/siemens/kas) and [kas-docker](https://github.com/siemens/kas/blob/master/kas-docker) go a long way to ensuring reproducible build environments, especially for CI.

I essentially implemented the first three steps of your plan using Gitlab CI and kas, but I’d assume it would be similar with Github actions. Because Yocto builds need a lot of processing power, I use the [Gitlab docker-machine runner](https://docs.gitlab.com/runner/executors/docker_machine.html) to spin up a really beefy cloud VM to do the build, which means I’m only paying for the beefy cloud VM while the build is taking place and builds are relatively quick (~1 hour).

edit: Reread your post and realised you’re building on top of Ubuntu server, so maybe the Yocto advice is not so useful, sorry. For a different project I automated building up an Ubuntu image using [kickstarts](https://help.ubuntu.com/lts/installation-guide/i386/ch04s06.html). Maybe you could use those to install all the packages in your base image and then build and copy your containers over?

---

<div class="post-metadata">

**Author:** ![dellgreen](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dellgreen/32/85_2.png) [@dellgreen](https://hub.mender.io/u/dellgreen)\
**Post date:** [May 1, 2020, 9:26am UTC](https://hub.mender.io/t/best-practices-for-automatically-building-images-in-a-ci-cd-pipeline/1866/3 "2020-05-01T09:26:05Z")

</div>

Is the golden image route a good starting point?

[https://docs.mender.io/2.3/artifacts/debian-family/building-a-mender-debian-image](https://docs.mender.io/2.3/artifacts/debian-family/building-a-mender-debian-image)

---

<div class="post-metadata">

**Author:** ![krystof](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/krystof/32/615_2.png) [@krystof](https://hub.mender.io/u/krystof)\
**Post date:** [May 3, 2020, 12:08am UTC](https://hub.mender.io/t/best-practices-for-automatically-building-images-in-a-ci-cd-pipeline/1866/4 "2020-05-03T00:08:44Z")

</div>

Thanks Sam. Kickstarts go some of the way to covering what I am looking for.

How did you run those jobs? I assume you made the images in virtual machine - what did you use to do so?

---

<div class="post-metadata">

**Author:** ![krystof](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/krystof/32/615_2.png) [@krystof](https://hub.mender.io/u/krystof)\
**Post date:** [May 3, 2020, 12:10am UTC](https://hub.mender.io/t/best-practices-for-automatically-building-images-in-a-ci-cd-pipeline/1866/5 "2020-05-03T00:10:15Z")

</div>

The recommended workflow for golden images suggests manually booting the device, logging in, and making all modifications. This is time-consuming, and error-prone, and unsuitable for CI/CD, which is what I am trying to accomplish.

---

<div class="post-metadata">

**Author:** ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)\
**Post date:** [May 4, 2020, 5:00pm UTC](https://hub.mender.io/t/best-practices-for-automatically-building-images-in-a-ci-cd-pipeline/1866/6 "2020-05-04T17:00:36Z")

</div>

HI @krystof indeed the Golden Master approach is a bit awkward and, as you say, time consuming. I have started playing with [mkosi](https://github.com/systemd/mkosi) which is a wrapper around the Debian build utilities and supports a decent number of distros. So far it seems pretty useful

Drew

---

<div class="post-metadata">

**Author:** ![krystof](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/krystof/32/615_2.png) [@krystof](https://hub.mender.io/u/krystof)\
**Post date:** [May 4, 2020, 9:50pm UTC](https://hub.mender.io/t/best-practices-for-automatically-building-images-in-a-ci-cd-pipeline/1866/7 "2020-05-04T21:50:10Z")

</div>

@drewmoseley `mkosi` looks like exactly what I need! Thanks a bunch!
