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:

go
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:a31f9c2 returns an Image with Registry: "ghcr.io", Repo: "yashdagar/zomato-api" and Tag: "a31f9c2". Calling String() reconstructs ghcr.io/yashdagar/zomato-api:a31f9c2.
  • ParseImage rejects empty references, untagged references, latest, v1, uppercase tags such as A31F9C2, six-character tags such as a31f9c, unsupported registries such as docker.io, and repositories without exactly an owner and a name, such as ghcr.io/zomato-api.
  • A lowercase hexadecimal tag containing 40 characters is accepted. A tag containing 41 characters is rejected.
  • Calling Pin(compose, "api", "b42e8d1") replaces zomato-api:a31f9c2 with zomato-api:b42e8d1. All unrelated Compose content remains unchanged.
  • Calling Pin(compose, "worker", "c53f9a2") replaces the interpolated image tag ${WORKER_TAG:-local} with c53f9a2.
  • Calling Pin(compose, "api-docs", "1111111") updates the api-docs image to zomato-api-docs:1111111 and leaves the api image zomato-api:a31f9c2 unchanged.
  • Pinning an unknown service such as caddy returns an error. Pinning a service such as mysql with an image outside ghcr.io/yashdagar/ also returns an error.
  • Pin rejects replacement tags latest, v2, B42E8D1, b42e8d and an empty tag.
  • Given history ["a31f9c2", "b42e8d1", "c53f9a2"], Rollback returns b42e8d1 when the current tag is c53f9a2, and a31f9c2 when the current tag is b42e8d1.
  • Given history ["a31f9c2", "b42e8d1", "b42e8d1", "c53f9a2"], Rollback returns b42e8d1 for current tag c53f9a2. Given history ["a31f9c2", "b42e8d1", "b42e8d1"], it returns a31f9c2 for current tag b42e8d1.
  • Rollback returns an error when history is empty, when the current tag is absent from history, or when no earlier distinct release exists. These cases include histories nil, ["a31f9c2", "b42e8d1"] with current ffffff0, and ["a31f9c2"] with current a31f9c2.
  • Calling Rollback does 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:

bash
cd backend
make test W=7O3

The 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.

Finished Exercise 7.18?

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 8 · Advanced features and project documentation

Week 7 done · Up next · 5 modules

Open week 8

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Image and ParseImage validate and reconstruct image references.
  2. Pin updates only the selected service's image tag.
  3. Interpolated Compose image tags are handled correctly.
  4. Rollback returns the previous distinct deployed tag.
  5. Invalid references and missing rollback targets return errors.
  6. make test W=7O3 passes all 12 tests.

Commands

terminal
cd backend && make test W=7O3

Tests

  • TestW7O3_ParseImageSplitsRegistryRepoAndTag in backend/internal/release/release_test.go
  • TestW7O3_ParseImageRejectsRefsThatCannotBeRolledBackTo in backend/internal/release/release_test.go
  • TestW7O3_ParseImageAcceptsFullLengthShaTags in backend/internal/release/release_test.go
  • TestW7O3_PinRewritesOnlyTheNamedServicesImageLine in backend/internal/release/release_test.go
  • TestW7O3_PinReplacesAnInterpolatedTag in backend/internal/release/release_test.go
  • TestW7O3_PinDoesNotConfuseServicesWithACommonPrefix in backend/internal/release/release_test.go
  • TestW7O3_PinRejectsUnknownServicesAndImagesOutsideOurRegistry in backend/internal/release/release_test.go
  • TestW7O3_PinRejectsTagsThatAreNotCommitShas in backend/internal/release/release_test.go
  • TestW7O3_RollbackReturnsThePreviousDeployedTag in backend/internal/release/release_test.go
  • TestW7O3_RollbackSkipsRedeploysOfTheSameTag in backend/internal/release/release_test.go
  • TestW7O3_RollbackFailsWithoutAPreviousRelease in backend/internal/release/release_test.go
  • TestW7O3_RollbackDoesNotMutateTheHistory in backend/internal/release/release_test.go