Release rollback planner
You build a Go release rollback planner that validates image references, pins a selected Compose service to a commit-SHA tag, and returns the previous distinct deployed release.
Ticket
You build a Go package named release that parses container image references, updates one service's image tag in a Compose file, and selects the previous distinct deployed tag from release history.
The planner helps you identify a known earlier release when a deployment introduces a regression. It uses commit-SHA tags instead of mutable identifiers such as latest. This is an independent optional extension. It does not deploy containers or modify the running application.
The tests call these exported declarations:
package release
type Image struct {
Registry, Repo, Tag string
}
func (i Image) String() string
func ParseImage(ref string) (Image, error)
func Pin(compose, service, tag string) (string, error)
func Rollback(history []string, current string) (string, error)Acceptance criteria
- Parsing
ghcr.io/yashdagar/zomato-api:a31f9c2returns anImagewithRegistry: "ghcr.io",Repo: "yashdagar/zomato-api"andTag: "a31f9c2". CallingString()reconstructsghcr.io/yashdagar/zomato-api:a31f9c2. ParseImagerejects empty references, untagged references,latest,v1, uppercase tags such asA31F9C2, six-character tags such asa31f9c, unsupported registries such asdocker.io, and repositories without exactly an owner and a name, such asghcr.io/zomato-api.- A lowercase hexadecimal tag containing 40 characters is accepted. A tag containing 41 characters is rejected.
- Calling
Pin(compose, "api", "b42e8d1")replaceszomato-api:a31f9c2withzomato-api:b42e8d1. All unrelated Compose content remains unchanged. - Calling
Pin(compose, "worker", "c53f9a2")replaces the interpolated image tag${WORKER_TAG:-local}withc53f9a2. - Calling
Pin(compose, "api-docs", "1111111")updates theapi-docsimage tozomato-api-docs:1111111and leaves theapiimagezomato-api:a31f9c2unchanged. - Pinning an unknown service such as
caddyreturns an error. Pinning a service such asmysqlwith an image outsideghcr.io/yashdagar/also returns an error. Pinrejects replacement tagslatest,v2,B42E8D1,b42e8dand an empty tag.- Given history
["a31f9c2", "b42e8d1", "c53f9a2"],Rollbackreturnsb42e8d1when the current tag isc53f9a2, anda31f9c2when the current tag isb42e8d1. - Given history
["a31f9c2", "b42e8d1", "b42e8d1", "c53f9a2"],Rollbackreturnsb42e8d1for current tagc53f9a2. Given history["a31f9c2", "b42e8d1", "b42e8d1"], it returnsa31f9c2for current tagb42e8d1. Rollbackreturns an error when history is empty, when the current tag is absent from history, or when no earlier distinct release exists. These cases include historiesnil,["a31f9c2", "b42e8d1"]with currentffffff0, and["a31f9c2"]with currenta31f9c2.- Calling
Rollbackdoes not modify the supplied history slice. The history["a31f9c2", "b42e8d1", "c53f9a2"]remains unchanged after selecting a rollback target.
Constraints
- Do not modify the provided tests.
Design direction
Keep the implementation self-contained in the release package. A natural location is backend/internal/release/release.go. Any layout that meets the contract passes.
Consider these design choices:
- Use one SHA tag validation rule for both image parsing and Compose pinning. This prevents the two operations from accepting different definitions of a valid release tag.
- Treat image parsing and Compose editing as separate operations. Parsing validates a reference, while pinning modifies only the selected service's image line.
- Preserve the original Compose text instead of parsing and serializing the entire YAML document. A serializer can change unrelated whitespace or line endings.
- Keep rollback selection as a pure operation over the supplied history. Searching from the newest end makes it easier to locate the current deployment and skip consecutive redeployments.
The planner should use ordinary Go values and return errors for invalid input. Use error wrapping to provide useful context when operations fail.
How to check your work
Run the optional exercise tests from the backend directory:
cd backend
make test W=7O3The runner applies the w7o3 build tag automatically. Read the first failing assertion and identify whether it concerns image validation, Compose text preservation, exact service matching or rollback history selection.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Release safety extensions
4 items
- 7.15Release checks and gRPC recoverynot completed
- 7.16Filesystem secret scannernot completed
- 7.17gRPC resilience interceptorsnot completed
- 7.18Release rollback plannernot completed
Week 7 done · Up next · 5 modules
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
ImageandParseImagevalidate and reconstruct image references.Pinupdates only the selected service's image tag.- Interpolated Compose image tags are handled correctly.
Rollbackreturns the previous distinct deployed tag.- Invalid references and missing rollback targets return errors.
make test W=7O3passes all 12 tests.
Commands
cd backend && make test W=7O3Tests
TestW7O3_ParseImageSplitsRegistryRepoAndTaginbackend/internal/release/release_test.goTestW7O3_ParseImageRejectsRefsThatCannotBeRolledBackToinbackend/internal/release/release_test.goTestW7O3_ParseImageAcceptsFullLengthShaTagsinbackend/internal/release/release_test.goTestW7O3_PinRewritesOnlyTheNamedServicesImageLineinbackend/internal/release/release_test.goTestW7O3_PinReplacesAnInterpolatedTaginbackend/internal/release/release_test.goTestW7O3_PinDoesNotConfuseServicesWithACommonPrefixinbackend/internal/release/release_test.goTestW7O3_PinRejectsUnknownServicesAndImagesOutsideOurRegistryinbackend/internal/release/release_test.goTestW7O3_PinRejectsTagsThatAreNotCommitShasinbackend/internal/release/release_test.goTestW7O3_RollbackReturnsThePreviousDeployedTaginbackend/internal/release/release_test.goTestW7O3_RollbackSkipsRedeploysOfTheSameTaginbackend/internal/release/release_test.goTestW7O3_RollbackFailsWithoutAPreviousReleaseinbackend/internal/release/release_test.goTestW7O3_RollbackDoesNotMutateTheHistoryinbackend/internal/release/release_test.go