diff --git a/README.md b/README.md index 67d2d77..ba5880a 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ For each service you must provide the following values: - `dockerfile`: (optional) the path to dockerfile to use for the service, relative to the `directory` of the service - `scripts`: (optional) an object containing global scripts override. It is possibile to override only the bundle script - `bundle` is ran right before the docker build, and receives in input the service name. It is executed instead of global bundle script. The path is relative to the service directory and it is executed in the service directory +- `image_tag_parameter`: (optional) it's possible to override the argo parameter for the image tag (default is `image.tag`) If all your services use the same dockerfile, you can specify it inside the `docker` key, as `dockerfile`. This must be a path relative to the root directory of your project. @@ -98,6 +99,7 @@ This must be a path relative to the root directory of your project. ### Complete configuration example A complete configuration looks something like this: + ```json { "default_environment": "dev", @@ -131,6 +133,16 @@ A complete configuration looks something like this: "directory": "./services/street-corners/", "service_name": "street-corners", "image_name": "street-corners" + }, + "my-other-service": { + "directory": "./services/my-other-service/", + "service_name": "my-other-service", + "image_name": "my-other-service", + "dockerfile": "./my-other-service.Dockerfile", + "scripts": { + "bundle": "./my-other-service-bundle.sh" + }, + "image_tag_parameter": "image.myservice.tag" } } } diff --git a/argo/deploy.go b/argo/deploy.go index f2b7145..58cd533 100644 --- a/argo/deploy.go +++ b/argo/deploy.go @@ -17,11 +17,11 @@ type resourceGroup string type resourceKind string const ( - imageTagParameter parameterName = "image.tag" - defaultImageTag = "latest" - deploymentResource resourceKind = "Deployment" - statefulSetResource resourceKind = "StatefulSet" - appResourceGroup resourceGroup = "apps" + defaultImageTagParameter parameterName = "image.tag" + defaultImageTag = "latest" + deploymentResource resourceKind = "Deployment" + statefulSetResource resourceKind = "StatefulSet" + appResourceGroup resourceGroup = "apps" ) type argoParameters struct { @@ -60,7 +60,7 @@ type serviceInfo struct { resourceKind resourceKind } -func getServiceInfo(service config.ServiceName, env config.Environment) (*serviceInfo, error) { +func getServiceInfo(service config.ServiceName, env config.Environment, customImageTagParameter string) (*serviceInfo, error) { cmd := exec.Command( "argocd", "--grpc-web", @@ -93,6 +93,11 @@ func getServiceInfo(service config.ServiceName, env config.Environment) (*servic parameters := response.Spec.Source.Helm.Parameters imageTag := defaultImageTag + imageTagParameter := defaultImageTagParameter + + if customImageTagParameter != "" { + imageTagParameter = parameterName(customImageTagParameter) + } for _, param := range parameters { if param.Name == imageTagParameter { @@ -149,7 +154,11 @@ func restart(service config.ServiceName, env config.Environment, kind resourceKi return nil } -func deploy(service config.ServiceName, tag string, env config.Environment) error { +func deploy(service config.ServiceName, tag string, env config.Environment, customImageTagParameter string) error { + imageTagParameter := defaultImageTagParameter + if customImageTagParameter != "" { + imageTagParameter = parameterName(customImageTagParameter) + } cmd := exec.Command( "argocd", "--grpc-web", @@ -175,9 +184,9 @@ func deploy(service config.ServiceName, tag string, env config.Environment) erro return nil } -func Deploy(service config.ServiceName, tag string, env config.Environment) error { +func Deploy(service config.ServiceName, tag string, env config.Environment, imageTagParameter string) error { log.Infof("retrieving current tag for service %s from argo...", service) - serviceInfo, err := getServiceInfo(service, env) + serviceInfo, err := getServiceInfo(service, env, imageTagParameter) if err != nil { return fmt.Errorf("failed to get current tag of service %s: %w", service, err) } @@ -193,7 +202,7 @@ func Deploy(service config.ServiceName, tag string, env config.Environment) erro log.Infof("service %s restarted with tag %s", service, tag) } else { log.Infof("current tag and given tag differ, overriding...") - err = deploy(service, tag, env) + err = deploy(service, tag, env, imageTagParameter) if err != nil { return fmt.Errorf("deploy of service %s failed: %w", service, err) } diff --git a/cmd/build.go b/cmd/build.go index 2e79902..b25c688 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -66,7 +66,7 @@ var buildCmd = &cobra.Command{ if buildConfig.deploy { for _, service := range services { - err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env) + err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env, config.GetImageTagParameter(service)) checkNoError(err) } } diff --git a/cmd/build_all.go b/cmd/build_all.go index d1065a1..9481894 100644 --- a/cmd/build_all.go +++ b/cmd/build_all.go @@ -52,7 +52,7 @@ var buildAllCmd = &cobra.Command{ if buildConfig.deploy { for _, service := range config.GetAllServices() { - err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env) + err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env, config.GetImageTagParameter(service)) checkNoError(err) } } diff --git a/cmd/deploy.go b/cmd/deploy.go index a9f1f6a..fa545ba 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -39,7 +39,7 @@ var deployCmd = &cobra.Command{ continue } } - err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env) + err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env, config.GetImageTagParameter(service)) checkNoError(err) } }, diff --git a/cmd/deploy_all.go b/cmd/deploy_all.go index 1b8bbce..7969d59 100644 --- a/cmd/deploy_all.go +++ b/cmd/deploy_all.go @@ -34,7 +34,7 @@ var deployAllCmd = &cobra.Command{ continue } } - err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env) + err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env, config.GetImageTagParameter(service)) checkNoError(err) } }, diff --git a/config/service.go b/config/service.go index a21470e..50bd376 100644 --- a/config/service.go +++ b/config/service.go @@ -19,6 +19,10 @@ func GetImageName(service Service) ImageName { return Config.Services[service].ImageName } +func GetImageTagParameter(service Service) string { + return Config.Services[service].ImageTagParameter +} + func GetAllServices() []Service { res := make([]Service, 0, len(Config.Services)) diff --git a/config/structure.go b/config/structure.go index 7bd7e95..e3eb6b0 100644 --- a/config/structure.go +++ b/config/structure.go @@ -44,11 +44,12 @@ type ScriptsConfiguration struct { } type ServiceConfiguration struct { - Directory string `json:"directory"` - ServiceName ServiceName `json:"service_name"` - ImageName ImageName `json:"image_name"` - Scripts ScriptsConfiguration `json:"scripts"` - Dockerfile string `json:"dockerfile"` + Directory string `json:"directory"` + ServiceName ServiceName `json:"service_name"` + ImageName ImageName `json:"image_name"` + Scripts ScriptsConfiguration `json:"scripts"` + Dockerfile string `json:"dockerfile"` + ImageTagParameter string `json:"image_tag_parameter"` } type ArgoEnvironmentConfiguration struct {