Skip to content

Instantly share code, notes, and snippets.

@zacksleo
Last active April 22, 2019 06:06
Show Gist options
  • Save zacksleo/6b86c61fff51939de7dbd6af531de9f3 to your computer and use it in GitHub Desktop.
Save zacksleo/6b86c61fff51939de7dbd6af531de9f3 to your computer and use it in GitHub Desktop.
Golang持续集成与自动化测试和部署
# golang-devops-and-auto-deploy
image: zacksleo/golang
stages:
- test
- build
- deploy
variables:
GOPATH: /root
before_script:
- mkdir -p ~/src/github.com/zacksleo/projectname
- cp -r . ~/src/github.com/zacksleo/projectname
- cd ~/src/github.com/zacksleo/projectname
lint:
stage: test
script:
- golint -set_exit_status
unit-tests:
stage: test
services:
- mysql:5.6
variables:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: web
MYSQL_USER: web
MYSQL_PASSWORD: web
script:
- dep ensure
- cp tests/.env .env
- migrate -database "mysql://web:web@tcp(mysql:3306)/web" -path "./db/migrations/" up
- go test -tags=unit_tests $(go list ./... | grep -v /vendor/ ./tests/api) -v -coverprofile .testCoverage.txt
coverage: '/^coverage:\s(\d+(?:\.\d+)?%)/'
integration-tests:
stage: test
services:
- mysql:5.6
variables:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: web
MYSQL_USER: web
MYSQL_PASSWORD: web
script:
- dep ensure
- cp tests/.env .env
- migrate -database "mysql://web:web@tcp(mysql:3306)/web" -path "./db/migrations/" up
- go test -tags=integration $(go list ./tests/... | grep -v /vendor/) -v
build-bin:
stage: test
script:
- dep ensure
- env GOOS=linux GOARCH=386 go build -o $CI_PROJECT_DIR/debug
artifacts:
expire_in: 60 mins
untracked: true
name: "app"
paths:
- debug
when: on_success
build-image:
image: docker
stage: build
dependencies:
- build-bin
script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
- docker rmi $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
only:
- tags
tags:
- go
deploy:
image: zacksleo/node
stage: deploy
before_script:
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" > ~/deploy.key
- chmod 0600 ~/deploy.key
- ssh-add ~/deploy.key
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
script:
- cd deploy/production
- rsync -rtvhze ssh . root@$DEPLOY_SERVER:/data/gitlab/projectname --stats
- ssh root@$DEPLOY_SERVER "docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY"
- ssh root@$DEPLOY_SERVER "cd /data/gitlab/projectname && echo -e '\nTAG=$CI_COMMIT_TAG' >> .env && docker-compose pull app && docker-compose stop app && docker-compose rm -f app && docker-compose up -d app"
only:
- tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment