r/devops • • Nov 14 '23

Is there anyway to avoid rebuilding the same images after a pull request?

For example, I have a CI build pipeline on three branches: dev, test and prod.

dev will be regularly pushed into and built an image on every new push, tagged with its commit hash and dev.

test will get pull request from dev, and merged.

prod will get pull request from test, and merged.

The thing is the image is already built (and tested) in the dev branches, I want to avoid rebuilding the same image and get that same image and tagged again with test. That goes the same with production build too.

The problem is a pull request on a branch always create a new hash, so there's no way for me to detect if it's the same commit (hence the same codebase). Is there anyway to detect if the codebase merged to branch test already been built in branch dev, so I can avoid a rebuild to make faster pull request?

4 Upvotes

28 comments sorted by

15

u/bilingual-german Nov 14 '23

Docker uses a cache to increase build speed. You just need to have one or more recent images on the same machine. Just by pulling the recently built images before building it should be possible to speed up the process.

1

u/No_Pain_1586 Nov 14 '23

If I'm always using the same machine, does all images would be in the cache already provided there is enough ram? This sounds like I should put all my CI runners and servers in the same machine, so I could utilize docker cache and pnpm installations. There's no need for highly available CI runners in different subnets.

4

u/[deleted] Nov 14 '23

Also check the order of your steps, because if a layer is changed everything built on top of it will be recreated.

2

u/No_Pain_1586 Nov 15 '23

yeah I had a RUN pnpm install after a COPY src, which cause it too takes more time that it is, (17s rebuild to 1.1s)

1

u/bilingual-german Nov 14 '23

The docker cache has nothing to do with RAM.

Like the other comment says: the cache works layer by layer and if one layer is rebuild, all following layers are rebuild too. So you could optimise by sorting your build file to do the infrequent changes in the first layers and the most frequent changes in the other layers. E.g. adding package.json in one step, installing everything with npm, then adding your code.

here is a write up https://docs.docker.com/build/cache/

4

u/Ausmith1 Nov 14 '23

I'm assuming you are using Kubernetes here. If so read up on image digests.

Essentially the image digest is immutable and if you use the image digest to pull the image instead of the image tag you can reuse the same image.

You can still put the tag into the manifest but it gets ignored if you put the digest in.

1

u/No_Pain_1586 Nov 14 '23

I still don't get how I would use it. If I make a pull request, I trigger a build for that request. After the build is success, I will attempt a merge. If a merge is complete, which means the branch was pushed, that triggers a pipeline but I have no idea if an image of that same codebase exists already.

For example:

- features/new => PR created => dev -> this is detected as a PR event with a source branch so I build an image tagged with that source branch name.

- features/new => PR merged => dev -> this merge is detected as a Push event without a source branch, so I don't know where this push originally came from to duplicate the same image on that PR.

fyi, I'm using Drone CI

1

u/Ausmith1 Nov 14 '23

First question here that I don't see that you have answered is: Are you deploying to Kubernetes?

1

u/No_Pain_1586 Nov 15 '23

yes

1

u/Ausmith1 Nov 15 '23

As others have mentioned you need a tool (such as the changed-files GitHub Action) to check if files have changed and in such a case you just reuse the prior image.

2

u/xiongchiamiov Site Reliability Engineer Nov 14 '23

The problem is a pull request on a branch always create a new hash, so there's no way for me to detect if it's the same commit (hence the same codebase).

If it's actually the same commit, the commit hash will be the same. Find a way to make it be the same commit.

0

u/No_Pain_1586 Nov 15 '23

If it's actually the same commit, the commit hash will be the same. Find a way to make it be the same commit.

I doubt this is true, the hash is created based on different information like branch name and created time, so it's gonna be different, at least that's how it works on Github.

2

u/JustMy10Bits Nov 15 '23

If it's not the same commit hash then you're not building the same image.

If the only thing that's different is the branch then you need to reexamine your branching strategy.

1

u/xiongchiamiov Site Reliability Engineer Nov 16 '23

My point is that if you're committing multiple times on different branches, those aren't the same commit and therefore shouldn't be sharing the same built image. What you would want to do is change the workflow such that you deploy from pre-existing commits rather than cherry-picking or whatever you're doing.

6

u/Ariquitaun Nov 14 '23

Google "docker build promotion".

3

u/thomas_michaud Nov 15 '23

Typically you want a binary storage system to store jars, war, exe, and compiled code. (Nexus Repository Manager or JFrog Artifactory)

So, if you cut a r1.4.2 from dev...when you go to test you pull the prebuilt r1.4.2 instead of rebuilding. (Definitely the case if you have a QA dept that tests the code...that code and ONLY that code goes to prod regardless of what may be built)

Some of this involves the difference between a release and a nightly (engineering/snapshot) build.

2

u/will592 Nov 15 '23

This is what I was looking for. I see more and more people building at every stage instead of cutting a release and promoting it as an intact artifact.

1

u/Old-Ad-3268 Nov 16 '23

I'm glad someone said it, my eye was twitching reading the op

2

u/comportsItself Nov 15 '23

The issue is the merge commit that gets created when merging a PR.

You could use something like the changed-files GitHub Action to see if any files have changed since the preceding commit, and if there are no files changed, then just use the previous commit SHA as the image tag, which should already be pushed to your Docker repo.

2

u/kkapelon Nov 15 '23

You should build an image once and then promote it to other envs.

Check antipattern 5 here https://codefresh.io/blog/docker-anti-patterns/

3

u/redkoil Nov 14 '23 edited Mar 03 '24

I enjoy playing video games.

1

u/SiurbliuMeistrs Nov 14 '23

Lots of ways to do that depending on the code management workflow. F.e you can tag good dev commit with a tag of test-1.1 and have test pipeline add tag to the existing image. Same could be applied to then add tag to good tested image with prod-1.1 etc. Then on each env deploy based on tag.

1

u/Live-Box-5048 DevOps Nov 14 '23

You can either create some sort of tag management, and pull them based on that, or try multi-stage docker builds as well.

1

u/cnelsonsic Nov 14 '23

If it's re-building on the same host, and the code hasn't changed, the "build" should take almost zero time. Check what steps in your dockerfile are invalidating the cache. This is usually people copying their code first and then installing dependencies, which breaks the cache. Some people just copy everything in the directory so any time anything changes, their image rebuilds. It's fine for development, but the more specific you can make it, the better tuned your build will be.

Once you have the builds caching correctly, don't be afraid of making a tag for every commit, they're just names for the same cached image.

0

u/soundwave_rk Nov 14 '23

Forcing merges with fast forward only will not change the hash if that helps you.

-1

u/hard_KOrr Nov 14 '23

How much time would you really save by not rebuilding?

There are potential situations where you can get yourself in trouble by not rebuilding in test and then prod, so I would be wary of doing that. A hotfix into prod that didn’t get into dev and test would get wiped if a new push came through from dev for example.

1

u/No_Pain_1586 Nov 14 '23

It would use less resources, and drilling up update would be way faster if I have a long pull request pipeline from dev -> test -> staging -> prod. I understand that it's likely pre optimized and wasn't really that neccessary though.

1

u/JustMy10Bits Nov 15 '23

More dangerous is a rebuild before going into prod if you're relying on your test results in test.