github.com/kolanos/fargate@v0.2.3/cmd/service_deploy.go (about) 1 package cmd 2 3 import ( 4 "github.com/jpignata/fargate/console" 5 "github.com/jpignata/fargate/docker" 6 ECR "github.com/jpignata/fargate/ecr" 7 ECS "github.com/jpignata/fargate/ecs" 8 "github.com/jpignata/fargate/git" 9 "github.com/spf13/cobra" 10 ) 11 12 type ServiceDeployOperation struct { 13 ServiceName string 14 Image string 15 } 16 17 var flagServiceDeployImage string 18 19 var serviceDeployCmd = &cobra.Command{ 20 Use: "deploy <service-name>", 21 Short: "Deploy new image to service", 22 Long: `Deploy new image to service 23 24 The Docker container image to use in the service can be optionally specified 25 via the --image flag. If not specified, fargate will build a new Docker 26 container image from the current working directory and push it to Amazon ECR in 27 a repository named for the task group. If the current working directory is a 28 git repository, the container image will be tagged with the short ref of the 29 HEAD commit. If not, a timestamp in the format of YYYYMMDDHHMMSS will be used.`, 30 Args: cobra.ExactArgs(1), 31 Run: func(cmd *cobra.Command, args []string) { 32 operation := &ServiceDeployOperation{ 33 ServiceName: args[0], 34 Image: flagServiceDeployImage, 35 } 36 37 deployService(operation) 38 }, 39 } 40 41 func init() { 42 serviceDeployCmd.Flags().StringVarP(&flagServiceDeployImage, "image", "i", "", "Docker image to run in the service; if omitted Fargate will build an image from the Dockerfile in the current directory") 43 44 serviceCmd.AddCommand(serviceDeployCmd) 45 } 46 47 func deployService(operation *ServiceDeployOperation) { 48 ecs := ECS.New(sess, clusterName) 49 service := ecs.DescribeService(operation.ServiceName) 50 51 if operation.Image == "" { 52 var tag string 53 54 ecr := ECR.New(sess) 55 repositoryUri := ecr.GetRepositoryUri(operation.ServiceName) 56 repository := docker.Repository{Uri: repositoryUri} 57 username, password := ecr.GetUsernameAndPassword() 58 59 if git.IsCwdGitRepo() { 60 tag = git.GetShortSha() 61 } else { 62 tag = docker.GenerateTag() 63 } 64 65 repository.Login(username, password) 66 repository.Build(tag) 67 repository.Push(tag) 68 69 operation.Image = repository.UriFor(tag) 70 } 71 72 taskDefinitionArn := ecs.UpdateTaskDefinitionImage(service.TaskDefinitionArn, operation.Image) 73 ecs.UpdateServiceTaskDefinition(operation.ServiceName, taskDefinitionArn) 74 console.Info("Deployed %s to service %s", operation.Image, operation.ServiceName) 75 }