根据 Gitlab 中的前端或后端更改构建和部署

Building and Deploying depending on front or backend changes in Gitlab

我开始使用 gitlab CI/CD 管道,但对构建过程的输出有一些疑问,如果我有一个项目(回购)并且在这个项目中我有前端和后端分离按项目结构,例如:

汽车项目

.gitlab-ci.yml

|__FrontEndCarProject

|__BackendCarProject

假设每次我在前端更改某些内容时,我都需要构建它并将其部署到 S3,但不需要构建后端(java 应用程序)并将其部署到弹性beantalk(当我更改后端时反之亦然)..有没有一种方法可以使用 GitLab 检查在哪里进行了更改(FrontEndCarProject/BackendCarProject)并将 .gitlab-ci.yml 重定向到脚本文件取决于是否必须部署到 S3 或 elastic beanstalk?

正在尝试

注意:另一种方法是根据我要部署的位置手动更改 yml 文件..但是有没有办法自动检测并自动执行?

.gitlab-ci.yml

Just to get the idea, heres an example that would run in a linear way, but how can i conditionally build/deploy(depending on my front or backend)? should i keep them in different repos for simplicity? is it a good practice?


variables:
  ARTIFACT_NAME: cars-api-v$CI_PIPELINE_IID.jar
  APP_NAME: cars-api

stages:
  - build
  - deploy

# ONLY Build when front(FrontendCarProject) in changed
build_front:
  stage: build
  image: Node:latest
  script:
    - npm install
  artifacts:
    paths:
      - ./dist

# ONLY build when backend(BackendCarProject) is changed
build_back:
  stage: build
  image: openjdk:12-alpine
  script:
    - ./gradlew build
  artifacts:
    paths:
      - ./build/libs/

# ONLY deploy when front(FrontendCarProject) in changed
deploy_s3:
  stage: deploy
  image:
    name: python:latest
  script:
    - aws configure set region us-east-1
    - aws s3 cp ./build/libs/cars-api.jar s3://$S3_BUCKET/cars-api.jar

# ONLY deploy when backend(BackendCarProject) is changed
deploy_back_end:
  stage: deploy
  image:
    name: banst/awscli
  script:
    - aws configure set region us-east-1
    - aws s3 cp ./build/libs/$ARTIFACT_NAME s3://$S3_BUCKET/$ARTIFACT_NAME
    - aws elasticbeanstalk create-application-version --application-name $APP_NAME --version-label $CI_PIPELINE_IID --source-bundle S3Bucket=$S3_BUCKET,S3Key=$ARTIFACT_NAME
    - aws elasticbeanstalk update-environment --application-name $APP_NAME --environment-name "production" --version-label=$CI_PIPELINE_IID

如果您的前端和后端可以单独构建和部署,那么您可以使用 rules:changes to check if a change happened and need:optional 只部署各自构建的库。

variables:
  ARTIFACT_NAME: cars-api-v$CI_PIPELINE_IID.jar
  APP_NAME: cars-api

stages:
  - build
  - deploy

# ONLY Build when front(FrontendCarProject) in changed
build_front:
  stage: build
  image: Node:latest
  script:
    - npm install
  rules:
    - changes:
        - FrontEndCarProject/*
  artifacts:
    paths:
      - ./dist

# ONLY build when backend(BackendCarProject) is changed
build_back:
  stage: build
  image: openjdk:12-alpine
  script:
    - ./gradlew build
  rules:
    - changes:
        - BackendEndCarProject/*
  artifacts:
    paths:
      - ./build/libs/

# ONLY deploy when front(FrontendCarProject) in changed
deploy_s3:
  stage: deploy
  image:
    name: python:latest
  script:
    - aws configure set region us-east-1
    - aws s3 cp ./build/libs/cars-api.jar s3://$S3_BUCKET/cars-api.jar
  needs:
    - job: build_front
      artifacts: true
      optional: true

# ONLY deploy when backend(BackendCarProject) is changed
deploy_back_end:
  stage: deploy
  image:
    name: banst/awscli
  script:
    - aws configure set region us-east-1
    - aws s3 cp ./build/libs/$ARTIFACT_NAME s3://$S3_BUCKET/$ARTIFACT_NAME
    - aws elasticbeanstalk create-application-version --application-name $APP_NAME --version-label $CI_PIPELINE_IID --source-bundle S3Bucket=$S3_BUCKET,S3Key=$ARTIFACT_NAME
    - aws elasticbeanstalk update-environment --application-name $APP_NAME --environment-name "production" --version-label=$CI_PIPELINE_IID
  needs:
    - job: build_back
      artifacts: true
      optional: true