<p>You've missed the part of the 1st quote about multi-stage builds and other methods which avoid exactly the problem mentioned in the 2nd quote: the resulting image would consist of it's base image and artifacts from previous stages while being run in the same <em>build</em> step of a pipeline.</p>

CC BY-SA 4.0
2021-07-21T13:13:57.963Z
2021-07-21T13:13:57.963Z
chupasaurus
<p>I seem to get a unique response for every person that I ask, or every article that I read, which is just confusing me even more.</p>
<p>Basically, I'm trying to setup automated testing in the CI pipeline which I'm pretty new to. The stack is basically these things:</p>
<ul>
<li>Django API service</li>
<li>React Admin Portal service</li>
<li>React Client Portal service</li>
<li>PostgreSQL</li>
<li>Redis</li>
<li>Azure Kubernetes Services</li>
<li>Azure DevOps Pipelines with Microsoft Hosted Agent</li>
</ul>
<p>I was originally under the impression thanks to advice <a href="https://softwareengineering.stackexchange.com/questions/425232/clarifying-the-steps-in-a-ci-cd-but-namely-if-if-unit-testing-should-be-done-bu">here</a>, which was pretty consistent among respondents, that basically my CI pipeline should be the following:</p>
<ol>
<li>Build the services.</li>
<li>Run unit tests.</li>
<li>If passing, <code>COPY</code> the build into the Docker image.</li>
<li>Push Docker image to Container Registry.</li>
<li>Deploy images to production-like Kubernetes (using Kind, Minikube, etc.) in a pipeline VM (&quot;Hosted Agent&quot; in Azure DevOps terms).</li>
<li>Run integration tests.</li>
<li>Run E2E/Selenium tests.</li>
<li>If passing, merge feature branch to production.</li>
</ol>
<p>Started talking to someone else about it and they basically said:</p>
<blockquote>
<p>Unit &gt; integration &gt; e2e. Test as much as you can at each step without pissing the devs off. Testing in parallel is better because it lessens the time taken. If a failure in any test type means the pipeline breaks the only thing that controls when you run the test is if you can do it before the app is running (unit, functional) or after compile and deployment (integration/e2e).</p>
<p>All of that aside: <strong>DON'T COPY COMPILED CODE INTO A CONTAINER</strong>. Such an anti-pattern and bad practice. The Dockerfile should include the entire build process and remain immutable once built. Copying code into your container is the equivalent of passing it to a coworker and assuming it's going to run on their laptop.</p>
<p>Multi-stage builds and development containers containing sdks/build-kits/whatever are the first step, runtime containers that contain only the necessary pieces to run are the second. If they aren't available from the platform provider - .net, java, python etc. (they are) - you can build them both out of the same base container which standardizes both the build and runtime environments.</p>
<p>Use multi-stage Dockerfiles and combine to reduce steps so that containers stay lean.</p>
</blockquote>
<p>In other words:</p>
<ol>
<li>Build the services in a Docker image.</li>
<li>Run unit tests in the the Docker image.</li>
<li>If passing, deploy images to production-like Kubernetes (using Kind, Minikube, etc.) in a pipeline VM (&quot;Hosted Agent&quot; in Azure DevOps terms).</li>
<li>Run integration tests.</li>
<li>Run E2E/Selenium tests.</li>
<li>If passing, push to Container Registry and merge feature branch to production.</li>
</ol>
<p>Which, unless I'm misinterpreting this, contradicts some other statements I've received like <a href="https://softwareengineering.stackexchange.com/a/425242/360359">this</a>:</p>
<blockquote>
<p><strong>Never, ever build in docker images</strong>, because it means we have to install dev-kits in the container. No docker container in production should have any of these kits. For security.</p>
</blockquote>
<p>I can see the value of both approaches.</p>
<p>I'm basically just looking for more clarification on this process regarding building, unit testing and Docker images. Suggestions?</p>

CC BY-SA 4.0
2021-07-21T23:15:38.470Z
I don't think you're going to get one single prospective in the community. Here is my view on modern principles: https://worklifenotes.com/2020/06/04/7-best-practices-modern-cicd/
taleodor
<continuous-integration><azure-pipelines><testing><integration><unit-testing>
Sorting out automated testing in CI pipeline and getting conflicting information related to building, unit testing and Docker images
cheslijones
# Sorting out automated testing in CI pipeline and getting conflicting information related to building, unit testing and Docker images

(Asked by: username_0 on 2021-07-20)

I seem to get a unique response for every person that I ask, or every article that I read, which is just confusing me even more.

Basically, I'm trying to setup automated testing in the CI pipeline which I'm pretty new to. The stack is basically these things:

* Django API service
* React Admin Portal service
* React Client Portal service
* PostgreSQL
* Redis
* Azure Kubernetes Services
* Azure DevOps Pipelines with Microsoft Hosted Agent

I was originally under the impression thanks to advice [here](https://softwareengineering.stackexchange.com/questions/425232/clarifying-the-steps-in-a-ci-cd-but-namely-if-if-unit-testing-should-be-done-bu), which was pretty consistent among respondents, that basically my CI pipeline should be the following:

1. Build the services.
2. Run unit tests.
3. If passing, `COPY` the build into the Docker image.
4. Push Docker image to Container Registry.
5. Deploy images to production-like Kubernetes (using Kind, Minikube, etc.) in a pipeline VM ("Hosted Agent" in Azure DevOps terms).
6. Run integration tests.
7. Run E2E/Selenium tests.
8. If passing, merge feature branch to production.

Started talking to someone else about it and they basically said:

> Unit > integration > e2e. Test as much as you can at each step without pissing the devs off. Testing in parallel is better because it lessens the time taken. If a failure in any test type means the pipeline breaks the only thing that controls when you run the test is if you can do it before the app is running (unit, functional) or after compile and deployment (integration/e2e).
>
> All of that aside: **DON'T COPY COMPILED CODE INTO A CONTAINER**. Such an anti-pattern and bad practice. The Dockerfile should include the entire build process and remain immutable once built. Copying code into your container is the equivalent of passing it to a coworker and assuming it's going to run on their laptop.
>
> Multi-stage builds and development containers containing sdks/build-kits/whatever are the first step, runtime containers that contain only the necessary pieces to run are the second. If they aren't available from the platform provider - .net, java, python etc. (they are) - you can build them both out of the same base container which standardizes both the build and runtime environments.
>
> Use multi-stage Dockerfiles and combine to reduce steps so that containers stay lean.

In other words:

1. Build the services in a Docker image.
2. Run unit tests in the the Docker image.
3. If passing, deploy images to production-like Kubernetes (using Kind, Minikube, etc.) in a pipeline VM ("Hosted Agent" in Azure DevOps terms).
4. Run integration tests.
5. Run E2E/Selenium tests.
6. If passing, push to Container Registry and merge feature branch to production.

Which, unless I'm misinterpreting this, contradicts some other statements I've received like [this](https://softwareengineering.stackexchange.com/a/425242/360359):

> **Never, ever build in docker images**, because it means we have to install dev-kits in the container. No docker container in production should have any of these kits. For security.

I can see the value of both approaches.

I'm basically just looking for more clarification on this process regarding building, unit testing and Docker images. Suggestions?

**Tags:** continuous-integration, azure-pipelines, testing, integration, unit-testing

> **Comments:**
> **username_1**: I don't think you're going to get one single prospective in the community. Here is my view on modern principles: https://worklifenotes.com/2020/06/04/7-best-practices-modern-cicd/  

---

## 1 answers

### Answer by username_2 on 2021-07-21. Score: 1

You've missed the part of the 1st quote about multi-stage builds and other methods which avoid exactly the problem mentioned in the 2nd quote: the resulting image would consist of it's base image and artifacts from previous stages while being run in the same *build* step of a pipeline.


---

