Gitlab Components using artefacts and job needs for order

I am currently experimenting with migrating to Gitlab Components, however it seems there are a few things that are not very clear.

I want to be able to use a component, however for the component to run it is necessary that previous jobs are done, like for example I want to scan a container image, but the build and package jobs (not stages, we use stageless) have to be done… how should I define the order with the component… I hacked up a modified component that uses needs: and an input, however this only work for one job, not for a chain of jobs… so for example if the build and package job are done I have no way

include:
  - component: gitlab.ecdevops.eu/cc/catalog/simple-component@0.0.3
    inputs:
      needs: build-18
  - component: gitlab.ecdevops.eu/cc/catalog/simple-component@0.0.1

build-20:
  image: webmutation/docker-gitlab-build-sonar:node20
  cache: {}
  dependencies: []
  artifacts:
    when: always
    paths:
    - $CI_PROJECT_DIR/build.log  
  script: 
    - |
      node -v

echo-bella:
  image: webmutation/docker-gitlab-build-sonar:node20
  script:
    - echo "Hello, Bella!"

build-18:
  image: webmutation/docker-gitlab-build-sonar:node18
  cache: {}
  dependencies: []
  needs: ["build-20","echo-bella"]
  artifacts:
    when: always
    paths:
    - $CI_PROJECT_DIR/build.log  
  script: 
    - |
      node -v

works fine, but if I want to depend on two jobs instead of one, I am not able to do this, it return invalid

include:
  - component: gitlab.ecdevops.eu/cc/catalog/simple-component@0.0.3
    inputs:
      needs: ["build-18", "build-20"]
  - component: gitlab.ecdevops.eu/cc/catalog/simple-component@0.0.1

build-20:
  image: webmutation/docker-gitlab-build-sonar:node20
  cache: {}
  dependencies: []
  artifacts:
    when: always
    paths:
    - $CI_PROJECT_DIR/build.log  
  script: 
    - |
      node -v

echo-bella:
  image: webmutation/docker-gitlab-build-sonar:node20
  script:
    - echo "Hello, Bella!"

build-18:
  image: webmutation/docker-gitlab-build-sonar:node18
  cache: {}
  dependencies: []
  needs: ["build-20","echo-bella"]
  artifacts:
    when: always
    paths:
    - $CI_PROJECT_DIR/build.log  
  script: 
    - |
      node -v

In short what I am asking is what is the proper way to plugin a component into the pipeline, and how should artefacts be passed into a component… in a similar way as in a job or is there a special way to do this with inputs

works fine, but if I want to depend on two jobs instead of one, I am not able to do this, it return invalid

You need to change the type of the needs input from type: string (implicit) to type: array.

In short what I am asking is what is the proper way to plugin a component into the pipeline

Using inputs as much as possible is the first recommended way to treat components as isolated units. You could also design components in a way that the consumer indicates the name of the job being added by the component so that you can use that name in other jobs. For example, I want to include a component X (which adds a job X) and I want my other job Y to depend on the included X.

##
# .gitlab-ci.yml
include:
  - component: gitlab.com/myorg/myproject/the-component@1.0.0
    inputs:
      job_name: 'my-job-name'

other-job:
  script: "..."
  needs: [my-job-name]

##
# component YAML
spec:
  inputs:
    job_name:
      default: 'the-component-job'
---
"$[[ inputs.job_name ]]":
  script: "..."

and how should artefacts be passed into a component

You could use needs input as type: array as described above.

2 Likes

Thanks alot for the tips. I am able to progress. I was not aware that the job name could be a variable, that is very handy.

Hi fabio, is input type array supported, it seems it is currently not supported
header:spec:inputs:needs input type unknown value: array
also on this issue Allow "null" and array for inputs to CI components or allow verbatim function (#438328) · Issues · GitLab.org / GitLab · GitLab