DEV Community

Discussion on: GitLab CI: Creating your own pipeline template library 👷

Collapse
 
avik735 profile image
Avi Bis

I created a profile just to comment and ask some questions. First of all, thank you for such a well written guide. There are very few articles on GitLab CI on the internet. Most are utterly theoretical and very few provides actual workable examples.

Coming to my question, I followed this guide to create a template repository. I have some job templates in the repo, and a pipeline config template which includes these locally available job templates. I am using the pipeline config as a template in other projects. One of the job templates is a maven build job, which looks like this:

.compile_commit:
  variables:
    POM: 'pom.xml'
  only:
    - branches
  tags:
    - ac-billing
  image: $IMAGE_PREFIX/maven:$MAVEN_TAG
  script:
    - mvn -f $POM $MAVEN_CLI_OPTS clean install $MAVEN_OPTS
  artifacts:
    paths:
      - target/*.zip
      - .m2/repository
    expire_in: 1 hour
Enter fullscreen mode Exit fullscreen mode

In the pipeline config template, I have extended this job template as

include: '/job_templates/compile_commit.gitlab-ci.yml'
compile_commit:
  stage: build
  extends: .compile_commit
  variable:
    POM: pom.xml
Enter fullscreen mode Exit fullscreen mode

Finally, in the actual project (project A), I have included the pipeline config template as

include:
  - project: 'mygroup/ci_templates'
    ref: 'master'
    file: '/job_templates/pipeline.gitlab-ci.yml'

variables:
  POM: 'myfolder/pom.xml'
Enter fullscreen mode Exit fullscreen mode

However, when I run the pipeline in project A, the build process fails to find the pom.xml path and the build fails. What am I doing wrong?

Collapse
 
anhtm profile image
Minh Trinh

Hi Avi, thanks for stopping by! It's amazing to see people like you finding the guide useful and actually following it.

I think the problem is that in your project A, you're declaring the variable POM: 'myfolder/pom.xml' on the pipeline level, and not on the job level. Hence it's perceived as a different variable from the original .compile_commit::variables::POM and GitLab does not parse it into the job

The easiest way to solve this is to make the template config job to be hidden, and then extend it again in Project A and change the variable POM:

In template config:

include: '/job_templates/compile_commit.gitlab-ci.yml'
.compile_commit_config: # make this job hidden
  stage: build
  extends: .compile_commit
  variable:
    POM: pom.xml
Enter fullscreen mode Exit fullscreen mode

In Project A:

include:
  - project: 'mygroup/ci_templates'
    ref: 'master'
    file: '/job_templates/pipeline.gitlab-ci.yml'

compile_commit:
  extends: .compile_commit_config
  variables:
    POM: 'myfolder/pom.xml'  # add your variable inside the new job
Enter fullscreen mode Exit fullscreen mode

Another approach would be to set the POM variable to be at the pipeline level from the beginning (remove it from the job and add it in the pipeline level). Then, override it in your Project A like you did. However, I haven't tested this solution so I can't 100% tell you that it will work. But it's worth trying it out. Let me know if that works also!

Minh