As developers, we all know that it is a good idea to reuse code as much as possible. We all know the DRY mantra - Don't Repeat Yourself. Functions,...
For further actions, you may consider blocking this person and/or reporting abuse
Hi, thanks for sharing!
What if I want to re-use a series of scripts in my jobs? I know you could write separate scripts to files e.g.
scripts/deploy.sh
and call its execution whenever you need it in the.gitlab-ci.yml
file.The
extends: .my-reusable-job
sentence could be overwritten.Only prints "Hello from testing job". My specific question is if I can use extends without overwriting it later?
Hi Gregori! Thanks for posting your question :)
I think this is an intended feature for
extends
. If you want the reusable script to not be overwritten, there's 2 ways I can think of:before_script
orafter_script
for reusable commands so that they're appended before or after the new script:This thus prints "First, hello world" and "Second, hello world, again!"
This will print the same 2 lines just like the first approach.
Let me know if you have any questions!
Thankssssss, it works 🙏🏾🙏🏾🙏🏾🙏🏾
Awesome! 🎉 I'm happy to help :)
Hey Minh, that’s a well written article about GitLab-Ci templates functionality, and the first which is valuable. Why? because it just shows what is hidden by this GitLab term
ci template
, it’s like that you can search for this term and you end opening tabs over tabs because you find articles which all talk about this feature - but in a way which is so useless that you just learn „yes that is what I need now, they can do it, it’s existing...“ but they all forget the main point, you want to see how to implement / use it / done real work with it.Please write more - I like your way, to show me „ah okay, we have a Feature...“, „... and here you use it like this“, „do that..“ and you get it explained in one article without thousand additional links which you have to open to see a snippet, additional documentations on GitLab.com which all show you nothing usefully, and in the end you read a lot but you have no clue how to use this feature.
You got it done, in a way I like it very much - to write it down, on just one paper A4/letter which shows me how to implement and work with it.
So thanks, and how I can inspire you to write more articles, because you are do it great.
Thank you Tobias for the kind words! You definitely inspired me to write more :D
I agree with you that articles around CICD is usually limited to being generic & theoretical. The reason, I think, is because it requires the author to have real hands-on experience on the tech (ie: gitlab ci/jenkins/etc) to really understand how to apply it in the real world. There are many implicit components to building a pipeline that only by reading the docs certainly isn't enough.
I'll try to write more "how to..." articles like this one in the future and hope to share my own insights on the subject (which I think is crucial to make an article useful)
If possible, please let me know what topic you'd like to read more from. Thanks!
I have few or one idea, as a follow up article, you could show how to make a a pipeline more generic, for example I have a pipeline for python which is also possible to do specific python projects like django, because there isn't to much difference. I use there often one-liner shell script steps like:
and then also how a reusable template can be used look, and used. For example:
my Project pipeline looks like this:
I have only to overwrite one variable to select the correct directory which is to be used to test. That I have to set the variable has a repository structure semantic, I could also have all python project in the same directory of a new repository, but I do it like this. -because I will use the same template again for the same repository but with a different directory (to release python packages to pypi, we refactored some django-apps into python packages to reuse them).
the interesting thing which I have in mind is maybe this:
Another topic which is often not really clear is how to versioning with a pipeline, how can we trigger a release (manual by API, or by a specific trigger like changed file) in the way that we have not to do a change in the gitlab-ci.yml file (to activate release button, and then deactivate release button)...
Releasing and Versioning is often confusing topic when I look into development teams, mostly they have per project only one package which they "release" what they do is just deploy.
hmm, I could write more and more ideas, as I sit here and write down what I have in mind about what you could write, often topics which I have to cover myself, also.
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:
In the pipeline config template, I have extended this job template as
Finally, in the actual project (project A), I have included the pipeline config template as
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?
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 jobThe 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:
In Project A:
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
Thank you for sharing this !!! Very useful !
Thanks for reading! :)