From 47d790075f388525e7579ee582ae4d6886e3e358 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Fri, 25 May 2018 16:01:58 +0300 Subject: [PATCH] add Jenkins configuration --- Jenkinsfile | 77 +++++++++++++++++++++++++++++++++++++++++ Makefile | 9 +++++ config_test.yml.dist | 2 +- docker-compose-test.yml | 3 +- 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..558c101 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,77 @@ +pipeline { + agent { + label 't2medium' + } + + options { + disableConcurrentBuilds() + } + + stages { + stage('Prepare') { + steps { + sh 'cp config_test.yml.dist config_test.yml' + compose 'up -d --build postgres_test' + compose 'run --rm mg_telegram make migrate_test' + } + } + + stage('Tests') { + steps { + compose 'run --rm --no-deps mg_telegram make jenkins_test' + } + + post { + always { + sh 'cat ./test-report.xml' + junit 'test-report.xml' + } + } + } + + stage('Docker Images') { + when { + branch 'master' + } + steps { + withCredentials([usernamePassword( + credentialsId: 'docker-hub-credentials', + usernameVariable: 'HUB_USER', + passwordVariable: 'HUB_PASS' + urlVariable: 'HUB_URL' + pathVariable: 'HUB_PATH' + )]) { + sh 'echo ${HUB_PASS} | docker login -u ${HUB_USER} --password-stdin ${HUB_URL}' + } + + sh 'docker build -t ${HUB_URL}${HUB_PATH} ./' + sh 'docker push ${HUB_URL}${HUB_PATH}' + } + post { + always { + sh 'docker rmi ${HUB_URL}${HUB_PATH}:latest' + } + } + } + } + + post { + always { + compose 'down -v' + deleteDir () + } + aborted { + echo "Aborted." + } + success { + echo "Success." + } + failure { + echo "Failure." + } + } +} + +def compose(cmd) { + sh "docker-compose --no-ansi -f docker-compose-test.yml ${cmd}" +} diff --git a/Makefile b/Makefile index ae2acb7..ac66c2f 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,15 @@ run: migrate @echo "==> Running" @${BIN} --config $(CONFIG_FILE) run +test: deps fmt + @echo "==> Running tests" + @cd $(SRC_DIR) && go test ./... -v -cpu 2 -cover -race + +jenkins_test: deps + @echo "==> Running tests (result in test-report.xml)" + @go get -v -u github.com/jstemmer/go-junit-report + @cd $(SRC_DIR) && go test ./... -v -cpu 2 -cover -race | go-junit-report -set-exit-code > $(SRC_DIR)/test-report.xml + fmt: @echo "==> Running gofmt" @gofmt -l -s -w $(SRC_DIR) diff --git a/config_test.yml.dist b/config_test.yml.dist index fbb2e3d..db022a0 100644 --- a/config_test.yml.dist +++ b/config_test.yml.dist @@ -3,7 +3,7 @@ database: http_server: host: ~ - listen: :3001 + listen: :3002 sentry_dsn: ~ diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 984a28a..c4f0e2e 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -9,7 +9,6 @@ services: POSTGRES_DATABASE: mg_telegram_test ports: - ${POSTGRES_ADDRESS:-127.0.0.1:5450}:${POSTGRES_PORT:-5450} - command: -p ${POSTGRES_PORT:-5450} mg_telegram_test: image: golang:1.9.3-stretch @@ -19,3 +18,5 @@ services: - ./:/mg_telegram/ links: - postgres_test + ports: + - ${MG_TELEGRAM_ADDRESS:-3002}:3002