Skip to content

Add project template for Gitlab CI/CD components

Philipp Hahn requested to merge gitlab-community/gitlab:phahn/comp-temp into master
  • Please check this box if this contribution uses AI-generated content (including content generated by GitLab Duo features) as outlined in the GitLab DCO & CLA

What does this MR do and why?

Add project template for Gitlab CI/CD components

File structure

  • .gitlab-ci.yml
    include:
      # include the component located in the current project from the current SHA
      - component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/my-component@$CI_COMMIT_SHA
        inputs:
          job_name: "component job of my-component"
          stage: build
    
    stages: [build, test, release]
    
    ensure-job-added:
      stage: test
      image: badouralix/curl-jq
      script:
        - |
          route="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/pipelines/${CI_PIPELINE_ID}/jobs"
          count="$(curl --silent "$route" | jq 'map(select(.name | contains("component job of my-component"))) | length')"
          [ "$count" -eq 1 ]
    
    create-release:
      stage: release
      image: registry.gitlab.com/gitlab-org/release-cli:latest
      rules:
        - if: $CI_COMMIT_TAG
      script: echo "Creating release $CI_COMMIT_TAG"
      release:
        tag_name: $CI_COMMIT_TAG
        description: "Release $CI_COMMIT_TAG of components repository $CI_PROJECT_PATH"
  • LICENSE
    MIT License
    
    Copyright (c) 2024 GitLab B.V.
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
  • README.md
    # Gitlab component template
    
    Includes a MVC structure to help you get started with [Gitlab CI/CD components](https://docs.gitlab.com/ee/ci/components/).
    
    - [ ] Replace this [README.md](README.md)
    - [ ] Replace [LICENCE](LICENCE)
    - [ ] Rename and adapt [templates/my-component.yml](templates/my-component.yml)
    - [ ] Adapt [.gitlab-ci.yml](.gitlab-ci.yml)
  • templates/my-component.yml
    spec:
      inputs:
        data:
          default: "Hello world!"
        allow_failure:
          default: false
          type: boolean
        job_name:
          default: job
        stage:
          default: test
    
    ---
    $[[ inputs.job_name ]]:
      image:
        name: busybox:latest
      stage: $[[ inputs.stage ]]
      script:
        - echo "$[[ inputs.data ]]"
      allow_failure: $[[ inputs.allow_failure ]]

Screenshot

Screenshot_2024-06-12_at_5.04.46_PM

How to set up and validate locally

Create a new project using the "Gitlab CI/CD component" template.

Edited by Marcel Amirault

Merge request reports