Share gitlab-ci.yml between projects

后端 未结 7 1207
面向向阳花
面向向阳花 2020-12-13 21:04

We are thinking to move our ci from jenkins to gitlab. We have several projects that have the same build workflow. Right now we use a shared library where the pipelines are

7条回答
  •  感情败类
    2020-12-13 21:36

    So, i always wanted to post, with what i came up with now:

    Right now we use a mixed approach of @stefan-van-gastel's idea of a shared ci library and the relatively new include feature of gitlab 11.7. We are very satisfied with this approach as we can now manage our build pipeline for 40+ repositories in a single repository.

    I have created a repository called ci_shared_library containing

    1. a shell script for every single build job containing the execution logic for the step.
    2. a pipeline.yml file containing the whole pipeline config. In the before script we load the ci_shared_library to /tmp/shared to be able to execute the scripts.
    stages:
      - test
      - build
      - deploy
      - validate
    
    services:
      - docker:dind
    
    before_script:
      # Clear existing shared library
      - rm -rf /tmp/shared
      # Get shared library
      - git clone https://oauth2:${GITLAB_TOKEN}@${SHARED_LIBRARY} /tmp/shared
      - cd /tmp/shared && git checkout master && cd $CI_PROJECT_DIR
      # Set permissions
      - chmod -R +x /tmp/shared
      # open access to registry
      - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    
    test:
      stage: test
      script:
        - /tmp/shared/test.sh
    
    build:
      stage: build
      script:
        - /tmp/shared/build.sh
      artifacts:
        paths:
          - $CI_PROJECT_DIR/target/RPMS/x86_64/*.rpm
        expire_in: 3h
      only:
        - develop
        - /release/.*/
    
    deploy:
      stage: deploy
      script:
        - /tmp/shared/deploy.sh
      artifacts:
        paths:
          - $CI_PROJECT_DIR/tmp/*
        expire_in: 12h
      only:
        - develop
        - /release/.*/
    
    validate:
      stage: validate
      script:
        - /tmp/shared/validate.sh
      only:
        - develop
        - /release\/.*/
    

    Every project that want's to use this pipeline config has to have a .gitlab-ci.yml. In this file the only thing to do is to import the shared pipeline.yml file from the ci_shared_library repo.

    # .gitlab-ci.yml
    
    include:
      - project: 'ci_shared_library'
        ref: master
        file: 'pipeline.yml'
    

    With this approach really everything regarding to the pipeline lives in one single repository and is reusable. We have the whole pipeline-template in one file, but i think it would even be possible to split this up to have every single job in a yml-file. This way it would be more flexible and one could create default jobs that can be merged together differently for projects that have similar jobs but not every project needing all jobs...

提交回复
热议问题