在 GitLab CI/CD 將 Docker 映像檔推送至 GitLab Container Registry

做法

新增 .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
default:
image: docker:20.10.16
services:
- docker:20.10.16-dind
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

stages:
- build

variables:
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
IMAGE_NAME: ${CI_PROJECT_NAME}-base

build:
stage: build
script:
- |
docker build --no-cache \
-t $IMAGE_NAME:$CI_COMMIT_REF_SLUG \
-t $IMAGE_NAME:latest \
-f Dockerfile .
- docker tag $IMAGE_NAME:$CI_COMMIT_REF_SLUG $CI_REGISTRY/group/project/$IMAGE_NAME:$CI_COMMIT_REF_SLUG
- docker tag $IMAGE_NAME:$CI_COMMIT_REF_SLUG $CI_REGISTRY/group/project/$IMAGE_NAME:latest
- docker push $CI_REGISTRY/group/project/$IMAGE_NAME:$CI_COMMIT_REF_SLUG
- docker push $CI_REGISTRY/group/project/$IMAGE_NAME:latest
only:
- main

最後,將程式碼推送到儲存庫。

參考資料