/
codedeploycontext.go
61 lines (50 loc) · 2.09 KB
/
codedeploycontext.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package deploy
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/codedeploy"
"github.com/aws/aws-sdk-go-v2/service/codedeploy/types"
"time"
)
func assembleCreateDeploymentInput(applicationName, deploymentGroupName string, appSpecJson []byte) *codedeploy.CreateDeploymentInput {
appSpecJsonHash := sha256.Sum256(appSpecJson)
return &codedeploy.CreateDeploymentInput{
ApplicationName: aws.String(applicationName),
DeploymentGroupName: aws.String(deploymentGroupName),
Revision: &types.RevisionLocation{
AppSpecContent: &types.AppSpecContent{
Content: aws.String(string(appSpecJson)),
Sha256: aws.String(fmt.Sprintf("%x", appSpecJsonHash)),
},
RevisionType: types.RevisionLocationTypeAppSpecContent,
},
}
}
type CodeDeployContext struct {
Client *codedeploy.Client
}
func (c *CodeDeployContext) CreateDeployment(ctx context.Context, applicationName, deploymentGroupName string, appSpec json.Marshaler) (string, error) {
appSpecJson, err := appSpec.MarshalJSON()
if err != nil {
return "", fmt.Errorf("cannot marshal JSON: %w", err)
}
deployment, err := c.Client.CreateDeployment(ctx, assembleCreateDeploymentInput(applicationName, deploymentGroupName, appSpecJson))
if err != nil {
return "", fmt.Errorf("cannot create deployment: %w", err)
}
return *deployment.DeploymentId, nil
}
func (c *CodeDeployContext) WaitForSuccessfulDeployment(ctx context.Context, deploymentID string, maxWaitDur time.Duration) error {
waiter := codedeploy.NewDeploymentSuccessfulWaiter(c.Client)
deployment := &codedeploy.GetDeploymentInput{DeploymentId: aws.String(deploymentID)}
if err := waiter.Wait(ctx, deployment, maxWaitDur); err != nil {
if output, getDeploymentErr := c.Client.GetDeployment(ctx, deployment); output != nil && output.DeploymentInfo != nil && output.DeploymentInfo.ErrorInformation != nil && output.DeploymentInfo.ErrorInformation.Message != nil && getDeploymentErr != nil {
return fmt.Errorf("%s (%w)", *output.DeploymentInfo.ErrorInformation.Message, err)
}
return err
}
return nil
}