Compare commits
1 Commits
lesson5-ci
...
lesson3-ci
Author | SHA1 | Date | |
---|---|---|---|
dfd16e74c5 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,7 +1,5 @@
|
|||||||
.env
|
.env
|
||||||
|
|
||||||
ci/app/go-site
|
|
||||||
|
|
||||||
data/gitlab-test/gitlab
|
data/gitlab-test/gitlab
|
||||||
data/gitlab-test/postgresql
|
data/gitlab-test/postgresql
|
||||||
data/gitlab-test/redis
|
data/gitlab-test/redis
|
||||||
|
@@ -7,124 +7,30 @@ image: docker:20.10.16
|
|||||||
|
|
||||||
variables:
|
variables:
|
||||||
REGISTRY: hub.bildme.ru
|
REGISTRY: hub.bildme.ru
|
||||||
GITLAB: git.bildme.ru
|
|
||||||
IMAGE_NAME: $REGISTRY/$CI_PROJECT_PATH:latest
|
IMAGE_NAME: $REGISTRY/$CI_PROJECT_PATH:latest
|
||||||
RELEASE_NAME: go-site
|
VERSION: 0.0.1
|
||||||
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:
|
stages:
|
||||||
- test
|
|
||||||
- build
|
- build
|
||||||
- push
|
- deploy
|
||||||
- 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:
|
build_main:
|
||||||
stage: build
|
stage: build
|
||||||
image: golang:latest
|
variables:
|
||||||
<<: *rules
|
DOCKER_BASEIMAGE: golang:latest
|
||||||
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:
|
before_script:
|
||||||
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $REGISTRY
|
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $REGISTRY
|
||||||
script:
|
script:
|
||||||
- docker build -t $IMAGE_NAME .
|
- docker build --pull --build-arg DOCKER_BASEIMAGE=${DOCKER_BASEIMAGE} -build-arg VERSION=${VERSION} -t $IMAGE_NAME .
|
||||||
- docker push $IMAGE_NAME
|
- docker push $IMAGE_NAME
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- 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:
|
deploy_prod:
|
||||||
stage: release
|
|
||||||
image: hub.realmanual.ru/pub/openssh-client:latest
|
image: hub.realmanual.ru/pub/openssh-client:latest
|
||||||
<<: *rules
|
stage: deploy
|
||||||
|
variables:
|
||||||
|
DOCKER_SERVER: 192.168.9.199
|
||||||
before_script:
|
before_script:
|
||||||
- eval $(ssh-agent -s)
|
- eval $(ssh-agent -s)
|
||||||
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
|
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
|
||||||
@@ -133,18 +39,6 @@ deploy_prod:
|
|||||||
- ssh-keyscan ${DOCKER_SERVER} >> ~/.ssh/known_hosts
|
- ssh-keyscan ${DOCKER_SERVER} >> ~/.ssh/known_hosts
|
||||||
- chmod 644 ~/.ssh/known_hosts
|
- chmod 644 ~/.ssh/known_hosts
|
||||||
script:
|
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"
|
- 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"
|
||||||
environment:
|
|
||||||
name: production
|
|
||||||
url: http://${DOCKER_SERVER}:3000/
|
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
when:
|
|
||||||
manual
|
|
||||||
|
|
||||||
|
|
||||||
### Что можно добавить ?
|
|
||||||
### - версионирование тегов
|
|
||||||
### - возможность откатываться назад на версию (на нужную версию через ввод переменной)
|
|
||||||
### - тестирование кода линтером перед деплоем
|
|
||||||
### - возможность деплоя на два окружения (сценарий, когда сначала на стейдж, потом в ручную по тригеру на прод)
|
|
||||||
|
@@ -1,10 +1,21 @@
|
|||||||
|
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
|
FROM alpine
|
||||||
ENV LANGUAGE="ru"
|
ENV LANGUAGE="ru"
|
||||||
|
COPY --from=0 /app/ /
|
||||||
COPY app/go-site /app/
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
RUN apk add --no-cache ca-certificates &&\
|
RUN apk add --no-cache ca-certificates &&\
|
||||||
chmod +x go-site
|
chmod +x main
|
||||||
|
|
||||||
CMD [ "./go-site" ]
|
CMD [ "./main" ]
|
||||||
|
@@ -1,26 +0,0 @@
|
|||||||
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,15 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Version = "0.0.1"
|
|
||||||
|
|
||||||
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("<h1>Hello World, im ver:"+Version+" !</h1>"))
|
w.Write([]byte("<h1>Hello World!</h1>"))
|
||||||
}
|
|
||||||
|
|
||||||
func healthMessage(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Add("Content-Type", "application/json")
|
|
||||||
w.Write([]byte(`{"health":"OK"}`))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -23,10 +16,9 @@ func main() {
|
|||||||
port = "3000"
|
port = "3000"
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print("\nVersion:", Version, ", Im run in http://127.0.0.1:", port)
|
fmt.Print("\nim run in http://127.0.0.1:", port)
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/", indexHandler)
|
mux.HandleFunc("/", indexHandler)
|
||||||
mux.HandleFunc("/health", healthMessage)
|
|
||||||
http.ListenAndServe(":"+port, mux)
|
http.ListenAndServe(":"+port, mux)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user