FROM registry.access.redhat.com/ubi8/ubi:latest AS builder

ARG GO_PACKAGE_PATH=github.com/jenkinsci/jenkins-automation-operator

# Basics
ENV GIT_COMMITTER_NAME="OpenShift Developer Sevices"
ENV GIT_COMMITTER_EMAIL=openshift-dev-services+jenkins@redhat.com
ENV LANG=en_US.utf8

# Go
ENV GOPATH=/tmp/go
ENV GOBIN=/tmp/go/bin
ENV GOROOT=/tmp/goroot/go
ENV GOCACHE=/tmp/go/.cache 
ENV GO_VERSION=1.14.1
ENV GO_DOWNLOAD_SITE=https://dl.google.com/go/
ENV GO_DIST=go$GO_VERSION.linux-amd64.tar.gz

# Operator SDK
ENV OPERATOR_SDK_VERSION=v1.0.1
ENV OPERATOR_SDK_DOWNLOAD_SITE=https://github.com/operator-framework/operator-sdk/releases/download/
ENV OPERATOR_SDK_DIST=operator-sdk-$OPERATOR_SDK_VERSION-x86_64-linux-gnu

# kubectl
ENV KUBECTL_VERSION=v1.14.3
ENV KUBECTL_DOWNLOAD_SITE=https://storage.googleapis.com/kubernetes-release/release/
ENV PATH=$PATH:$GOROOT/bin:$GOPATH/bin:$GOPATH/bin:/tmp/goroot/go/bin:/usr/bin:/usr/local/bin:/usr/sbin:$PATH

WORKDIR /tmp
RUN mkdir -p $GOPATH/bin &&  \
    mkdir -p /tmp/goroot && \
    curl -Lo $GO_DIST $GO_DOWNLOAD_SITE/$GO_DIST && \
    tar -C /tmp/goroot -xzf $GO_DIST && \
    curl -Lo kubectl $KUBECTL_DOWNLOAD_SITE/$KUBECTL_VERSION/bin/linux/amd64/kubectl && \
    chmod +x kubectl && mv kubectl $GOPATH/bin/ && \
    curl -Lo operator-sdk $OPERATOR_SDK_DOWNLOAD_SITE/$OPERATOR_SDK_VERSION/$OPERATOR_SDK_DIST && \
    chmod +x operator-sdk && mv operator-sdk $GOPATH/bin/ && \
    mkdir -p ${GOPATH}/src/${GO_PACKAGE_PATH}/ && \
    yum -y install --nodocs git make gcc diffutils 

WORKDIR ${GOPATH}/src/${GO_PACKAGE_PATH}

# Copy only relevant things (instead of all) to speed-up the build.
COPY main.go main.go
COPY api/ api/
COPY hack/ hack/
COPY controllers/ controllers/
COPY pkg/ pkg/
COPY version/ version/
COPY config/ config/
COPY scripts/ scripts/
COPY go.mod go.sum LICENSE Makefile ./
ARG VERBOSE=2
RUN make bin

FROM registry.access.redhat.com/ubi8/ubi-minimal
LABEL com.redhat.delivery.appregistry=true
LABEL maintainer "openshift-dev-services+jenkins@redhat.com"

ENV LANG=en_US.utf8 \
    OPERATOR_NAME=jenkins-operator

COPY --from=builder /tmp/go/src/github.com/jenkinsci/jenkins-automation-operator/build/_output/bin/${OPERATOR_NAME} /jenkins-operator

ENTRYPOINT [ "/jenkins-operator" ]

