-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
85 lines (79 loc) · 3.09 KB
/
.gitlab-ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
variables:
VERSION_MAJOR: "1"
DOCKER_IMAGE: mcr.microsoft.com/dotnet/sdk:8.0-alpine
DOCKER_IMAGE_NAME: monobank-exporter
PROJECT_NAME: "MonobankExporter"
PROJECT_REPOSITORY: "https://github.com/TheGarmr/monobank-exporter"
stages:
- build
- unit-tests
- publish
- tag-new-version
Build:
stage: build
image: $DOCKER_IMAGE
script:
- echo "Building $PROJECT_NAME..."
- dotnet build
Unit tests:
stage: unit-tests
image: $DOCKER_IMAGE
script:
- dotnet test ./$PROJECT_NAME.UnitTests/$PROJECT_NAME.UnitTests.csproj
Publish docker image:
stage: publish
image: docker:20-dind
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
only:
- main
when: manual
services:
- name: docker:20-dind
alias: docker
command: ["--tls=false"]
before_script:
- apk update && apk add --no-cache git
script:
- CURRENT_VERSION_TAG=$(git describe --abbrev=0 --tags $(git rev-list --tags --max-count=1) 2>/dev/null || echo "")
- CURRENT_VERSION_MAJOR=$(echo "${CURRENT_VERSION_TAG}" | cut -d'.' -f1)
- OLD_VERSION_MINOR=$(echo "${CURRENT_VERSION_TAG}" | cut -d'.' -f2)
- if [ $(echo "$CURRENT_VERSION_MAJOR" | tr -dc '0-9') -gt "${VERSION_MAJOR}" ]; then
- NEW_VERSION="${CURRENT_VERSION_MAJOR}.0"
- else
- NEW_VERSION="${CURRENT_VERSION_MAJOR}.$((${OLD_VERSION_MINOR} + 1))"
- fi
- echo "Building ${NEW_VERSION}"
- echo "Logging into Docker Hub..."
- docker login -u $DOCKER_HUB_USERNAME -p $DOCKER_HUB_PASSWORD
- echo "Building image with version ${NEW_VERSION}"
- docker build --build-arg VERSION=${NEW_VERSION#v} --build-arg MAINTAINER=$DOCKER_HUB_USERNAME --build-arg URL=$PROJECT_REPOSITORY -t $DOCKER_HUB_USERNAME/$DOCKER_IMAGE_NAME:${NEW_VERSION} -f ./$PROJECT_NAME.Service/Dockerfile .
- docker tag "$DOCKER_HUB_USERNAME/$DOCKER_IMAGE_NAME:${NEW_VERSION}" "$DOCKER_HUB_USERNAME/$DOCKER_IMAGE_NAME:latest"
- docker push "$DOCKER_HUB_USERNAME/$DOCKER_IMAGE_NAME:${NEW_VERSION}"
- docker push "$DOCKER_HUB_USERNAME/$DOCKER_IMAGE_NAME:latest"
Tag new version:
image: alpine
stage: tag-new-version
allow_failure: false
interruptible: false
needs:
- Publish docker image
only:
- main
when: on_success
before_script:
- apk update && apk add --no-cache git
script:
- CURRENT_VERSION_TAG=$(git describe --abbrev=0 --tags $(git rev-list --tags --max-count=1) 2>/dev/null || echo "")
- CURRENT_VERSION_MAJOR=$(echo "${CURRENT_VERSION_TAG}" | cut -d'.' -f1)
- OLD_VERSION_MINOR=$(echo "${CURRENT_VERSION_TAG}" | cut -d'.' -f2)
- if [ $(echo "$CURRENT_VERSION_MAJOR" | tr -dc '0-9') -gt "${VERSION_MAJOR}" ]; then
- NEW_VERSION_TAG="${CURRENT_VERSION_MAJOR}.0"
- else
- NEW_VERSION_TAG="${CURRENT_VERSION_MAJOR}.$((${OLD_VERSION_MINOR} + 1))"
- fi
- echo "Current version tag - ${CURRENT_VERSION_TAG}. New version tag - ${NEW_VERSION_TAG}"
- git tag "${NEW_VERSION_TAG}"
- git push "${CI_SERVER_PROTOCOL}://gitlab-ci-token:${RUNNER_PERSONAL_ACCESS_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}" "${NEW_VERSION_TAG}"