Created
February 1, 2023 10:27
-
-
Save thesadabc/b0236a47713f51cf54068f5849bc1f28 to your computer and use it in GitHub Desktop.
自动化gitlab构建模板,曲线使用缓存创建项目镜像
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 0. 项目中包含Dockerfile | |
# 1. 设置,指定项目ci模板,如果模板放在公共项目,可以不侵入项目源码 | |
# 2. 设置,配置对应环境变量,公用环境变量设置到项目组,使用gitlab内置变量,比如项目名变量等重写本配置,可能会更加方便 | |
# - CI_REGISTRY_IMAGE registry.example.com/xxxxxx/yyyyy | |
# - REGISTRY registry.example.com | |
# - REGISTRY_USER uuu | |
# - REGISTRY_PASSWORD ppp | |
.build: | |
image: docker:latest | |
stage: build | |
variables: | |
DOCKER_HOST: tcp://docker:2375 | |
DOCKER_TLS_CERTDIR: "" | |
services: | |
- name: docker:dind | |
alias: docker | |
command: [ '--tls=false' ] | |
before_script: | |
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY | |
after_script: | |
- echo "build tag[$CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG] from branch[$CI_COMMIT_BRANCH]" | |
- docker logout $CI_REGISTRY | |
build-dev: | |
extends: .build | |
variables: | |
CI_REGISTRY_TAG: $CI_COMMIT_REF_NAME # 保留到分支版本 | |
script: | |
- docker pull $CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG || echo "pull failed" | |
- docker build --cache-from $CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG -t $CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG . | |
- docker push -a $CI_REGISTRY_IMAGE | |
rules: | |
- if: $CI_REGISTRY_IMAGE && $CI_COMMIT_BRANCH =~ /dev|test|staging/ | |
exists: | |
- Dockerfile | |
build-release: | |
extends: .build | |
variables: | |
CI_REGISTRY_TAG: $CI_COMMIT_SHA # 主分支保留到版本号 | |
script: | |
# 主分支提交到 latest | |
- docker pull $CI_REGISTRY_IMAGE:latest || echo "pull failed" | |
- docker build --cache-from $CI_REGISTRY_IMAGE:latest -t $CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG -t $CI_REGISTRY_IMAGE:latest . | |
- docker push -a $CI_REGISTRY_IMAGE | |
rules: | |
- if: $CI_REGISTRY_IMAGE && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH | |
exists: | |
- Dockerfile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment