From 9ee3e930421158c5e14a0e8a265f84081ba25830 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Thu, 24 Aug 2017 14:57:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D1=91?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D1=80=D0=BE=D0=B5=D0=BD=D0=B8=D1=8F=20produc?= =?UTF-8?q?tion=20image,=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D1=83=D0=B5=D0=BC=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20scr?= =?UTF-8?q?atch,=20=D0=B1=D0=B5=D0=B7=20alpine=20linux?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitlab-ci.yml | 119 +++++++++++++++++++++++++++++++++---------------- Dockerfile | 32 ++----------- Dockerfile-dev | 23 ++++++++++ 3 files changed, 106 insertions(+), 68 deletions(-) create mode 100644 Dockerfile-dev diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4caefbc..cf01634 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,46 +1,87 @@ +# Предполагается, что между работой "build docker container" и этапом push +# построенные docker images остаются статичными и никуда не пропадают +# +# В противном случае их нужно после каждого этапа билда пушить в registry + stages: - - test - - build - - push + - test + - build + - build_docker_image + - push + - cleanup variables: - CONTAINER_IMAGE: registry.ely.by/elyby/skinsystem + CONTAINER_IMAGE: registry.ely.by/elyby/skinsystem + +.golang_template: &setup_go_environment + image: golang:1.8.3-stretch + before_script: + - mkdir -p $GOPATH/src/$CI_PROJECT_NAMESPACE + - cp -r $(pwd) $GOPATH/src/$CI_PROJECT_PATH + - cd $GOPATH/src/$CI_PROJECT_PATH + - go get -u github.com/golang/dep/cmd/dep + - $GOPATH/bin/dep ensure + +.docker_template: &setup_docker_environment + image: docker:latest + variables: + GIT_STRATEGY: none + before_script: + - docker login -u gitlab-ci -p $CI_BUILD_TOKEN registry.ely.by + - export TEMP_IMAGE_NAME="$CONTAINER_IMAGE:$CI_PIPELINE_ID" test: - image: golang:1.8.3-stretch - stage: test - script: - - mkdir -p $GOPATH/src/$CI_PROJECT_NAMESPACE - - cp -r $(pwd) $GOPATH/src/$CI_PROJECT_PATH - - cd $GOPATH/src/$CI_PROJECT_PATH - - go get -u github.com/golang/dep/cmd/dep - - $GOPATH/bin/dep ensure - - ./script/coverage + <<: *setup_go_environment + stage: test + script: + - ./script/coverage -build: - image: docker:latest - stage: build - before_script: - - docker login -u gitlab-ci -p $CI_BUILD_TOKEN registry.ely.by - script: - - export IMAGE_NAME="$CONTAINER_IMAGE:dev" - - docker build --pull -t $IMAGE_NAME . - - docker push $IMAGE_NAME - only: - - develop +build executable: + <<: *setup_go_environment + stage: build + script: + - env GOOS=linux go build -o $CI_PROJECT_DIR/minecraft-skinsystem main.go + artifacts: + name: "${CI_JOB_STAGE} executable" + paths: + - $CI_PROJECT_DIR/minecraft-skinsystem + expire_in: 1 day -push_tags: - image: docker:latest - stage: push - variables: - GIT_STRATEGY: none - before_script: - - docker login -u gitlab-ci -p $CI_BUILD_TOKEN registry.ely.by - script: - - export IMAGE_NAME="$CONTAINER_IMAGE:$CI_BUILD_TAG" - - docker tag $CONTAINER_IMAGE:dev $CONTAINER_IMAGE:latest - - docker tag $CONTAINER_IMAGE:latest $IMAGE_NAME - - docker push $IMAGE_NAME - - docker push $CONTAINER_IMAGE:latest - only: - - tags +build docker image: + <<: *setup_docker_environment + stage: build_docker_image + script: + - docker build -t $TEMP_IMAGE_NAME . + only: + - tags + - develop + +push dev: + <<: *setup_docker_environment + stage: push + script: + - export IMAGE_NAME="$CONTAINER_IMAGE:dev" + - docker tag $TEMP_IMAGE_NAME $IMAGE_NAME + - docker push $IMAGE_NAME + only: + - develop + +push tag: + <<: *setup_docker_environment + stage: push + script: + - export IMAGE_NAME="$CONTAINER_IMAGE:$CI_BUILD_TAG" + - export LATEST_IMAGE_NAME="$CONTAINER_IMAGE:latest" + - docker tag $TEMP_IMAGE_NAME $IMAGE_NAME + - docker tag $TEMP_IMAGE_NAME $LATEST_IMAGE_NAME + - docker push $IMAGE_NAME + - docker push $LATEST_IMAGE_NAME + only: + - tags + +cleanup temp image: + <<: *setup_docker_environment + stage: cleanup + when: always + script: + - docker rmi $TEMP_IMAGE_NAME diff --git a/Dockerfile b/Dockerfile index 7cce980..6493c76 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,32 +1,6 @@ -FROM golang:1.9-alpine +FROM scratch -RUN mkdir -p /go/src/elyby/minecraft-skinsystem \ - /go/src/elyby/minecraft-skinsystem/data/capes \ - && ln -s /go/src/elyby/minecraft-skinsystem /go/src/app +COPY ./minecraft-skinsystem /app/ -WORKDIR /go/src/app - -COPY ./Gopkg.* /go/src/app/ -COPY ./main.go /go/src/app/ -COPY ./bootstrap /go/src/app/bootstrap -COPY ./cmd /go/src/app/cmd -COPY ./daemon /go/src/app/daemon -COPY ./db /go/src/app/db -COPY ./model /go/src/app/model -COPY ./repositories /go/src/app/repositories -COPY ./ui /go/src/app/ui -COPY ./utils /go/src/app/utils - -RUN apk add --no-cache git \ - && go get -u github.com/golang/dep/cmd/dep \ - && dep ensure \ - && go clean -i github.com/golang/dep \ - && rm -rf $GOPATH/src/github.com/golang/dep \ - && apk del git \ - && go build main.go \ - && mv main /usr/local/bin/minecraft-skinsystem - -EXPOSE 80 - -ENTRYPOINT ["minecraft-skinsystem"] +ENTRYPOINT ["/app/minecraft-skinsystem"] CMD ["serve"] diff --git a/Dockerfile-dev b/Dockerfile-dev new file mode 100644 index 0000000..5f2f567 --- /dev/null +++ b/Dockerfile-dev @@ -0,0 +1,23 @@ +FROM golang:1.9-alpine + +RUN mkdir -p /go/src/elyby/minecraft-skinsystem \ + /go/src/elyby/minecraft-skinsystem/data/capes \ + && ln -s /go/src/elyby/minecraft-skinsystem /go/src/app + +WORKDIR /go/src/app + +COPY ./ /go/src/app/ + +RUN apk add --no-cache git \ + && go get -u github.com/golang/dep/cmd/dep \ + && dep ensure \ + && go clean -i github.com/golang/dep \ + && rm -rf $GOPATH/src/github.com/golang/dep \ + && apk del git \ + && go build main.go \ + && mv main /usr/local/bin/minecraft-skinsystem + +EXPOSE 80 + +ENTRYPOINT ["minecraft-skinsystem"] +CMD ["serve"]