CI pipelines, concurrency and build results possibly overwriting each-other I'm currently using gitlab-ci to generate docker images for some repositories, however i dont feel as though this solution is as reliable as it should be.

I can forsee a issue with multiple builds happening close together and build #100 overwriting the newly built docker image produced by build #101, if for whatever reason jobs dont finish sequentially (eg: multiple build hosts, etc...).

Currently the tags i'm hoping to use follow this pattern:
namespace/application:branch-name

The crux of the question is as follows:


How do other services solve this problem? (presumibly running builds sequentially per branch?)
How should this issue be resolved in gitlab environments in a totally watertight manner


I understand this problem would go away if i use the SHA of the commit for the build tag, but i'd like to stick to the branch name for the following reasons:


orphaned / old images will be grabage collected by the docker registry
branch name is easier to use
I am using AzureDevOps and the trick we use when naming containers is to add build number to the label

namespace/application:branch-name-bulidNumber


So in your case, you could try gitlab variable CI_CONCURRENT_PROJECT_ID

After comment

for images clean-up you can use:

  docker image prune --all --filter until=48h 


As docker system prune and docker image prune have the until filter. So docker image prune --all --filter until=48h would remove all (not just dangling) images that were created more than 48 hours ago. Hopefully that helps. source here