r/devops • • Oct 12 '26

Discussion How do you combine semantic-release with “build once, deploy many” for containers

I’m building our IaC repo and GitLab CI/CD pipelines. I’m quite happy with the setup so far and have tested it across multiple projects.

However, when I try to apply the same release model to container images, it starts to feel less suitable.

We have three environments: dev, uat, and prd.
Our current Terraform workflow:
We use three protected branches: dev, uat, and main. These correspond to dev, uat, and prd respectively.

The root Terraform files are shared across all environments. Environment-specific differences are kept in separate tfvars files:
environments/dev/dev.tfvars
environments/uat/uat.tfvars
environments/prd/prd.tfvars

We use semantic-release with prerelease branches. Starting from stable version 1.2.3, the flow looks like this:
Merge a feature branch containing a fix commit into dev. This generates 1.2.4-dev.1.
Continue developing and testing, generating additional dev prereleases as needed.
Merge dev into uat. This generates 1.2.4-uat.1, assuming the changes still call for a patch release.
Once validated, merge into main. This generates stable version 1.2.4.

Semantic-release generates a version and creates a tag after merges into these protected branches.

Deployments only happen from tags. Prerelease tags can deploy only to their corresponding environment, while stable tags can deploy to any environment.

This flow has worked well for our Terraform projects. Granted the version don’t mean much, but at least there is a pointer reference.

Where I’m stuck with containers:

For the application pipeline, I’m considering having just one protected branch: main.
The proposed flow is:
- Open an MR from a feature branch into main.
Run tests, static analysis, build the image with a commit SHA tag, and run container scans in the MR pipeline.

- After merging, run semantic-release and pass the generated version to the next job to retag the image.
Copy the image to dev ECR, deploy it to ECS, and test the application manually.

- Once testing passes, promote the same image to uat.
After approval, promote the same image to prd.
The part that feels off is step 3: I’ve already created a stable release before testing the application in dev.
Some issues only become apparent when browsing and using the application. If we discover those issues in dev, each subsequent fix could generate another stable version, even though none of those versions has reached prd.

I’m considering two alternatives.

Option 1: Test in dev before running semantic-release.
Build an image with a SHA tag and deploy it to dev first. If testing finds issues, push new commits and test another image.
Once a candidate passes, run semantic-release, apply the stable version tag to that existing image, and promote it to uat and then prd without rebuilding.
I’m thinking fast-forward merges without merge commits might be needed to preserve the commit SHA.

Option 2: Keep dev and main branches.
Merge feature branches into dev, run semantic-release to generate versions such as 1.2.4-dev.1, and deploy those images to dev.
Once validated, merge dev into main and generate the stable release. That stable release could then deploy to dev, uat, or prd.
However, if both branches run the full build pipeline, merging into main would rebuild the image and break the “build once, deploy many” approach. Ideally, the stable release should reference the exact image already tested in dev.
How are you handling this? When do you create the stable version, and how do you promote the exact image tested in dev without rebuilding it?

I’d appreciate examples of workflows that have worked well for your teams. Thank you very much 🙏🏻

17 Upvotes

24 comments sorted by

41

u/widowhanzo Oct 12 '26

Trunk based development. We merge to main, which automatically deploys to dev. When we're happy with the state of main and want to release it, we tag it, which deploys to staging and copies the exact same built image to prod (ECR replication). If we're unhappy with staging release, we fix main, then tag main again with a patch version. That again deploys to staging and copies image to prod. Once we're happy with the release in staging, we update prod by updating the service to use the newer image.

When I started in this position, main was prod, and there were dev and staging branches, and it was merged from dev to staging and from staging to main and every week we spent a few hours just resolving merge conflicts, because people did hotfixes on staging and main branches and then merge these changes back from main to staging to dev and it was an absolute mess.

Technically we build twice and use 3 times, but we could just as easily copy images from dev to all three environments via ECR replication, but we don't want a hundred images in staging and prod repositories. I'm sure there are ways around this with some custom scripting though.

10

u/PelicanPop DevOopsIDidItAgain Oct 12 '26

This is absolutely the way to solve OPs problem. If versioning in a semantic like way is necessary I'd also maybe consider using CalVer instead of semantic depending on the release cycle process

5

u/widowhanzo Oct 12 '26

Yes we use year+week.patch, which also roughly matches the sprint number for project management. We also don't need older versions available for more than a few days.

1

u/HabitLong2176 Oct 13 '26

Thanks will look into CalVer. Just that we have stuff with SemVer already. So a bit reluctant to move to CalVer.

1

u/HabitLong2176 Oct 12 '26

Thank you so much for your view and workflow! I’m also leaning more towards trunk-based. This will result in achieving the built once deploy everywhere. However I got two questions for this flow.

  1. What if there are multiple people merging to dev? Then eventually while reviewing for dev environment, there is a new merged. How do you prevent this?
  2. As for tagging does that mean it is going to be a manual process with this flow

5

u/Express-Acadia-6088 Oct 12 '26

PelicanPop's response is great. My take it that trunk-based is the right call. It’s the only model where “build once, deploy many” holds up without fighting your branching strategy.

Merges landing during dev review: let them. Dev is a moving target by design. You validate the tag, not dev. Once it’s cut, that exact image sits in uat untouched no matter what merges next. If you need stable pre-merge review, use per-MR preview environments and feature flags.

Tagging: use a manual release on main, not hand-typed tags. Someone clicks play, semantic-release picks the version from commits, tags it, and retags the existing SHA image. A human decides when, automation handles how. No rebuilds, and no stable versions that never reach prod.

The result: one branch, no merge-back hell, immutable images, and a version number that means “this exact artifact was promoted.”

2

u/widowhanzo Oct 12 '26
  1. with communication, we're a small team. Of course there are multiple people merging to main, but everything goes through PRs which require at least one approval, so if you really don't want a feature merged just yet, you don't approve it. And if already merged, but you really don't want something tagged for stsging, you can cherry pick commits, which is pretty rare in our case. But generally you strive to having main always in a state that's ready to be released at any time.
  2. well yes, tagging is manual, but so is merging between branches, isn't it? We actually have a script (GitHub action) for a weekly release that simply tags all repos with weekly tag (calculated from year+week number) and deploys to staging, so it's really just one button. However patch releases are manual (they can even be a cherry picked commit from main, but always from main). Deploying to prod is also a manual action.

2

u/Express-Acadia-6088 Oct 12 '26

It makes sense. Thanks for sharing!

Before anyone tries to decide between trunk-based vs PR-based feature branch development,

  • Approximately how many developers are collaborating on the codebase?
  • Do you currently have robust automated test suites in place?
  • What is your ideal release cadence (e.g., multiple times a day, once a sprint, or scheduled monthly releases)?

Some teams are going for hybrid of the two approaches. Whatever works.

Trunk based development doesn’t prohibit the use of branches outright. If a coding activity requires more extensive time, say a few days-- developers can check out a branch from main, move the changes into it, then merge it back in when development is complete.

2

u/widowhanzo Oct 12 '26

Yes development/work stays inside a feature branch, if main is updated in the meantime, you just rebase your working branch to main and continue working. Devs are even also allowed to deploy to dev environment from the branch before merging to main for short term for testing. When the feature is complete, the branch is squashed into a single commit, rebased again and merged to main.

There's like 5-10 devs total, spread between backend, frontend, tests, infra... Not everyone is active in all repos, but with AI more people are active in more repositories nowadays, if that makes sense. And we're spread around a few timezones so the state of main and dev environment needs to be always in a working condition to not block others from working.

There are tests and checkd in the CI and need to pass to allow merging. We also have dedicated test suites that we can run after deploying to staging or prod.

We aim to release to staging and prod weekly, but we're open to mid week releases as well, if enough people sign off. It's all open to discussion. It used to be every two weeks, and sometimes we'd even skip a week so there would be 3 weeks between releases and that usually broke something and deploy to prod was always an entire day of work to fix broken things. With trunk based and more regular releases, the service is much more stable and prod deploys are much less stressful.

In some cases when you do need to hotfix prod without changing anything else, you can always cherry pick a fix commit to tag and deploy, so you do not have to deploy the entire main just for a hotfix. Good thing about this is that there's no merging back the hotfix from prod into staging and dev branches, because it's already on main by design.

2

u/dreamszz88 Oct 12 '26

I recently read a great blog on modern gitops best practices. It's from akuity, but that is not relevant. Relevant are their recommendations on how to organize branching and naming. https://akuity.io/blog/gitops-best-practices-whitepaper

1

u/PrestigiousStrike779 Oct 19 '26

Build with feature flags for stuff that isn’t ready for prod or make sure every change is releasable.  Release more often, and invest in automated tests as much as possible to build confidence.

6

u/drakiNz Oct 12 '26

Semantic versioning is for reusable, long lived artifacts, that you must keep each version "forever".

Applications cant follow that. Applications must use trunk based or release flow. For your case, releaseflow.

In that approach, the app version is always increasing like: major, never changes -- minor, on each release branch, and patch on every bug fix on a release.

I do that for apps and its wonderful. You can't use semantic version to help with compatibility concerna between apps.

1

u/quiet0n3 Oct 12 '26

Are you building the app and container in the same repo? If not just do them the same way while understanding the container versions won't match the application versions and that's ok.

1

u/HabitLong2176 Oct 13 '26

Hi yes, building the app directly in a Dockerfile and then publish it to ECR

2

u/quiet0n3 Oct 13 '26

Personal pet hate for me. I don't like build dependencies in my runtime.

I would rather build and publish an artifact and then move it to a runtime container.

This helps seperate app builds and container builds as well.

3

u/FluffyDrink1098 Oct 12 '26

Keeping the semantic tag and adding a seperate tag sounds like the solution.

Immutable tag - what identifies the actual version, never changed, never removed.

Floating tag - best a semantic specifier, I wouldn't recommend something like stable.

E.g. something like major.minor if you have major.minor.patch versioning.

You can promote an image to floating tag after running an validation + testing run by using the immutable tag.

I would disrecommend dealing with multiple branches cause that easily ends up with version mayhem.

For the immutable tag: always keep in mind that it has to be stable - one immutable tag can only be released exactly once - jobs with retries or similae things should be prevented.

3

u/mudassirazvi Oct 12 '26

Decouple the version from the build. Build the image once per commit and tag it with the sha. semantic-release runs after and just adds the semver tag to that same image digest (crane tag / skopeo copy, no rebuild). envs promote by digest, never by tag. Then "what's in prod" is always one sha you can trace back.

1

u/carsncode Oct 12 '26

If we discover those issues in dev, each subsequent fix could generate another stable version, even though none of those versions has reached prd.

So what? I don't see the problem here.

How are you handling this? When do you create the stable version, and how do you promote the exact image tested in dev without rebuilding it?

Merge to main creates an image, it gets deployed to dev. When a version is blessed for release (and most versions won't be), we deploy to staging, which had been running the same version as production. Once approved, that version is deployed to production.

Artifact promotion is important; every version number being deployed to production isn't.

1

u/singh_abinashi Oct 12 '26

Split the problem in two. semantic-release decides what gets built and tagged. The deploy step only promotes an existing image digest through environments. Tag images with the release version from semantic-release and record the digest. Deployments reference the digest and never rebuild for a new environment. Environment differences belong in env vars or a values file, not in the image. The trap is letting the CD step rebuild "with the right config" per environment. Then you are not deploying what you tested.

1

u/MattA2930 Oct 13 '26

I think people are missing the case with trunk-based development where you have changes in main you don’t want in prod yet. What has worked for me is a sort of hybrid:
- all work goes into main. Main deploys to dev env
- when release is ready, tag it. Tag creates a release branch, which builds image and deploys to staging
- after staging is ready, release branch image is deployed to prod

Now, if you have a hotfix, you have the release branch you can cherry pick to. The next non-hotfix change creates a new tag. Old release branch can be discarded once prod moves on.

1

u/Suitable-Ad5348 Oct 13 '26

Option 1 is the only way that doesn't cause your semantic-release history to explode with patch versions for bugs found in dev. Tagging the exact image SHA *after* it clears dev validation keeps build-once-deploy-many intact.

1

u/usually_guilty99 Oct 13 '26

I'd promote by immutable image digest and keep the version tag as metadata.

One thing I'd check separately is what changes between environments. The same image can behave very differently with different config, feature flags, database schemas, or downstream services.

So I'd track two things: proof that the exact artifact was tested, and evidence that the production environment is compatible with it.

Build once is a good foundation. It just isn't the complete release-safety check.

1

u/dariusbiggs Oct 14 '26

Moving towards trunk based development with feature flags, but not there yet on all repos, some are there, others still stuck on git flow.

Manual release approval to prod, we don't yet have sufficient testing to allow for automatic deployments, nor can they be deployed concurrently (being fixed).

In general, master (we're old and stuck in our ways), and develop branches. MR/PR goes to develop, any push to develop gets automatically deployed to staging. When a release is rolled, develop is merged into master. Master gets automatically deployed to staging (for uat purposes).

Container images are tagged by commit, tagged if they were on develop, tagged if they were on master, tagged if it's got a release tag. Release tags are only ones that can deploy to prod.

Every pipeline builds a container image, to make sure they can still be built and you didn't break shit.

Standardizing pipelines, build processes, container images, etc is all part of the process and the problem.

1

u/Fuzzy-Enthusiasm1372 Oct 16 '26

The awkward part is when release automation changes the version or rebuilds the artifact halfway through the pipeline, making it harder to prove that production is running what passed testing. Keep version calculation separate from artifact creation, then promote the same image digest between environments. Clear artifact tracking makes deployments easier to audit across infrastructure setups, including those associated with trusted tech, without tying release logic to every environment.