Compare commits
7 Commits
main
...
lesson4-ci
Author | SHA1 | Date | |
---|---|---|---|
8de7cef179 | |||
768cc0015c | |||
bab862a084 | |||
920f1d4f12 | |||
293a7d39b0 | |||
429f1f3a6e | |||
2d0c627dc9 |
107
ci/.gitlab-ci.yml
Normal file
107
ci/.gitlab-ci.yml
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
# полный реферерс тут
|
||||||
|
# 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.5
|
||||||
|
PACKAGE_REGISTRY_URL: https://${GITLAB}/api/v4/projects/${CI_PROJECT_ID}/packages/generic/${RELEASE_NAME}/${VERSION}
|
||||||
|
|
||||||
|
stages:
|
||||||
|
- build
|
||||||
|
- push
|
||||||
|
- artifacts
|
||||||
|
- release
|
||||||
|
- deploy
|
||||||
|
|
||||||
|
.rules: &rules
|
||||||
|
rules:
|
||||||
|
- if: $CI_COMMIT_TAG
|
||||||
|
when: never
|
||||||
|
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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}'
|
||||||
|
|
||||||
|
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}'
|
||||||
|
|
||||||
|
deploy_prod:
|
||||||
|
stage: deploy
|
||||||
|
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 -
|
||||||
|
- 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
|
||||||
|
|
||||||
|
|
||||||
|
### Что можно добавить ?
|
||||||
|
### - версионирование тегов
|
||||||
|
### - возможность откатываться назад на версию (на нужную версию через ввод переменной)
|
||||||
|
### - тестирование кода линтером перед деплоем
|
||||||
|
### - возможность деплоя на два окружения (сценарий, когда сначала на стейдж, потом в ручную по тригеру на прод)
|
10
ci/Dockerfile
Normal file
10
ci/Dockerfile
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
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" ]
|
26
ci/app/main.go
Normal file
26
ci/app/main.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
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 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)
|
||||||
|
http.ListenAndServe(":"+port, mux)
|
||||||
|
}
|
4
ci/docker-run.sh
Executable file
4
ci/docker-run.sh
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/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
|
Reference in New Issue
Block a user