# syntax=docker/dockerfile:experimental

# ----------------------------

# Stage to run linter and tests in the CI server (Jenkins)
# Latest Go image from https://git-aws.internal.justin.tv/dta/language-build-images#xenial-images-for-languagestools
FROM docker.internal.justin.tv/devtools/xenial/go1.14.2:latest as go_build
WORKDIR /build/go/src/code.justin.tv/lifecycle/Vienna

# Copy over the rest of the files and build.
# The vendor folder should contain all dependencies, if not, remember to go mod vendor, and commit.
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -o main .

# Install golangci-lint
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.30.0
RUN golangci-lint --version

# ----------------------------

# Stage to build frontend single-page-app assets
FROM node:10 AS node_build
WORKDIR /opt/vienna

# Install npm packages
COPY package.json package-lock.json .npmrc ./
RUN npm ci

# Copy over the rest of the files and build static assets into ./dist
COPY . .
RUN yarn build

# ----------------------------

# Stage to run with the restuls of the build
FROM alpine:latest as webapp
WORKDIR /vienna

# Install root certs for HTTPS
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*

# Copy static assets (please run `yarn build` first)
COPY --from=node_build /opt/vienna/dist ./dist

# Copy pre-built binary file from previous stage
COPY --from=go_build /build/go/src/code.justin.tv/lifecycle/Vienna/main .

ENTRYPOINT ["./main"]
