2 Commits

Author SHA1 Message Date
02baec5951 Update README.md 2022-11-24 22:40:45 +07:00
Vassiliy Yegorov
cfdd245655 fix 2022-09-25 21:18:43 +07:00
7 changed files with 4 additions and 227 deletions

3
.gitignore vendored
View File

@@ -1,9 +1,8 @@
.env
ci/app/go-site
data/gitlab-test/gitlab
data/gitlab-test/postgresql
data/gitlab-test/redis
data/gitlab-test/gitlab-runner*
Addons/

View File

@@ -1,8 +1,8 @@
# Репа по курсу Gitlab-CI: только практика
1. Курс размещен [тут](https://lk.realmanual.ru/student/courses/25)
2. Более подробно о курсе [тут](https://realmanual.ru/gitlab)
3. Миттап про установку [тут](https://lk.realmanual.ru/student/courses/22)
1. Курс размещен [тут](https://realmanual.ru/public/course/570b3728-c242-4bcc-9502-4f8a81201ba7)
2. Более подробно о курсе [тут](https://promo.realmanual.ru/gitlab)
3. Миттап про установку [тут](https://realmanual.ru/public/course/abfbffe1-a6a8-4c7e-b87c-36acd589aed0)
4. Наша группа ТГ для вопросов [тут](https://t.me/realmanual_group)
# Твики

View File

@@ -1,150 +0,0 @@
# полный реферерс тут
# https://docs.gitlab.com/ee/ci/yaml/index.html
# а тут доступные переменные
# https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
image: docker:20.10.16
variables:
REGISTRY: hub.bildme.ru
GITLAB: git.bildme.ru
IMAGE_NAME: $REGISTRY/$CI_PROJECT_PATH:latest
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
- 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
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 -t $IMAGE_NAME .
- docker push $IMAGE_NAME
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
### Что можно добавить ?
### - версионирование тегов
### - возможность откатываться назад на версию (на нужную версию через ввод переменной)
### - тестирование кода линтером перед деплоем
### - возможность деплоя на два окружения (сценарий, когда сначала на стейдж, потом в ручную по тригеру на прод)

View File

@@ -1,10 +0,0 @@
FROM alpine
ENV LANGUAGE="ru"
COPY app/go-site /app/
WORKDIR /app
RUN apk add --no-cache ca-certificates &&\
chmod +x go-site
CMD [ "./go-site" ]

View File

@@ -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(),
)
}
}

View File

@@ -1,32 +0,0 @@
package main
import (
"net/http"
"os"
"fmt"
)
var Version = "0.0.1"
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 == "" {
port = "3000"
}
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)
}

View File

@@ -1,4 +0,0 @@
#!/bin/bash
docker pull hub.bildme.ru/firstgroup/go-site:latest
docker run --rm --name main-go -p 3000:3000 hub.bildme.ru/firstgroup/go-site:latest