我将如何合并或区分 GitLab ci cd yaml 脚本文件的两个 yml 文件?
How will I combine or differentiate the two yml files for GitLab ci cd yaml script file?
我有 GitLab ci/cd yaml 文件脚本
services:
- docker:19.03.11-dind
workflow:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH == "developer" || $CI_COMMIT_BRANCH == "stage"|| ($CI_COMMIT_BRANCH =~ (/^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i))
when: always
- if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH != "developer" || $CI_COMMIT_BRANCH != "stage"|| ($CI_COMMIT_BRANCH !~ (/^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i))
when: never
stages:
- build
- Publish
- deploy
cache:
paths:
- .m2/repository
- target
build_jar:
image: maven:3.8.3-jdk-11
stage: build
script:
- mvn clean install package -DskipTests=true
artifacts:
paths:
- target/*.jar
docker_build:
stage: Publish
image: docker:19.03.11
services:
- docker:19.03.11-dind
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $IMAGE_TAG .
- docker push $IMAGE_TAG
deploy_dev:
stage: deploy
image: stellacenter/aws-helm-kubectl
before_script:
- aws configure set aws_access_key_id ${DEV_AWS_ACCESS_KEY_ID}
- aws configure set aws_secret_access_key ${DEV_AWS_SECRET_ACCESS_KEY}
- aws configure set region ${DEV_AWS_DEFAULT_REGION}
script:
- sed -i "s/<VERSION>/${CI_COMMIT_SHORT_SHA}/g" provider-service-dev.yml
- mkdir -p $HOME/.kube
- cp $KUBE_CONFIG_DEV $HOME/.kube/config
- chown $(id -u):$(id -g) $HOME/.kube/config
- export KUBECONFIG=$HOME/.kube/config
- kubectl apply -f provider-service-dev.yml
only:
- developer
deploy_stage:
stage: deploy
image: stellacenter/aws-helm-kubectl
before_script:
- aws configure set aws_access_key_id ${DEV_AWS_ACCESS_KEY_ID}
- aws configure set aws_secret_access_key ${DEV_AWS_SECRET_ACCESS_KEY}
- aws configure set region ${DEV_AWS_DEFAULT_REGION}
script:
- sed -i "s/<VERSION>/${CI_COMMIT_SHORT_SHA}/g" provider-service-stage.yml
- mkdir -p $HOME/.kube
- cp $KUBE_CONFIG_STAGE $HOME/.kube/config
- chown $(id -u):$(id -g) $HOME/.kube/config
- export KUBECONFIG=$HOME/.kube/config
- kubectl apply -f provider-service-stage.yml
only:
- stage
我把这个 yaml 脚本合并成两个分支,即 developer 和 stage
但我有两个 yml 文件用于单独的分支(用于开发人员和阶段)
提供商服务-dev.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: provider-app
namespace: stellacenter-dev
labels:
app: provider-app
spec:
replicas: 1
selector:
matchLabels:
app : provider-app
template:
metadata:
labels:
app: provider-app
spec:
containers:
- name: provider-app
image: registry.gitlab.com/stella-center/backend-services/provider-service:<VERSION>
imagePullPolicy: Always
ports:
- containerPort: 8092
imagePullSecrets:
- name: gitlab-registry-token-auth
---
apiVersion: v1
kind: Service
metadata:
name: provider-service
namespace: stellacenter-dev
spec:
type: NodePort
selector:
app: provider-app
ports:
- port: 8092
targetPort: 8092
提供商服务-stage.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: provider-app
namespace: stellacenter-stage-uat
labels:
app: provider-app
spec:
replicas: 1
selector:
matchLabels:
app : provider-app
template:
metadata:
labels:
app: provider-app
spec:
containers:
- name: provider-app
image: registry.gitlab.com/stella-center/backend-services/provider-service:<VERSION>
imagePullPolicy: Always
ports:
- containerPort: 8092
imagePullSecrets:
- name: gitlab-registry-token-auth
---
apiVersion: v1
kind: Service
metadata:
name: provider-service
namespace: stellacenter-stage-uat
spec:
type: NodePort
selector:
app: provider-app
ports:
- port: 8092
targetPort: 8092
我在gitlab cicd yaml脚本中单独提到了但是它显示了类似
的错误
$ kubectl apply -f provider-service-dev.yml
Error from server (NotFound): error when creating "provider-service-dev.yml": namespaces "stellacenter-dev" not found
Error from server (NotFound): error when creating "provider-service-dev.yml": namespaces "stellacenter-dev" not found
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1
它在脚本的最后一行显示错误。它表明找不到命名空间,但那只是存在的。我不知道如何解决这个问题。请帮我整理一下
如果我正确理解你的问题,你可以通过编辑这个 stage 并添加 one variable : CI_COMMIT_BRANCH
Docker 构建命令类似于:docker build -t $IMAGE_TAG -f $CI_COMMIT_BRANCH .
docker_build:
stage: Publish
image: docker:19.03.11
services:
- docker:19.03.11-dind
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $IMAGE_TAG -f $CI_COMMIT_BRANCH .
- docker push $IMAGE_TAG
所以现在您必须将两个 docker 文件都保留在存储库中,但是只需根据 分支 名称更改这些文件的名称即可。
developer:
FROM maven:3.8.3-jdk-11 AS MAVEN_BUILD
COPY pom.xml /build/
COPY src /build/src/
WORKDIR /build/
RUN mvn clean install package -DskipTests=true
FROM openjdk:11
WORKDIR /app
COPY --from=MAVEN_BUILD /build/target/provider-service-*.jar /app/provider-service.jar
ENV PORT 8092
EXPOSE $PORT
ENTRYPOINT ["java","-Dspring.profiles.active=development","-jar","/app/provider-service.jar"]
和
stage:
FROM maven:3.8.3-jdk-11 AS MAVEN_BUILD
COPY pom.xml /build/
COPY src /build/src/
WORKDIR /build/
RUN mvn clean install package -DskipTests=true
FROM openjdk:11
WORKDIR /app
COPY --from=MAVEN_BUILD /build/target/provider-service-*.jar /app/provider-service.jar
ENV PORT 8092
EXPOSE $PORT
ENTRYPOINT ["java","-Dspring.profiles.active=stage","-jar","/app/provider-service.jar"]
rules:variables:
可能很适合这个用例。您可以使用 rules:
更改环境变量。使用它根据分支名称设置 dockerfile 的位置。将此变量传递给 docker build
中的 -f
标志,使其使用该 dockerfile。
docker_build:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
variables:
DOCKERFILE_PATH: "Dockerfile"
- if: '"$CI_COMMIT_BRANCH == "stage"'
variables:
DOCKERFILE_PATH: "staging.Dockerfile"
# and so on
script:
# ...
- echo "Building dockerfile: ${DOCKERFILE_PATH}"
- docker build -t registry.tld/myimage:latest -f "$DOCKERFILE_PATH" .
我有 GitLab ci/cd yaml 文件脚本
services:
- docker:19.03.11-dind
workflow:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH == "developer" || $CI_COMMIT_BRANCH == "stage"|| ($CI_COMMIT_BRANCH =~ (/^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i))
when: always
- if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH != "developer" || $CI_COMMIT_BRANCH != "stage"|| ($CI_COMMIT_BRANCH !~ (/^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i))
when: never
stages:
- build
- Publish
- deploy
cache:
paths:
- .m2/repository
- target
build_jar:
image: maven:3.8.3-jdk-11
stage: build
script:
- mvn clean install package -DskipTests=true
artifacts:
paths:
- target/*.jar
docker_build:
stage: Publish
image: docker:19.03.11
services:
- docker:19.03.11-dind
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $IMAGE_TAG .
- docker push $IMAGE_TAG
deploy_dev:
stage: deploy
image: stellacenter/aws-helm-kubectl
before_script:
- aws configure set aws_access_key_id ${DEV_AWS_ACCESS_KEY_ID}
- aws configure set aws_secret_access_key ${DEV_AWS_SECRET_ACCESS_KEY}
- aws configure set region ${DEV_AWS_DEFAULT_REGION}
script:
- sed -i "s/<VERSION>/${CI_COMMIT_SHORT_SHA}/g" provider-service-dev.yml
- mkdir -p $HOME/.kube
- cp $KUBE_CONFIG_DEV $HOME/.kube/config
- chown $(id -u):$(id -g) $HOME/.kube/config
- export KUBECONFIG=$HOME/.kube/config
- kubectl apply -f provider-service-dev.yml
only:
- developer
deploy_stage:
stage: deploy
image: stellacenter/aws-helm-kubectl
before_script:
- aws configure set aws_access_key_id ${DEV_AWS_ACCESS_KEY_ID}
- aws configure set aws_secret_access_key ${DEV_AWS_SECRET_ACCESS_KEY}
- aws configure set region ${DEV_AWS_DEFAULT_REGION}
script:
- sed -i "s/<VERSION>/${CI_COMMIT_SHORT_SHA}/g" provider-service-stage.yml
- mkdir -p $HOME/.kube
- cp $KUBE_CONFIG_STAGE $HOME/.kube/config
- chown $(id -u):$(id -g) $HOME/.kube/config
- export KUBECONFIG=$HOME/.kube/config
- kubectl apply -f provider-service-stage.yml
only:
- stage
我把这个 yaml 脚本合并成两个分支,即 developer 和 stage 但我有两个 yml 文件用于单独的分支(用于开发人员和阶段)
提供商服务-dev.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: provider-app
namespace: stellacenter-dev
labels:
app: provider-app
spec:
replicas: 1
selector:
matchLabels:
app : provider-app
template:
metadata:
labels:
app: provider-app
spec:
containers:
- name: provider-app
image: registry.gitlab.com/stella-center/backend-services/provider-service:<VERSION>
imagePullPolicy: Always
ports:
- containerPort: 8092
imagePullSecrets:
- name: gitlab-registry-token-auth
---
apiVersion: v1
kind: Service
metadata:
name: provider-service
namespace: stellacenter-dev
spec:
type: NodePort
selector:
app: provider-app
ports:
- port: 8092
targetPort: 8092
提供商服务-stage.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: provider-app
namespace: stellacenter-stage-uat
labels:
app: provider-app
spec:
replicas: 1
selector:
matchLabels:
app : provider-app
template:
metadata:
labels:
app: provider-app
spec:
containers:
- name: provider-app
image: registry.gitlab.com/stella-center/backend-services/provider-service:<VERSION>
imagePullPolicy: Always
ports:
- containerPort: 8092
imagePullSecrets:
- name: gitlab-registry-token-auth
---
apiVersion: v1
kind: Service
metadata:
name: provider-service
namespace: stellacenter-stage-uat
spec:
type: NodePort
selector:
app: provider-app
ports:
- port: 8092
targetPort: 8092
我在gitlab cicd yaml脚本中单独提到了但是它显示了类似
的错误$ kubectl apply -f provider-service-dev.yml
Error from server (NotFound): error when creating "provider-service-dev.yml": namespaces "stellacenter-dev" not found
Error from server (NotFound): error when creating "provider-service-dev.yml": namespaces "stellacenter-dev" not found
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1
它在脚本的最后一行显示错误。它表明找不到命名空间,但那只是存在的。我不知道如何解决这个问题。请帮我整理一下
如果我正确理解你的问题,你可以通过编辑这个 stage 并添加 one variable : CI_COMMIT_BRANCH
Docker 构建命令类似于:docker build -t $IMAGE_TAG -f $CI_COMMIT_BRANCH .
docker_build:
stage: Publish
image: docker:19.03.11
services:
- docker:19.03.11-dind
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $IMAGE_TAG -f $CI_COMMIT_BRANCH .
- docker push $IMAGE_TAG
所以现在您必须将两个 docker 文件都保留在存储库中,但是只需根据 分支 名称更改这些文件的名称即可。
developer:
FROM maven:3.8.3-jdk-11 AS MAVEN_BUILD
COPY pom.xml /build/
COPY src /build/src/
WORKDIR /build/
RUN mvn clean install package -DskipTests=true
FROM openjdk:11
WORKDIR /app
COPY --from=MAVEN_BUILD /build/target/provider-service-*.jar /app/provider-service.jar
ENV PORT 8092
EXPOSE $PORT
ENTRYPOINT ["java","-Dspring.profiles.active=development","-jar","/app/provider-service.jar"]
和
stage:
FROM maven:3.8.3-jdk-11 AS MAVEN_BUILD
COPY pom.xml /build/
COPY src /build/src/
WORKDIR /build/
RUN mvn clean install package -DskipTests=true
FROM openjdk:11
WORKDIR /app
COPY --from=MAVEN_BUILD /build/target/provider-service-*.jar /app/provider-service.jar
ENV PORT 8092
EXPOSE $PORT
ENTRYPOINT ["java","-Dspring.profiles.active=stage","-jar","/app/provider-service.jar"]
rules:variables:
可能很适合这个用例。您可以使用 rules:
更改环境变量。使用它根据分支名称设置 dockerfile 的位置。将此变量传递给 docker build
中的 -f
标志,使其使用该 dockerfile。
docker_build:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
variables:
DOCKERFILE_PATH: "Dockerfile"
- if: '"$CI_COMMIT_BRANCH == "stage"'
variables:
DOCKERFILE_PATH: "staging.Dockerfile"
# and so on
script:
# ...
- echo "Building dockerfile: ${DOCKERFILE_PATH}"
- docker build -t registry.tld/myimage:latest -f "$DOCKERFILE_PATH" .