Compare commits
2 Commits
lesson4-ci
...
lesson5-ci
Author | SHA1 | Date | |
---|---|---|---|
2e688d8fa8 | |||
cda2e7a2bc |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,5 +1,7 @@
|
|||||||
.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
|
||||||
|
@@ -10,15 +10,17 @@ variables:
|
|||||||
GITLAB: git.bildme.ru
|
GITLAB: git.bildme.ru
|
||||||
IMAGE_NAME: $REGISTRY/$CI_PROJECT_PATH:latest
|
IMAGE_NAME: $REGISTRY/$CI_PROJECT_PATH:latest
|
||||||
RELEASE_NAME: go-site
|
RELEASE_NAME: go-site
|
||||||
VERSION: 0.0.5
|
VERSION: 0.0.7
|
||||||
PACKAGE_REGISTRY_URL: https://${GITLAB}/api/v4/projects/${CI_PROJECT_ID}/packages/generic/${RELEASE_NAME}/${VERSION}
|
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
|
- push
|
||||||
- artifacts
|
- deploy-stage
|
||||||
|
- test-stage
|
||||||
- release
|
- release
|
||||||
- deploy
|
|
||||||
|
|
||||||
.rules: &rules
|
.rules: &rules
|
||||||
rules:
|
rules:
|
||||||
@@ -26,6 +28,16 @@ stages:
|
|||||||
when: never
|
when: never
|
||||||
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
- 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
|
image: golang:latest
|
||||||
@@ -50,6 +62,35 @@ push_image:
|
|||||||
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:
|
package:
|
||||||
stage: release
|
stage: release
|
||||||
<<: *rules
|
<<: *rules
|
||||||
@@ -59,6 +100,8 @@ package:
|
|||||||
- FILE_NAME=${RELEASE_NAME}
|
- FILE_NAME=${RELEASE_NAME}
|
||||||
- cd app
|
- cd app
|
||||||
- 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file ${FILE_NAME} ${PACKAGE_REGISTRY_URL}/${FILE_NAME}'
|
- 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file ${FILE_NAME} ${PACKAGE_REGISTRY_URL}/${FILE_NAME}'
|
||||||
|
when:
|
||||||
|
manual
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
@@ -75,13 +118,13 @@ release:
|
|||||||
links:
|
links:
|
||||||
- name: '${RELEASE_NAME}'
|
- name: '${RELEASE_NAME}'
|
||||||
url: '${PACKAGE_REGISTRY_URL}/${RELEASE_NAME}'
|
url: '${PACKAGE_REGISTRY_URL}/${RELEASE_NAME}'
|
||||||
|
when:
|
||||||
|
manual
|
||||||
|
|
||||||
deploy_prod:
|
deploy_prod:
|
||||||
stage: deploy
|
stage: release
|
||||||
image: hub.realmanual.ru/pub/openssh-client:latest
|
image: hub.realmanual.ru/pub/openssh-client:latest
|
||||||
<<: *rules
|
<<: *rules
|
||||||
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 -
|
||||||
@@ -96,8 +139,8 @@ deploy_prod:
|
|||||||
url: http://${DOCKER_SERVER}:3000/
|
url: http://${DOCKER_SERVER}:3000/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
# when:
|
when:
|
||||||
# manual
|
manual
|
||||||
|
|
||||||
|
|
||||||
### Что можно добавить ?
|
### Что можно добавить ?
|
||||||
|
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(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@@ -12,6 +12,11 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte("<h1>Hello World, im ver:"+Version+" !</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() {
|
func main() {
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
if port == "" {
|
if port == "" {
|
||||||
@@ -22,5 +27,6 @@ func main() {
|
|||||||
|
|
||||||
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