2 Commits

Author SHA1 Message Date
2e688d8fa8 fix 2022-09-19 09:55:06 +07:00
cda2e7a2bc add test code 2022-09-19 07:47:09 +07:00
4 changed files with 85 additions and 8 deletions

2
.gitignore vendored
View File

@@ -1,5 +1,7 @@
.env
ci/app/go-site
data/gitlab-test/gitlab
data/gitlab-test/postgresql
data/gitlab-test/redis

View File

@@ -10,15 +10,17 @@ variables:
GITLAB: git.bildme.ru
IMAGE_NAME: $REGISTRY/$CI_PROJECT_PATH:latest
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}
DOCKER_SERVER: 192.168.9.199
stages:
- test
- build
- push
- artifacts
- deploy-stage
- test-stage
- release
- deploy
.rules: &rules
rules:
@@ -26,6 +28,16 @@ stages:
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
image: golang:latest
@@ -50,6 +62,35 @@ push_image:
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
@@ -59,6 +100,8 @@ package:
- 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
@@ -75,13 +118,13 @@ release:
links:
- name: '${RELEASE_NAME}'
url: '${PACKAGE_REGISTRY_URL}/${RELEASE_NAME}'
when:
manual
deploy_prod:
stage: deploy
stage: release
image: hub.realmanual.ru/pub/openssh-client:latest
<<: *rules
variables:
DOCKER_SERVER: 192.168.9.199
before_script:
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
@@ -96,8 +139,8 @@ deploy_prod:
url: http://${DOCKER_SERVER}:3000/
tags:
- docker
# when:
# manual
when:
manual
### Что можно добавить ?

26
ci/app/go_test.go Normal file
View 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(),
)
}
}

View File

@@ -12,6 +12,11 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
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() {
port := os.Getenv("PORT")
if port == "" {
@@ -22,5 +27,6 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", indexHandler)
mux.HandleFunc("/health", healthMessage)
http.ListenAndServe(":"+port, mux)
}