-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (71 loc) · 2.67 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
)
func main() {
ECS_TASK_DEFINITION := os.Getenv("ECS_TASK_DEFINITION")
REGION := os.Getenv("REGION")
IMAGE_URI := os.Getenv("IMAGE_URI")
accessKeyID := os.Getenv("AWS_ACCESS_KEY_ID")
secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
ecsCluster := os.Getenv("ECS_CLUSTER")
ecsService := os.Getenv("ECS_SERVICE")
sess, err := session.NewSession(&aws.Config{
Region: aws.String(REGION),
Credentials: credentials.NewStaticCredentials(accessKeyID, secretAccessKey, ""),
})
svc := ecs.New(sess)
resp, err := svc.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
TaskDefinition: aws.String(ECS_TASK_DEFINITION),
})
if err != nil {
fmt.Println("Error", err)
os.Exit(1)
}
// check if TaskDefinition is nil
if resp.TaskDefinition == nil {
fmt.Println("Error: TaskDefinition is nil")
os.Exit(1)
}
// // Updating the image in container definitions
for i := range resp.TaskDefinition.ContainerDefinitions {
resp.TaskDefinition.ContainerDefinitions[i].Image = aws.String(fmt.Sprintf(IMAGE_URI))
}
newTaskDefinition, err := svc.RegisterTaskDefinition(&ecs.RegisterTaskDefinitionInput{
ContainerDefinitions: resp.TaskDefinition.ContainerDefinitions,
Cpu: resp.TaskDefinition.Cpu,
ExecutionRoleArn: resp.TaskDefinition.ExecutionRoleArn,
Family: resp.TaskDefinition.Family,
IpcMode: resp.TaskDefinition.IpcMode,
Memory: resp.TaskDefinition.Memory,
NetworkMode: resp.TaskDefinition.NetworkMode,
PlacementConstraints: resp.TaskDefinition.PlacementConstraints,
ProxyConfiguration: resp.TaskDefinition.ProxyConfiguration,
RequiresCompatibilities: resp.TaskDefinition.RequiresCompatibilities,
TaskRoleArn: resp.TaskDefinition.TaskRoleArn,
Volumes: resp.TaskDefinition.Volumes,
RuntimePlatform: resp.TaskDefinition.RuntimePlatform,
})
if err != nil {
fmt.Println("Error", err)
os.Exit(1)
}
updateServiceInput := &ecs.UpdateServiceInput{
Cluster: aws.String(ecsCluster),
Service: aws.String(ecsService),
TaskDefinition: aws.String(*newTaskDefinition.TaskDefinition.TaskDefinitionArn),
}
// Call UpdateService
_, err = svc.UpdateService(updateServiceInput)
if err != nil {
fmt.Println("Error updating service ", err)
os.Exit(1)
}
fmt.Println(*newTaskDefinition.TaskDefinition.TaskDefinitionArn)
os.Setenv("GITHUB_OUTPUT", "taskARN="+string(*newTaskDefinition.TaskDefinition.TaskDefinitionArn))
}