background

In the ci /CD process with GitLab CI build, you will encounter the content of publishing specific projects. This article focuses on how gitLab CI implements publishing on demand!

Project directory and configuration

  • A directory collection for projects for workspace
. ├ ─ ─ the README. Md ├ ─ ─ lerna. Json ├ ─ ─ node_modules ├ ─ ─ package. The json ├ ─ ─ the gitlab - ci. Yml ├ ─ ─ the projects ├ ─ ─ tsconfig. Json └ ─ ─ yarn.lockCopy the code

package.json

Set the workspaces

{... "workspaces": [ "projects/*" ], ...... }Copy the code

lerna.json

{"useWorkspaces": true, "npmClient": "YARN ", "version": "0.0.0"}Copy the code

CI/CD .gitlab-ci.yml

Variables: ROOT_BUILD_PATH: $CI_PROJECT_DIR/build # Paths: # Cache path -node_modules / -. Yarn # build step stages: -install-build-deploy # install_dependency: stage: install # Deliver the file to the next job expire_in Untracked: true expire_in: 1 week script: Untracked: true expire_in: 1 -mkdir -p $root_build_path-yarn install --pure-lockfile --prefer-offline --cache-folder. Yarn # Build project project1 Project1_build: stage: build variables: DIR: 'project1' extends: -.build_project # # build project2 project2_build: stage: build variables: DIR: 'project2' extends: - .build_project only: changes: - projects/project2/src/**/*.ts .build_project: artifacts: untracked: true expire_in: 1 week script: - cd projects/$DIR - yarn build - rm -rf $ROOT_BUILD_PATH/$DIR - cp -r dist/$DIR $ROOT_BUILD_PATH cache: policy: Pull # Forbid upload update cache only: refs: -master # master trigger after submission deploy_project: stage: deploy artifacts: expire_in: 1 week Paths: - $ROOT_BUILD_PATH cache: policy: pull only: refs: - masterCopy the code

conclusion

The post-build artifacts that need to be published are collected when the script is delivered to deploy and then synchronized to the appropriate environment using rsync or other synchronization tools for further operations. The build on demand process is perfectly implemented by doing this, and there are multiple versions of the schema that can be deeply customized through After_Script