Compare commits
7 Commits
lesson3-ci
...
lesson5-ci
Author | SHA1 | Date | |
---|---|---|---|
2e688d8fa8 | |||
cda2e7a2bc | |||
8de7cef179 | |||
768cc0015c | |||
bab862a084 | |||
920f1d4f12 | |||
293a7d39b0 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,5 +1,7 @@
|
||||
.env
|
||||
|
||||
ci/app/go-site
|
||||
|
||||
data/gitlab-test/gitlab
|
||||
data/gitlab-test/postgresql
|
||||
data/gitlab-test/redis
|
||||
|
@@ -7,38 +7,144 @@ image: docker:20.10.16
|
||||
|
||||
variables:
|
||||
REGISTRY: hub.bildme.ru
|
||||
GITLAB: git.bildme.ru
|
||||
IMAGE_NAME: $REGISTRY/$CI_PROJECT_PATH:latest
|
||||
VERSION: 0.0.1
|
||||
RELEASE_NAME: go-site
|
||||
VERSION: 0.0.7
|
||||
PACKAGE_REGISTRY_URL: https://${GITLAB}/api/v4/projects/${CI_PROJECT_ID}/packages/generic/${RELEASE_NAME}/${VERSION}
|
||||
DOCKER_SERVER: 192.168.9.199
|
||||
|
||||
stages:
|
||||
- test
|
||||
- build
|
||||
# - deploy
|
||||
- push
|
||||
- deploy-stage
|
||||
- test-stage
|
||||
- release
|
||||
|
||||
.rules: &rules
|
||||
rules:
|
||||
- if: $CI_COMMIT_TAG
|
||||
when: never
|
||||
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
||||
|
||||
test_code:
|
||||
stage: test
|
||||
image: golang:latest
|
||||
<<: *rules
|
||||
script:
|
||||
- cd app
|
||||
- CGO_ENABLED=0 GOOS=linux GO111MODULE=auto go test
|
||||
tags:
|
||||
- docker
|
||||
|
||||
build_main:
|
||||
stage: build
|
||||
variables:
|
||||
DOCKER_BASEIMAGE: golang:latest
|
||||
image: golang:latest
|
||||
<<: *rules
|
||||
script:
|
||||
- cd app
|
||||
- CGO_ENABLED=0 GOOS=linux GO111MODULE=auto go build -a -ldflags="-X 'main.Version=v${VERSION}'" -installsuffix cgo -o go-site .
|
||||
artifacts:
|
||||
paths:
|
||||
- app/go-site
|
||||
tags:
|
||||
- docker
|
||||
|
||||
push_image:
|
||||
stage: push
|
||||
<<: *rules
|
||||
before_script:
|
||||
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $REGISTRY
|
||||
script:
|
||||
- docker build --pull --build-arg DOCKER_BASEIMAGE=${DOCKER_BASEIMAGE} -build-arg VERSION=${VERSION} -t $IMAGE_NAME .
|
||||
- docker build -t $IMAGE_NAME .
|
||||
- docker push $IMAGE_NAME
|
||||
tags:
|
||||
- docker
|
||||
|
||||
# deploy_prod:
|
||||
# image: hub.realmanual.ru/pub/openssh-client:latest
|
||||
# stage: deploy
|
||||
# variables:
|
||||
# DOCKER_SERVER: 192.168.9.199
|
||||
# before_script:
|
||||
# - eval $(ssh-agent -s)
|
||||
# - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
|
||||
# - mkdir -p ~/.ssh
|
||||
# - chmod 700 ~/.ssh
|
||||
# - ssh-keyscan ${DOCKER_SERVER} >> ~/.ssh/known_hosts
|
||||
# - chmod 644 ~/.ssh/known_hosts
|
||||
# script:
|
||||
# - ssh root@${DOCKER_SERVER} "docker rm -f main-go; docker pull hub.bildme.ru/firstgroup/go-site && docker run --rm --name main-go -p 3000:3000 hub.bildme.ru/firstgroup/go-site:latest"
|
||||
# tags:
|
||||
# - docker
|
||||
deploy_stage:
|
||||
stage: deploy-stage
|
||||
image: hub.realmanual.ru/pub/openssh-client:latest
|
||||
<<: *rules
|
||||
before_script:
|
||||
- eval $(ssh-agent -s)
|
||||
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
|
||||
- mkdir -p ~/.ssh
|
||||
- chmod 700 ~/.ssh
|
||||
- ssh-keyscan ${DOCKER_SERVER} >> ~/.ssh/known_hosts
|
||||
- chmod 644 ~/.ssh/known_hosts
|
||||
script:
|
||||
- ssh root@${DOCKER_SERVER} "docker rm -f main-go; docker pull hub.bildme.ru/firstgroup/go-site && docker run -d --rm --name main-go -p 3000:3000 hub.bildme.ru/firstgroup/go-site:latest"
|
||||
environment:
|
||||
name: stage
|
||||
url: http://${DOCKER_SERVER}:3000/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
test_stage:
|
||||
stage: test-stage
|
||||
image: alpine
|
||||
<<: *rules
|
||||
script:
|
||||
- apk --no-cache add curl
|
||||
- "[ $(curl -sw '%{http_code}' --connect-timeout 2 'http://${DOCKER_SERVER}:3000/health' -o /dev/null) -ne 200 ] && exit 0 || exit 1"
|
||||
tags:
|
||||
- docker
|
||||
|
||||
package:
|
||||
stage: release
|
||||
<<: *rules
|
||||
before_script:
|
||||
- apk add curl
|
||||
script:
|
||||
- FILE_NAME=${RELEASE_NAME}
|
||||
- cd app
|
||||
- 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file ${FILE_NAME} ${PACKAGE_REGISTRY_URL}/${FILE_NAME}'
|
||||
when:
|
||||
manual
|
||||
|
||||
release:
|
||||
stage: release
|
||||
image: registry.gitlab.com/gitlab-org/release-cli:latest
|
||||
<<: *rules
|
||||
script:
|
||||
- echo "Release ${RELEASE_NAME}"
|
||||
release:
|
||||
name: 'Release $VERSION'
|
||||
description: 'Release $VERSION'
|
||||
tag_name: '$VERSION'
|
||||
ref: '$CI_COMMIT_SHA'
|
||||
assets:
|
||||
links:
|
||||
- name: '${RELEASE_NAME}'
|
||||
url: '${PACKAGE_REGISTRY_URL}/${RELEASE_NAME}'
|
||||
when:
|
||||
manual
|
||||
|
||||
deploy_prod:
|
||||
stage: release
|
||||
image: hub.realmanual.ru/pub/openssh-client:latest
|
||||
<<: *rules
|
||||
before_script:
|
||||
- eval $(ssh-agent -s)
|
||||
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
|
||||
- mkdir -p ~/.ssh
|
||||
- chmod 700 ~/.ssh
|
||||
- ssh-keyscan ${DOCKER_SERVER} >> ~/.ssh/known_hosts
|
||||
- chmod 644 ~/.ssh/known_hosts
|
||||
script:
|
||||
- ssh root@${DOCKER_SERVER} "docker rm -f main-go; docker pull hub.bildme.ru/firstgroup/go-site && docker run -d --rm --name main-go -p 3000:3000 hub.bildme.ru/firstgroup/go-site:latest"
|
||||
environment:
|
||||
name: production
|
||||
url: http://${DOCKER_SERVER}:3000/
|
||||
tags:
|
||||
- docker
|
||||
when:
|
||||
manual
|
||||
|
||||
|
||||
### Что можно добавить ?
|
||||
### - версионирование тегов
|
||||
### - возможность откатываться назад на версию (на нужную версию через ввод переменной)
|
||||
### - тестирование кода линтером перед деплоем
|
||||
### - возможность деплоя на два окружения (сценарий, когда сначала на стейдж, потом в ручную по тригеру на прод)
|
||||
|
@@ -1,21 +1,10 @@
|
||||
ARG DOCKER_BASEIMAGE
|
||||
FROM ${DOCKER_BASEIMAGE}
|
||||
|
||||
LABEL maintainer="${DOCKER_MAINTAINER:-vasyakrg@gmail.com}"
|
||||
|
||||
ADD ./app /app/
|
||||
WORKDIR /app/
|
||||
|
||||
ARG VERSION=0.0.1
|
||||
ENV VERSION=${VERSION}
|
||||
|
||||
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=auto go build -a -ldflags="-X 'main.Version=v${VERSION}'" -installsuffix cgo -o main .
|
||||
|
||||
|
||||
FROM alpine
|
||||
ENV LANGUAGE="ru"
|
||||
COPY --from=0 /app/ /
|
||||
RUN apk add --no-cache ca-certificates &&\
|
||||
chmod +x main
|
||||
|
||||
CMD [ "./main" ]
|
||||
COPY app/go-site /app/
|
||||
WORKDIR /app
|
||||
|
||||
RUN apk add --no-cache ca-certificates &&\
|
||||
chmod +x go-site
|
||||
|
||||
CMD [ "./go-site" ]
|
||||
|
26
ci/app/go_test.go
Normal file
26
ci/app/go_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
||||
func TestHealthMessage(t *testing.T) {
|
||||
wr := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||
|
||||
healthMessage(wr, req)
|
||||
if wr.Code != http.StatusOK {
|
||||
t.Errorf("got HTTP status code %d, expected 200", wr.Code)
|
||||
}
|
||||
|
||||
if !strings.Contains(wr.Body.String(), `{"health":"OK"}`) {
|
||||
t.Errorf(
|
||||
`response body "%s" does not contain "health OK"`,
|
||||
wr.Body.String(),
|
||||
)
|
||||
}
|
||||
}
|
@@ -6,8 +6,15 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var Version = "0.0.1"
|
||||
|
||||
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("<h1>Hello World!</h1>"))
|
||||
w.Write([]byte("<h1>Hello World, im ver:"+Version+" !</h1>"))
|
||||
}
|
||||
|
||||
func healthMessage(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"health":"OK"}`))
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -16,9 +23,10 @@ func main() {
|
||||
port = "3000"
|
||||
}
|
||||
|
||||
fmt.Print("\nim run in http://127.0.0.1:", port)
|
||||
fmt.Print("\nVersion:", Version, ", Im run in http://127.0.0.1:", port)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", indexHandler)
|
||||
mux.HandleFunc("/health", healthMessage)
|
||||
http.ListenAndServe(":"+port, mux)
|
||||
}
|
||||
|
Reference in New Issue
Block a user