Skip to content

Commit

Permalink
fix(deploy): use rollback target from jobmanager instead of retrievin…
Browse files Browse the repository at this point in the history
…g from db
  • Loading branch information
smrz2001 committed Nov 17, 2023
1 parent f50fc83 commit 5e1e9c3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
9 changes: 6 additions & 3 deletions cd/manager/common/aws/ddb/dynamoDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ type DynamoDb struct {
}

const defaultJobStateTtl = 2 * 7 * 24 * time.Hour // Two weeks
const buildTag = "sha_tag"

// buildState represents build/deploy tag information. This information is maintained in a legacy DynamoDB table used by
// our utility AWS Lambdas.
type buildState struct {
Key manager.DeployComponent `dynamodbav:"key"`
DeployTag string `dynamodbav:"deployTag"`
BuildInfo map[string]interface{} `dynamodbav:"buildInfo"`
BuildInfo buildInfo `dynamodbav:"buildInfo"`
}

type buildInfo struct {
BuildTag string `dynamodbav:"sha_tag"`
}

func NewDynamoDb(cfg aws.Config, cache manager.Cache) manager.Database {
Expand Down Expand Up @@ -368,7 +371,7 @@ func (db DynamoDb) GetBuildTags() (map[manager.DeployComponent]string, error) {
} else {
buildTags := make(map[manager.DeployComponent]string, len(buildStates))
for _, state := range buildStates {
buildTags[state.Key] = state.BuildInfo[buildTag].(string)
buildTags[state.Key] = state.BuildInfo.BuildTag
}
return buildTags, nil
}
Expand Down
12 changes: 5 additions & 7 deletions cd/manager/jobmanager/jobManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"runtime/debug"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -507,20 +508,17 @@ func (m *JobManager) postProcessJob(jobState job.JobState) {
if rollback, _ := jobState.Params[job.DeployJobParam_Rollback].(bool); !rollback {
if component, found := jobState.Params[job.DeployJobParam_Component].(string); !found {
log.Printf("postProcessJob: missing component (ceramic, ipfs, cas, casv5, rust-ceramic): %s", manager.PrintJob(jobState))
} else
// Get the latest deployed tag from the database. We're getting this from the build tags because
// the last successfully deployed tag would have been the same as the current build tag.
if buildTags, err := m.db.GetBuildTags(); err != nil {
log.Printf("postProcessJob: failed to retrieve build tags: %v, %s", err, manager.PrintJob(jobState))
} else if buildTag, found := buildTags[manager.DeployComponent(component)]; !found {
} else if deployTags, err := m.db.GetDeployTags(); err != nil { // Get latest deployed tag from database
log.Printf("postProcessJob: failed to retrieve deploy tags: %v, %s", err, manager.PrintJob(jobState))
} else if deployTag, found := deployTags[manager.DeployComponent(component)]; !found {
log.Printf("postProcessJob: missing component build tag: %s, %s", component, manager.PrintJob(jobState))
} else if _, err := m.NewJob(job.JobState{
Type: job.JobType_Deploy,
Params: map[string]interface{}{
job.DeployJobParam_Component: jobState.Params[job.DeployJobParam_Component],
job.DeployJobParam_Rollback: true,
job.DeployJobParam_Sha: job.DeployJobTarget_Rollback,
job.DeployJobParam_ShaTag: buildTag,
job.DeployJobParam_ShaTag: strings.Split(deployTag, ",")[0], // Strip deploy target
// No point in waiting for other jobs to complete before redeploying a working image
job.DeployJobParam_Force: true,
job.JobParam_Source: manager.ServiceName,
Expand Down
8 changes: 2 additions & 6 deletions cd/manager/jobs/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (d deployJob) Advance() (job.JobState, error) {
{
if deployTags, err := d.db.GetDeployTags(); err != nil {
return d.advance(job.JobStage_Failed, now, err)
} else if err = d.prepareJob(deployTags); err != nil {
} else if err = d.prepareJob(); err != nil {
return d.advance(job.JobStage_Failed, now, err)
} else if deployTag, found := d.state.Params[job.DeployJobParam_DeployTag].(string); found &&
!d.manual && !d.force &&
Expand Down Expand Up @@ -144,12 +144,8 @@ func (d deployJob) Advance() (job.JobState, error) {
}
}

func (d deployJob) prepareJob(deployTags map[manager.DeployComponent]string) error {
func (d deployJob) prepareJob() error {
deployTag := ""
if d.rollback {
// Use the latest successfully deployed tag when rolling back
deployTag = deployTags[d.component]
} else
// - If the specified deployment target is "latest", fetch the latest branch commit hash from GitHub.
// - Else if the specified deployment target is "release" or "rollback", use the specified tag.
// - Else if it's a valid hash, use it.
Expand Down

0 comments on commit 5e1e9c3

Please sign in to comment.