r/devops • u/No_Pain_1586 • 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?
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.
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.
2
u/xiongchiamiov Site Reliability Engineer Nov 14 '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.
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/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.