From 195754432cd592564c80af936dd2dad271ed82df Mon Sep 17 00:00:00 2001 From: Lee Brown Date: Wed, 7 Aug 2019 17:49:21 -0800 Subject: [PATCH] fix docker-compose up - webapp required sender email to be set --- cmd/web-api/Dockerfile | 16 +++++++++------ cmd/web-app/Dockerfile | 13 +++++++----- cmd/web-app/main.go | 2 +- docker-compose.yaml | 11 ++++++++--- tools/devops/cmd/cicd/s3_batch_upload.go | 3 +-- tools/devops/cmd/cicd/service_build.go | 25 ++++++++++++++++++++---- tools/devops/cmd/cicd/service_deploy.go | 2 +- 7 files changed, 50 insertions(+), 22 deletions(-) diff --git a/cmd/web-api/Dockerfile b/cmd/web-api/Dockerfile index 26ec395..fe60f16 100644 --- a/cmd/web-api/Dockerfile +++ b/cmd/web-api/Dockerfile @@ -32,23 +32,27 @@ RUN go mod download FROM build_base_golang AS builder +ARG service +ARG commit_ref=- + # Copy shared packages. COPY internal ./internal # Copy cmd specific packages. -COPY cmd/web-api ./cmd/web-api -COPY cmd/web-api/templates /templates -#COPY cmd/web-api/static /static +COPY cmd/${service} ./cmd/web-api +COPY cmd/${service}/templates /templates +#COPY cmd/${service}/static /static # Copy the global templates. ADD resources/templates/shared /templates/shared -WORKDIR ./cmd/web-api +WORKDIR ./cmd/${service} # Update the API documentation. -RUN swag init +# Disabled for the moment as it takes forever to run, rely on manual execution. +#RUN swag init -RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /gosrv . +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.build=${commit_ref}" -a -installsuffix nocgo -o /gosrv . FROM alpine:3.9 diff --git a/cmd/web-app/Dockerfile b/cmd/web-app/Dockerfile index feebb9e..66ebe65 100644 --- a/cmd/web-app/Dockerfile +++ b/cmd/web-app/Dockerfile @@ -16,20 +16,23 @@ RUN go mod download FROM build_base_golang AS builder +ARG service +ARG commit_ref=- + # Copy shared packages. COPY internal ./internal # Copy cmd specific packages. -COPY cmd/web-app ./cmd/web-app -COPY cmd/web-app/templates /templates -COPY cmd/web-app/static /static +COPY cmd/${service} ./cmd/web-app +COPY cmd/${service}/templates /templates +COPY cmd/${service}/static /static # Copy the global templates. ADD resources/templates/shared /templates/shared -WORKDIR ./cmd/web-app +WORKDIR ./cmd/${service} -RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /gosrv . +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.build=${commit_ref}" -a -installsuffix nocgo -o /gosrv . FROM alpine:3.9 diff --git a/cmd/web-app/main.go b/cmd/web-app/main.go index e1dd427..4fa82a1 100644 --- a/cmd/web-app/main.go +++ b/cmd/web-app/main.go @@ -102,7 +102,7 @@ func main() { WebApiBaseUrl string `default:"http://127.0.0.1:3001" envconfig:"WEB_API_BASE_URL" example:"http://api.eproc.tech"` SessionKey string `default:"" envconfig:"SESSION_KEY"` SessionName string `default:"" envconfig:"SESSION_NAME"` - EmailSender string `default:"" envconfig:"EMAIL_SENDER"` + EmailSender string `default:"test@eproc.tech" envconfig:"EMAIL_SENDER"` DebugHost string `default:"0.0.0.0:4000" envconfig:"DEBUG_HOST"` ShutdownTimeout time.Duration `default:"5s" envconfig:"SHUTDOWN_TIMEOUT"` } diff --git a/docker-compose.yaml b/docker-compose.yaml index 694ad38..6d8c8e5 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -58,13 +58,14 @@ services: - DD_TAGS=source:docker env:dev - DD_DOGSTATSD_ORIGIN_DETECTION=true - DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true - #- ECS_FARGATE=false - DD_EXPVAR=service_name=web-app env=dev url=http://web-app:4000/debug/vars|service_name=web-api env=dev url=http://web-api:4001/debug/vars web-app: image: example-project/web-app:latest build: context: . dockerfile: cmd/web-app/Dockerfile + args: + service: 'web-app' ports: - 3000:3000 # WEB APP - 4000:4000 # DEBUG API @@ -83,10 +84,11 @@ services: - WEB_APP_APP_BASE_URL=http://127.0.0.1:3000 - WEB_API_APP_DEBUG_HOST=:4000 - WEB_APP_REDIS_HOST=redis:6379 - - WEB_APP_DB_HOST=postgres:5433 + - WEB_APP_DB_HOST=postgres:5432 - WEB_APP_DB_USER=postgres - WEB_APP_DB_PASS=postgres - WEB_APP_DB_DATABASE=shared + - WEB_APP_DB_DISABLE_TLS=true - DD_TRACE_AGENT_HOSTNAME=datadog - DD_TRACE_AGENT_PORT=8126 - DD_SERVICE_NAME=web-app @@ -98,6 +100,8 @@ services: build: context: . dockerfile: cmd/web-api/Dockerfile + args: + service: 'web-api' ports: - 3001:3001 # WEB API - 4001:4001 # DEBUG API @@ -116,10 +120,11 @@ services: - WEB_API_APP_BASE_URL=http://127.0.0.1:3001 - WEB_API_APP_DEBUG_HOST=:4001 - WEB_API_REDIS_HOST=redis:6379 - - WEB_API_DB_HOST=postgres:5433 + - WEB_API_DB_HOST=postgres:5432 - WEB_API_DB_USER=postgres - WEB_API_DB_PASS=postgres - WEB_API_DB_DATABASE=shared + - WEB_API_DB_DISABLE_TLS=true - DD_TRACE_AGENT_HOSTNAME=datadog - DD_TRACE_AGENT_PORT=8126 - DD_SERVICE_NAME=web-app diff --git a/tools/devops/cmd/cicd/s3_batch_upload.go b/tools/devops/cmd/cicd/s3_batch_upload.go index 321b7ca..c36be5c 100644 --- a/tools/devops/cmd/cicd/s3_batch_upload.go +++ b/tools/devops/cmd/cicd/s3_batch_upload.go @@ -75,8 +75,7 @@ func (di *DirectoryIterator) UploadObject() s3manager.BatchUploadObject { acl = aws.String(di.acl) } - - buffer, contentType, rerr := readFile(f) + buffer, contentType, rerr := readFile(f) nextPath, _ := filepath.Rel(di.dir, di.next.path) diff --git a/tools/devops/cmd/cicd/service_build.go b/tools/devops/cmd/cicd/service_build.go index d34e40d..46bded4 100644 --- a/tools/devops/cmd/cicd/service_build.go +++ b/tools/devops/cmd/cicd/service_build.go @@ -29,6 +29,7 @@ type ServiceBuildFlags struct { ProjectRoot string `validate:"omitempty" example:"."` ProjectName string ` validate:"omitempty" example:"example-project"` DockerFile string `validate:"omitempty" example:"./cmd/web-api/Dockerfile"` + CommitRef string `validate:"omitempty" example:"master@1ecfd275"` NoCache bool `validate:"omitempty" example:"false"` NoPush bool `validate:"omitempty" example:"false"` } @@ -41,8 +42,9 @@ type serviceBuildRequest struct { EcrRepository *ecr.CreateRepositoryInput EcrRepositoryMaxImages int `validate:"omitempty"` - NoCache bool `validate:"omitempty"` - NoPush bool `validate:"omitempty"` + CommitRef string `validate:"omitempty"` + NoCache bool `validate:"omitempty"` + NoPush bool `validate:"omitempty"` flags ServiceBuildFlags } @@ -79,8 +81,9 @@ func NewServiceBuildRequest(log *log.Logger, flags ServiceBuildFlags) (*serviceB req = serviceBuildRequest{ serviceRequest: sr, - NoCache: flags.NoCache, - NoPush: flags.NoPush, + CommitRef: flags.CommitRef, + NoCache: flags.NoCache, + NoPush: flags.NoPush, flags: flags, } @@ -100,6 +103,19 @@ func NewServiceBuildRequest(log *log.Logger, flags ServiceBuildFlags) (*serviceB req.EcrRepositoryMaxImages = defaultAwsRegistryMaxImages log.Printf("\t\t\tSet ECR Regsistry Max Images to '%d'.", req.EcrRepositoryMaxImages) + // Get the default commit ref. + if req.CommitRef == "" { + if ev := os.Getenv("CI_COMMIT_TAG"); ev != "" { + req.CommitRef = "tag-" + ev + } else if ev := os.Getenv("CI_COMMIT_REF_NAME"); ev != "" { + req.CommitRef = "branch-" + ev + } + + if ev := os.Getenv("CI_COMMIT_SHORT_SHA"); ev != "" { + req.CommitRef = req.CommitRef + "@" + ev + } + } + } return &req, nil @@ -315,6 +331,7 @@ func ServiceBuild(log *log.Logger, req *serviceBuildRequest) error { "--file=" + dockerFile, "--build-arg", "service=" + req.ServiceName, "--build-arg", "env=" + req.Env, + "--build-arg", "commit_ref=" + req.CommitRef, "-t", req.ReleaseImage, } diff --git a/tools/devops/cmd/cicd/service_deploy.go b/tools/devops/cmd/cicd/service_deploy.go index f67563b..c4543ec 100644 --- a/tools/devops/cmd/cicd/service_deploy.go +++ b/tools/devops/cmd/cicd/service_deploy.go @@ -3329,7 +3329,7 @@ func ServiceDeploy(log *log.Logger, req *serviceDeployRequest) error { staticDir := filepath.Join(req.ServiceDir, "static") - if _, err := os.Stat(staticDir); err != nil { + if _, err := os.Stat(staticDir); err != nil { return errors.Wrapf(err, "Static directory '%s' does not exist.", staticDir) }