Skip to content

Commit eb2d7d1

Browse files
committed
Also build Docker Image
1 parent 8f5dcf3 commit eb2d7d1

File tree

1 file changed

+82
-15
lines changed

1 file changed

+82
-15
lines changed

.gitlab-ci.yml

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,41 @@ cache:
22
paths:
33
- vendor/
44

5-
before_script:
6-
- apt-get update -yqq
7-
- apt-get install -yqq git libzip-dev unzip zlib1g-dev
8-
- docker-php-ext-install zip
9-
- pecl install xdebug
10-
- docker-php-ext-enable xdebug
11-
- curl -sS https://getcomposer.org/installer | php
12-
- php composer.phar install
5+
stages:
6+
- test
7+
- deploy
8+
- build_image
9+
- push_image
1310

14-
test:7.3:
11+
.php_build_install:
12+
before_script:
13+
- apt-get update -yqq
14+
- apt-get install -yqq git libzip-dev unzip zlib1g-dev
15+
- docker-php-ext-install zip
16+
- pecl install xdebug
17+
- docker-php-ext-enable xdebug
18+
- curl -sS https://getcomposer.org/installer | php
19+
20+
.tests:
21+
extends: .php_build_install
1522
stage: test
16-
image: php:7.3
1723
script:
24+
- php composer.phar install
1825
- vendor/bin/phpunit --coverage-text --colors=never
1926

27+
test:7.3:
28+
extends: .tests
29+
image: php:7.3
30+
2031
test:7.4:
21-
stage: test
32+
extends: .tests
2233
image: php:7.4
23-
script:
24-
- vendor/bin/phpunit --coverage-text --colors=never
2534

2635
release:
36+
extends: .php_build_install
2737
stage: deploy
2838
image: php:7.4
29-
script: "true"
39+
script: php composer.phar install --no-dev
3040
dependencies:
3141
- test:7.3
3242
- test:7.4
@@ -40,4 +50,61 @@ release:
4050
- LICENSE
4151
- README.md
4252
only:
43-
- tags
53+
- tags
54+
55+
Docker Build:
56+
stage: build_image
57+
script:
58+
- echo -n $CI_JOB_TOKEN | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY
59+
# fetches the latest image (not failing if image is not found)
60+
- docker pull $CI_REGISTRY_IMAGE:latest || true
61+
# builds the project and passes vcs vars for LABEL
62+
# notice the cache-from, which is going to use the image we just pulled locally
63+
# the built image is tagged locally with the commit SHA, and then pushed to
64+
# the GitLab registry
65+
- >
66+
docker build
67+
--pull
68+
--build-arg VCS_REF=$CI_COMMIT_SHA
69+
--build-arg VCS_URL=$CI_PROJECT_URL
70+
--cache-from $CI_REGISTRY_IMAGE:latest
71+
--tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
72+
.
73+
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
74+
75+
76+
# Here, the goal is to tag the "master" branch as "latest"
77+
Push Docker latest:
78+
variables:
79+
# We are just playing with Docker here.
80+
# We do not need GitLab to clone the source code.
81+
GIT_STRATEGY: none
82+
stage: push_image
83+
only:
84+
# Only "master" should be tagged "latest"
85+
- master
86+
script:
87+
- echo -n $CI_JOB_TOKEN | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY
88+
# Because we have no guarantee that this job will be picked up by the same runner
89+
# that built the image in the previous step, we pull it again locally
90+
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
91+
# Then we tag it "latest"
92+
- docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:latest
93+
# Annnd we push it.
94+
- docker push $CI_REGISTRY_IMAGE:latest
95+
96+
# Finally, the goal here is to Docker tag any Git tag
97+
# GitLab will start a new pipeline everytime a Git tag is created, which is pretty awesome
98+
Push Docker tag:
99+
variables:
100+
# Again, we do not need the source code here. Just playing with Docker.
101+
GIT_STRATEGY: none
102+
stage: push_image
103+
only:
104+
# We want this job to be run on tags only.
105+
- tags
106+
script:
107+
- echo -n $CI_JOB_TOKEN | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY
108+
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
109+
- docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
110+
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME

0 commit comments

Comments
 (0)