Jenkins Pipeline Environment Variables: A Comprehensive Guide

  Рет қаралды 50,015

Szymon Stepniak

Szymon Stepniak

Күн бұрын

Пікірлер: 179
@szymonstepniak
@szymonstepniak 4 жыл бұрын
➡️ "Jenkins Pipeline Environment Variables - The Definitive Guide" blog post can be found here: e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/ Timestamps: 00:00 Introduction 00:29 Listing env variables 01:34 Accessing env variables 03:20 Setting env variables 07:45 Overriding env variables 09:50 Using env variable as boolean 12:18 Capturing the output from other steps 14:08 Dealing with null in the env variable
@szymonstepniak
@szymonstepniak 3 жыл бұрын
@Eduard FLORINESCU This is Blue Ocean UI - www.jenkins.io/projects/blueocean/
@yonatanhuber9464
@yonatanhuber9464 3 жыл бұрын
man, I was breaking my head over this for hours. THANK YOU
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Thank you for your kind words, Yonatan! Have a good day!
@harsimranjitsingh591
@harsimranjitsingh591 2 жыл бұрын
Some one please give this guy a medal for the awesome video ❤️👍
@demianpelos
@demianpelos 3 жыл бұрын
Very simple and easy to follow. Now doing pipelines is not that frithing. Thanks for taking the time to make this material
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Thank you so much for your kind words! Take care, and have a good day!
@BE55Roberts
@BE55Roberts 3 жыл бұрын
Clear and concise explanation of variables within different contexts. Just what I was looking for!
@szymonstepniak
@szymonstepniak 3 жыл бұрын
I'm so happy to hear that, Bradley! Thanks for your kind words and have a good day!
@marwanalturky
@marwanalturky 4 жыл бұрын
thank you for taking some time and teach people something very valuable.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thank you so much for the kind words, Memis! I'm happy to hear you find this video valuable. Have a good day!
@shashankmarri1056
@shashankmarri1056 2 жыл бұрын
Lovely and brilliant video. Precise to the point. Please make more videos on other devops concepts like Chef and Ansible.
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Thank you!
@safaricourse3023
@safaricourse3023 4 жыл бұрын
You are awesome.I learnt a lot from this video. I would like to see more videos of yours and learn new things in Jenkins Pipeline.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thanks for the kind words! I would recommend subscribing to my channel if you haven't already - more Jenkins Pipeline related content coming soon :)
@safaricourse3023
@safaricourse3023 4 жыл бұрын
@@szymonstepniak Thank you, I have subscribed it :) I request if you could please make a video on Shared Pipeline Libraries.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
I have it on my list already :) Thanks for helping me in shaping the content for the upcoming videos 👍
@narayan89k
@narayan89k 3 жыл бұрын
Awesome and perfect video to learn Jenkins environment variable concepts and various types of it. Author has very clearly explained how they can be set, referred and overridden in groovy and also how they can be used in shell block. Thank you very much for this video ... Szymon ....
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Thank you for your kind words, Narayanan! Have a good day!
@cqyzsssyzm
@cqyzsssyzm Жыл бұрын
Thank you very much for the explanation, it is so easy to make a mistake...
@szymonstepniak
@szymonstepniak Жыл бұрын
Thanks for your kind words, Tycho! Take care, and have a good day!
@hitabratabhainsa1719
@hitabratabhainsa1719 2 жыл бұрын
Best tutorial for Jenkins...
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Thank you so much for your kind words! Take care, and have a good day!
@pandorasbox5576
@pandorasbox5576 4 жыл бұрын
Hey Szymon.. Amazing Explanation!! This is the best video I have come across today.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thanks for the kind words! Is there anything in this topic you would like to hear more about? I would love to hear your opinion and create next videos to match your expectations much closely. Thanks in advance!
@nikhileshsaggere
@nikhileshsaggere 3 жыл бұрын
Excellent tutorial and explaination, please add more such lucid and informative videos. Practical examples on both sh and bat commands in the script block would be helpful.🙏🏻
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Thank you for your kind words, Nikhilesh! Have a good day!
@prakashperam
@prakashperam Жыл бұрын
This is super detailed. Thank you very much
@szymonstepniak
@szymonstepniak Жыл бұрын
I'm happy to hear that, Prakash! Take care, and have a good day!
@1986smooth1986
@1986smooth1986 4 жыл бұрын
Thank you for your efforts. This tutorial is amazing.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thank you, Khuswant! I'm glad to hear it was useful to you.
@devnullskull
@devnullskull 2 жыл бұрын
I second this. By far the best Jenkins tutorial video I've seen. Already looking to see what other videos you've made as this one just clicked with me and I said "ah, someone who gets it, and works and explains in a way that is very relatable."
@too_bad
@too_bad 3 жыл бұрын
Excellent video. Thank you!
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Thank you so much for your kind words! Take care, and have a good day!
@charank7852
@charank7852 2 жыл бұрын
Hi Excellent tutorial and explaination.I learnt a lot from this video.
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Glad it was helpful! Take care, and have a good day!
@aadyanaanayacooking7863
@aadyanaanayacooking7863 2 жыл бұрын
Thank you so much . I was breaking my head .❤️❤️❤️
@szymonstepniak
@szymonstepniak 2 жыл бұрын
You’re welcome 😊 Take care, and have a good day!
@vigneshs4219
@vigneshs4219 4 жыл бұрын
Excellent . Need more videos👍
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thank you for your kind words, Vignesh! Have a good day!
@whogashaga
@whogashaga 3 жыл бұрын
Thanks for sharing this! It helps me a lot.
@szymonstepniak
@szymonstepniak 3 жыл бұрын
I'm so glad to hear that! Take care, and have a good day!
@dinkum375
@dinkum375 3 жыл бұрын
The best guide on Jenkins Env Variables I have seen so far. Can you please also guide how to pass environment variables to a python script (that I have checked out from SCM) in Jenkins?
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Thanks for your kind words, Muhammad! I guess you can read such a variable inside your Python script using `os.environ.get(name)`, or you can simply pass the value of the variable as an argument for the Python script (e.g. sh "python script.py '${env.VAR_NAME}'" - just make sure you wrap the env variable value with the quotes, so it is treated as a single string.) Good luck, have a good day!
@KernelFault
@KernelFault 4 жыл бұрын
Thank you for your videos. They are very helpful.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thanks for the kind words, Dave! I'm glad to hear you find them helpful. Have a good day!
@daianacv6536
@daianacv6536 4 жыл бұрын
Szymon, thank you!! It's really awesome your explanation!!
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thanks for your kind words, Daiana. Have a good day!
@no1sploit529
@no1sploit529 3 жыл бұрын
Great video Man !!
@vaddisivakumar
@vaddisivakumar 2 жыл бұрын
Great explanation 👌🏻
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Thanks for your kind words! Take care, and have a good day!
@loudouncps2107
@loudouncps2107 3 жыл бұрын
Excellent tutorial , very useful
@szymonstepniak
@szymonstepniak 3 жыл бұрын
I'm happy to hear you find it helpful. Take care, and have a good day!
@alea9140
@alea9140 9 ай бұрын
Highly informative. Could you do the same with Jenkins YAML pipelines?
@tyagisong
@tyagisong 2 жыл бұрын
Thanks for explaining my many doubts
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Thanks for your kind words! Take care, and have a good day!
4 жыл бұрын
Your videos are great, I like every detail.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thank you, Roberto! I'm happy to hear you like it 😎👍
@OurHappyWorld
@OurHappyWorld 3 жыл бұрын
Nice Explanation with soothing music. Do you have any videos that explains complex scripted pipelines?
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Hi Prasad, thanks for the comment. All Jenkins pipeline related videos are available in this playlist - kzbin.info/www/bejne/l3e1m6GVadOVY7M&ab_channel=SzymonStepniak You can take a look and check if it contains content you are looking for. Have a good day!
@elangovans310
@elangovans310 4 жыл бұрын
Previously I had written in a scripted pipeline. After watching your tutorial I have understood the use of the declarative pipeline and now I am very interested in working on this. Thanks, @Szymon Stepniak
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thank you for your kind words, Elangovan! If you are interested in the declarative pipeline and scripted pipeline differences, I made a video about it some time ago - kzbin.info/www/bejne/e5rCf55nadulq6c Thanks again, and have a good day!
@WaldiShrek
@WaldiShrek 2 жыл бұрын
Very good tutorial. But how inject a global environment variable so it can be used also by different build jobs? E.g. for locking other builds while a special build is running
@szymonstepniak
@szymonstepniak 2 жыл бұрын
You can't define environment variable that goes beyond the context of the current job/build. I'm guessing that you are looking for something like lockable resources - www.jenkins.io/doc/pipeline/steps/lockable-resources/ This thing allows you to lock specific "resource", and if there is concurrent job running that also tries to acquire the same lock, it will have to wait until the other job releases it. Hope it helps!
@dmitriyengineer2441
@dmitriyengineer2441 2 жыл бұрын
Yo man your explanation is so good. Come on guys, 2 more likes for 1k😁
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Thanks, Dmitriy! :) Take care, and have a good day!
@ashok.9761
@ashok.9761 4 жыл бұрын
Well explained sir ... Easy to understand .. Can you make it one video dockerfile
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Hi! Thanks for the kind words. Regarding Dockerfile video - is there anything specific that interests you most? I added this topic to my backlog, but it would be very helpful if you could share some more thoughts. Thanks in advance, and have a nice day!
@chintalapativenkataramarahul
@chintalapativenkataramarahul 3 жыл бұрын
Can you please explain the difference between src and vars folders? Which one to use in which case and why?
@andreymelnichenko9373
@andreymelnichenko9373 4 жыл бұрын
amazing video! Thanks a lot! This is exactly what I looking for!
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thank you for your kind words, Andrey! I'm glad to hear you've found exactly what you were looking for. Have a good day!
@andreymelnichenko9373
@andreymelnichenko9373 4 жыл бұрын
@@szymonstepniak Also, would be grate to get tutorial about multibranch pipeline project based on git hook trigger, and AWS integration. For me, as automation engineer it is actual question cause on my project I need create test infrastructure without DevOps specialist.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
@@andreymelnichenko9373 Thanks for suggesting the topic. I plan to create a full video dedicated to creating a complete CI/CD pipeline for a Java project, probably with some AWS parts as well. What type of integration with AWS are you interested in most? I could put it to that video if it fits the general idea. Thanks in advance, and have a good day!
@Walruz1000
@Walruz1000 2 жыл бұрын
Thanks, very helpful content. I have recently started working with Jenkins again after 2 years using only code build and code pipeline and these videos are really helping me get back up to speed quickly.
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Thanks for your kind words! And good luck with your Jenkins experience! :)
@dumpalpr
@dumpalpr 4 жыл бұрын
Very informative video sir as always. Would it be possible to make a video which cover below topics. 1) Full end to end pipe line with any sample application which depict a real time implementation. Different states involves Builds maintenance Reporting ..etc 2) Typical Jenkins environment set-up in real production world. Kind of architectural representation. Thank you again.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Hi, Pradeep! Thank you very much for the comment and topics suggestion. I'm going to create a video on setting up the full CICD pipeline for Java projects, and maybe Node.js projects as well in the future. As for the production-ready Jenkins infrastructure, I have something like this on the backlog already - added your comment as well. Thanks a lot for the suggestions! I will do my best to publish the videos you suggested as soon as possible. Have a good day!
@dumpalpr
@dumpalpr 4 жыл бұрын
@@szymonstepniak Thank you for considering my input. Also can you please shed some light around best practices in managing CI/CD in a typical agile projects, Like how you create or maintain build definitions for different environment. Regards, Pradeep
@szymonstepniak
@szymonstepniak 4 жыл бұрын
@@dumpalpr Yeah, I think the full CI/CD pipeline example for an exemplary Java project will show a few best practices. It's hard to explain them without a specific context. Thanks again for the insightful comment, have a good day!
@jinkahari
@jinkahari 3 жыл бұрын
Thanks for your video stepniak. How to write the script to delete the workspace using environment variable like boolean?
@Jonathanmismamente
@Jonathanmismamente 4 жыл бұрын
This is awesome and useful video!! Thanks you!
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thanks for the comment, Jonathan! 👍 I'm happy to hear you find it useful 😀
@danvyr
@danvyr 4 жыл бұрын
Great explanation!
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thanks for the comment, Vitali! 👍 It helps ranking video higher, and that means a lot to me. I'm happy to hear you find the video useful. I hope you will find other videos useful as well 🙂
@pavangummadi3378
@pavangummadi3378 4 жыл бұрын
Hi Szymon Stepniak tutorial is amazing, can you please tell me about the green demo console which you are using to test pipeline? is it blue ocean?
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Hi Pavan! Thanks for the comment 🙏 That's correct, this is Blue Ocean UI.
@CarlosManuel-qf7mb
@CarlosManuel-qf7mb 3 жыл бұрын
excellent video. I was looking but I couldn't find how to use the user running the pipeline and save it in an environment variable. is that possible without using Jenkins plugins?
@szymonstepniak
@szymonstepniak 3 жыл бұрын
You can use Build User Vars plugin to access such information through environment variables - plugins.jenkins.io/build-user-vars-plugin/ The documentation page contains pipeline example, you need to use the "wrap" block to make those variables exposed. Good luck and have a good day!
@srinivasgowda9335
@srinivasgowda9335 3 жыл бұрын
@Szymon Stepniak thanks for your wonderful video. I had one question. Say suppose i have declared a global environment variable which is empty or null. environment { a= "" } This will be overwritten in next stages and i am able to get those. steps{ script { a= 'b' } } a= b am able to get this. But i am failing to get that environment variable in the post step of extended email. its always printing null value. is there a way i can read that modified value? post{ always{ $a } } a value is null or empty. how do i get the modified value? ie a = b
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Hi Srinivas! It won't work as you expect as far as I remember. In your example, you don't override the env variable in the script block, but you create a local variable `a` inside the script block. The environment variable once initialized inside the "environment" block cannot be overridden (with a few exceptions like overriding env variable in the withEnv block for a limited scope.) Check out my blog post for more detailed information - e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/#overriding-environment-variables The one workaround I could suggest you to do it to skip initializing this variable in the environment block and creating this variable inside the script block. In this case, Jenkins pipeline will allow you to override such a variable. I explain this example in my blog post as well. Good luck and have a good day!
@srinivasgowda9335
@srinivasgowda9335 3 жыл бұрын
@@szymonstepniak Thanks so much for your prompt response. Yes before posting i had seen that blog thank you. One more thing i wanted to ask and get re-confirmed Question--> is there a way to export envars to a file like this Jenkins-url/lastSuccessfulBuild/injectedEnvVars/export/file.txt same in pipeline jobs? i am able to do in freestyle jobs, just wanted to check whether we can do same in pipeline jobs. Many blogs i read but i found that answer is no. Is this true? or do you have any suggestion on this?
@szymonstepniak
@szymonstepniak 3 жыл бұрын
@@srinivasgowda9335 I have no suggestion in this area, I've never done anything like that in any of my pipelines. I would look into issues.jenkins.io/browse/JENKINS-42614 and ask a question there if the workaround posted in the comments does not work as expected. Good luck!
@srinivasgowda9335
@srinivasgowda9335 3 жыл бұрын
@@szymonstepniak Thank you so much :)
@pavelow36
@pavelow36 3 жыл бұрын
I'm currently struggling with a Jenkins pipeline using the Extended Choice plugin and adding it into the Jenkins script. I am unable to correctly create a pipeline script using pure declarative script. But I am able to do it if I go through the UI. Thank you.
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Hi there! Do you have any specific question related to the Jenkins pipeline environment variables? Have a good day!
@pavelow36
@pavelow36 3 жыл бұрын
@@szymonstepniak I think I'm getting it now. Variables are just not behaving correctly during expansion. For example, the Extended Choice parameters selected will create a comma delimited list of strings like Cars="ford,toyota,jaguar", but when I do like, sh '''echo ${Cars} | sed "s/,/_/g" ''' to replace all the commas with underscores it does not do it. Thanks Szymon!
@szymonstepniak
@szymonstepniak 3 жыл бұрын
@@pavelow36 Every env variable is represented as a string, so if you end up storing a list of elements, it will be represented as a string value. If you use the "sh" step, make sure you set "returnStdout: true" as explained in the blog post e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/. Alternatively, you can make the replacement inside the script block and using Groovy code like Cars.replaceAll(",", "_"). Good luck!
@pavelow36
@pavelow36 3 жыл бұрын
@@szymonstepniak Thank you! will try from my end. Please continue to teach. You are a good teacher.
@dipi411
@dipi411 2 жыл бұрын
it's very informative
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Glad to hear that, Bhabani! Take care, and have a good day!
@АлексейЗорин-й9с
@АлексейЗорин-й9с 3 жыл бұрын
Hi. Great video. I was looking for an answer on how to set up a pipeline in jenkins and came across your video. It is very interesting and understandable. But I still couldn't solve my problem. Can I ask you for help? At first glance, the task is simple. But I cannot find the answers on the net for it. If I can ask you for help, it would be great)
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Hi Алексей, what is the problem you faced with the pipeline? Is it related to Jenkins environment variables? Have a good day!
@katejermina
@katejermina 4 жыл бұрын
Very helpful explanation. Thank you Is there a way to stash a variable onto another stage (that uses another agent)?
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thanks, Ali! Env variables are agent-agnostic, which means that you can set a variable in a stage that gets executed on agent A, and then access this env variable in the next stage that gets executed by agent B. Stash/unstash operations are used with files.
@katejermina
@katejermina 4 жыл бұрын
@@szymonstepniakAhhh I see. Thanks again!
@vishweshshouche2698
@vishweshshouche2698 2 жыл бұрын
Hello Szymon - Small question and maybe dumb, how do we get Jenkins UI like you, I have blue ocean plug-ins installed on my laptop as well but did not get able to do get UI like you.
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Hi Vihwesh! This is Blue Ocean UI plugin. Take care, and have a good day!
@vishweshshouche2698
@vishweshshouche2698 2 жыл бұрын
@@szymonstepniak thank you
@ScrolltheNature
@ScrolltheNature 3 жыл бұрын
You make me understand this, do you have autocompletion/linter available for dsl pipelines in any editors which you prefer.
@pablov.santos4689
@pablov.santos4689 4 жыл бұрын
very good, thanks man!!!
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Thanks for the kind words, Pablo! I'm happy to hear you find it useful :)
@Oswee
@Oswee 4 жыл бұрын
Personally i am more interested in interpolation/substitution part of the shell in the Jenkinsfiles. Specifically when i need to have double quotes, slashes, etc in the shell commands.
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Thanks for the feedback, Dzintars! I'm thinking about creating a video that explains Groovy from the Jenkins pipeline syntax perspective and explains how to avoid common pitfalls, including escaping slashes in strings. Stay tuned! :)
@Oswee
@Oswee 3 жыл бұрын
@@szymonstepniak Tnx. That would be great! In the wild i saw it's quite common question.
@szymonstepniak
@szymonstepniak 3 жыл бұрын
@@Oswee Absolutely, I do agree! I see many people treat Jenkins pipeline Groovy syntax as it was a vanila Groovy code which in many cases causes some issues. Hope I will be able to help in this area as well :) Thanks and have a good day!
@harishkashyapm
@harishkashyapm 4 жыл бұрын
Hello Szymon, is it possible to add parallel steps in a pipeline? Each parallel step to run when an environment Boolean parameter is set to true.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Hi Harish! Could you elaborate, please? Correct me if I didn't understand your question correctly, but the answer is - yes. You can use the "when" block (www.jenkins.io/doc/book/pipeline/syntax/#when) inside the "stage" block, no matter if the stage gets executed sequentially or in the parallel. You can use the "when" block either to all parallel stages or you can define separate block for each parallel stage. Let me know if you have run into some specific issue. Will do my best to help you. Have a good day!
@harishkashyapm
@harishkashyapm 4 жыл бұрын
@@szymonstepniak Thanks for the reply, Szymon. I have a Boolean parameter to accept input via a checkbox. I have a set of set of such checkboxes. Each checkbox stands for an environment. If the checkbox is checked, then I would like to deploy code to that environment. Also, I would like to deploy to all the environments in parallel. If you have any suggestions for me to implement this, I world be really grateful.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
@@harishkashyapm You could use either matrix feature (I made a video about it in a bit different context - kzbin.info/www/bejne/oGLPeWashM2omdk) or the declarative parallel configuration. Here is a simple example of using matrix alongside with the boolean checkbox parameters - gist.github.com/wololock/7c747d09e5e75bee8518143e8174a647 And here is a simple example of using declarative parallel stages alongside with the boolean checkbox parameters - gist.github.com/wololock/9e9e0abc55858a735e00c5c9b6e54fe8 Each of them has its pros and cons. The matrix is better if you want to follow the DRY rule (don't repeat yourself) - you just define axes and a single stage. The only problem with this approach is that it does not produce clean UI and you need to hover at the axis to check what environment it is. (You can't use the axis variable in the stage name unfortunately.) On the other hand, the second approach produces much cleaner and simpler UI, but it requires some repetition. I would choose whatever works for you better. I hope it helps.
@cptechno
@cptechno 3 жыл бұрын
I'm using Jenkins free version. I don't see the screens that you are using in this demo. Can you explain your environment for Jenkins.
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Hi there! The GUI you can see in the video is provided by the Blue Ocean UI plugin. You can install it from the Jenkins Plugin Manager. Good luck, and have a good day!
@yusufzainee
@yusufzainee 3 жыл бұрын
@snymon Stepniak, I have a problem. Im trying to edit the Path env variable, where in, in the pipeline script, i can see the changed env value, but when the call lands to a java step class and i try to system.getenv("PATH"), i dont get the edited value. Why is that so and how can i overcome this problem?
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Hi Yusuf! I don't know how you update the PATH env variable, but I'm guessing that you do it using "sh" (or "bat") step. If this is the case, then the updated variable does not get exposed to the global env variables, but gets updated only inside the "sh" script process. You can overcome this limitation by using e.g. withEnv() directive. You can find an example in my blog post: e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/#setting-environment-variables Hope it helps. Have a good day!
@pedadatuber
@pedadatuber 2 жыл бұрын
Would be great to know how withEnv is implemented, could you share a pointer to the source code?
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Hi Peter, here's the source code github.com/jenkinsci/workflow-basic-steps-plugin/blob/master/src/main/java/org/jenkinsci/plugins/workflow/steps/EnvStep.java and the associated unit test github.com/jenkinsci/workflow-basic-steps-plugin/blob/master/src/test/java/org/jenkinsci/plugins/workflow/steps/EnvStepTest.java
@pedadatuber
@pedadatuber 2 жыл бұрын
@@szymonstepniak thanks a lot!
@siddharthmehra8178
@siddharthmehra8178 3 жыл бұрын
Hello Szymon, Thanks for awesome explanation. I have a question, I want to fetch the tags from the git repo and store it in a variable. How can I do that? Below is what I'm doing right now. environment{ GIT_TAG = sh(returnStdout: true, script: "git tag --sort version:refname | grep -i pcc*| tail -1") } Error that I'm getting: + git tag --sort version:refname + grep -i pcc* fatal: Not a git repository (or any parent up to mount point /var/jenkins_home) Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set). + tail -1 [Pipeline] withEnv [Pipeline] { [Pipeline] echo The lastest tag is : null
@ScrolltheNature
@ScrolltheNature 3 жыл бұрын
Thanks, but when i am printing env i am not getting branch name variable & git commit variable although my repo is on gitlab, also i need to get the commit message to select the execution accordingly. currently i am using gitlab api curl to get these values in pipelines.let me know if you any thoughts on this.
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Hmm, I never used Gitlab with Jenkins, so I have no out-of-the-box solution to this problem. I would suggest checking if there is a gitlab plugin for Jenkins and maybe it is required to install it to get those variables exposed correctly. Or maybe it's not yet supported and there is a corresponding JIRA issue you may find in your research. Good luck and have a good day!
@guvvalamadhav4903
@guvvalamadhav4903 4 жыл бұрын
This video helped me a lot sir, Is there any way to contact you personally sir ?
@szymonstepniak
@szymonstepniak 4 жыл бұрын
I'm glad to hear you found this video helpful. Regarding your question, my contact email is mentioned in the video description and in the channel information page. Have a good day!
@guvvalamadhav4903
@guvvalamadhav4903 4 жыл бұрын
@@szymonstepniak Thanks for the reply sir
@SebastianPlagge
@SebastianPlagge 4 жыл бұрын
Your video is awesome (and nice music in the background). Started teaching myself this stuff but I still make many mistakes :(. Right now I have a problem with an environmental variable "CHANGE_TARGET". When my team creates a Pull Request, Jenkins automatically gets notified and starts the pipeline. However, for several reasons, I want to run a specific step just, if the CHANGE_TARGET is /^release\/\d{2}\.\d{1,2}$/ (e.g. release/20.6). This works so far, but when there is no Pull Request there is no CHANGE_TARGET and I get the error No such property CHANGE_TARGET for class: groovy.lang.Binding. I know what's the problem but I do not know a good way, to deal with it so far. I want a when condition, that if the change target is correct, pipeline should run this stage... But so far I just go from error to error :( Do you have an idea?
@SebastianPlagge
@SebastianPlagge 4 жыл бұрын
By the way. I just realised, that the Groovy Regular Expression Guide and Jenkins Pipeline Environment Guides are by you. I used them a lot the last months. Thank you so much! It was super helpful!
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Hi, Sebastian! Thanks for your kind words! I'm happy to hear both video and blog posts were useful to you 🙂 Regarding your question, I would try using "changeRequest" to define your expected conditions. Take a look at this simple example - gist.github.com/wololock/508e1220ecb950d6574d07af12e35375 Here I define a following condition for the "Test" stage: execute when the pull request to "develop" is created and when it comes from the branch like TEST-123. You don't have to specify both "target" and "branch", you can use just a single one. Or even you can use a condition like "changeRequest()" without any additional options - in this case, the condition is true to any pull request. I hope it helps you solve the problem. Happy coding!
@SebastianPlagge
@SebastianPlagge 4 жыл бұрын
@@szymonstepniak Thank you very much. Your example was very helpful. It took me a while to get the REGEXP right but now it works. It also took me a while to understand that "branch" (at least in my case) was the change target and not the branch name. I am still pretty unsure when to use single / double quotes. When to use echo and when to use sh. Everything works but I think I can improve my pipeline a lot. :D
@szymonstepniak
@szymonstepniak 4 жыл бұрын
@@SebastianPlagge Yes, I should also post this link to give you more information about "changeRequest" condition type - jenkins.io/doc/book/pipeline/syntax/#when All its options refer to CHANGE_* variables, so "branch" in this case means CHANGE_BRANCH. When it comes to regular expression, it is much easier to use Groovy's backslash string feature, so you don't have to worry about escaping characters. I used "echo" and "sh" steps in this video just to illustrate how env variables can be used in both cases. It should not be confusing when to use one or another. When you want to print something to the console, use the "echo" step. When you need to execute some shell command or script, the "sh" step (for Linux nodes) is your best choice. Keep practicing and learning, the time for improvements will come :) I can tell you that I develop a Jenkins shared library that produces pipelines for engineers in the company I work for, and it's been 2 years now and we still learn new things and try to improve what does not work as expected. Just be patient and let your Jenkinsfile do its job :) Good luck!
@SebastianPlagge
@SebastianPlagge 4 жыл бұрын
@@szymonstepniak thanks, I had the information but I read too fast and did not see it. I really need to practice more and I will do some exercises with Groovy's backslash feature. End of December we still used SVN + Hudson. I was working over Christmas and spontanously started to migrate SVN to GIT and Hudson to Jenkins. Since we have around 40 repos and complex ANT scripts for versioning I had a lot to do and learn. Since the devs of my team are pretty lazy I started to put a lot of stuff in our pipelines. It takes care about giving the correct version, it creates tags and pushes everything to git, it checks if branches are named correctly. One thing I want to do are shared libraries (I read a bit about it but had no time so far). Right now it is so much work to fix bugs. I need to update all repos and all branches (at least the current release branch). It is annoying and takes a lot of time. Future goals are sonarqube / quality gates. Everything works fine but I think there is a lot of room for improvement ;)
@sirdomino1000
@sirdomino1000 Жыл бұрын
Hello do you provide training ?
@szymonstepniak
@szymonstepniak Жыл бұрын
No, I dont. Take care, and have a good day!
@theentertainmentetc64
@theentertainmentetc64 2 жыл бұрын
Hi , Your knowledge sharing is awesome , need some help on the below task Need to generate a test cases TestNGXML report and need to push to XRAY using restapi ... All these things should be done by pipeline and shell script .. can you give me some documentation as i am not getting proper knowledge from xray official website
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Hi! There's nothing I could point you directly to that comes to my mind. I have no experience with the tools you mentioned in the comment. Good luck, and have a good day!
@jafarshaik5160
@jafarshaik5160 2 жыл бұрын
Hello stepniak sir do u have any course on Jenkins scripting in udemy please
@szymonstepniak
@szymonstepniak 2 жыл бұрын
Nope, I have no courses on Udemy.
@cacheman2k
@cacheman2k 3 жыл бұрын
Thanks!
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Welcome!
@chetankashetty6409
@chetankashetty6409 3 жыл бұрын
How do we disable a Jenkins job (Disable Project option) with Jenkinsfile? Thanks
@GaadhiMahendra
@GaadhiMahendra 3 жыл бұрын
Thank you
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Thanks! :)
@lemonUA
@lemonUA 4 жыл бұрын
Is there a way to trigger Jenkins Job from Slack with different parameters and variables? Can't understand how to use /build parameter1 command in Slack :'(
@szymonstepniak
@szymonstepniak 4 жыл бұрын
I have no answer to your question. I never tried configuring Slack to trigger Jenkins jobs. Try looking at the Slack plugin documentation and check if there is any form of contact with its maintainers. I wish I could help you more. Good luck and have a good day! 👍
@beniamintucu2588
@beniamintucu2588 2 жыл бұрын
How can I activate a virtual environment variable that I have on my system, and use it to call a python scrypt in a stage ?
@narendrabhupathiraju8986
@narendrabhupathiraju8986 3 жыл бұрын
hi, how can i auto approval notification to mail in jenkins pipeline?
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Hi Narendra, I have no answer to your question. Good luck, and have a good day!
@ramaraokondeti7157
@ramaraokondeti7157 4 жыл бұрын
Nice explanation Szymon Great work :) In declarative pipeline when one stage fails it will fail all next stages in the entire pipeline right, Can you tell me how to go next stage(or Continue the pipeline) if any previous stage fails ? and my second question is may I know how to move one stage to another stage? for example stage("Clean") { steps { sh """ rm -rf /home/.cache """ post { always{ node ( 'linux' ) { sendnotifications currentBuild.result, 'mm.com' } } from the above example I want to move clean stage into post may I know how to do ? Thanks in advance
@szymonstepniak
@szymonstepniak 4 жыл бұрын
Hey, Ramarao! Thanks for one more comment ☺️ I really appreciate that. Regarding your question - check this post from my blog e.printstacktrace.blog/how-to-time-out-jenkins-pipeline-stage-and-keep-the-pipeline-running/ It explains how to handle the timeout and keep running the pipeline. It's not exactly the same thing you are asking, but the timeout is just an error in terms of the pipeline, so it should give you a good understanding of how to use the same mechanism to implement your expected solution. Let me know if it worked for you. Thanks in advance!
@ramaraokondeti7157
@ramaraokondeti7157 4 жыл бұрын
@@szymonstepniak Thanks for sharing the link looks like in my scenario try catch will work, So that if stage fails it will print the message from the catch method and will go to next stage. Correct me if I am wrong.
@szymonstepniak
@szymonstepniak 4 жыл бұрын
@@ramaraokondeti7157 That is correct. In addition to that, you can use "catchError" step to mark the failing stage as "UNSTABLE". But it depends on your use case. If printing a message to console and keeping the stage "STABLE" is OK, then using try-catch is all you need. Good luck!
@mahendrapalla1373
@mahendrapalla1373 3 жыл бұрын
Hi Brother, Thanks for the video. I was having 2 scenarios. 1) how to run few stages in one Node and another few Stages in another NODE 2) how to deploy in 100 servers Parallelly with Jenkins. It would be greatful if u Post a video or explan me. Thanks , b Safe
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Hi Mahendra! I don't have any out-of-the box answer to you. I may, however, post a video in the future that covers some similar aspects, but I can't promise anything. Take care, and have a good day!
@R-Kannada-DevOps
@R-Kannada-DevOps 3 жыл бұрын
I need to pass my build number to my Kubernetes manifest file dynamically , in jenkins pipeline, how can do this step, i need to use this step for building dockoer image with buildnumber and pass the same build number to my manifest which is my scm
@R-Kannada-DevOps
@R-Kannada-DevOps 2 жыл бұрын
I found a solution for this and Thank for your effort
@cptechno
@cptechno 3 жыл бұрын
You forget to explore two questions: 1) Are environment variables passed to execution agents? Demo? 2) Are environment variables passed when calling another pipeline job.
@szymonstepniak
@szymonstepniak 3 жыл бұрын
I must admit I haven't think about those two cases when I was preparing the material for that video. The answers are: 1) Yes, they are passed. I guess the only exception is the env var defined in the script block. 2) Nope. When you trigger another job from the pipeline, it has no access to the env variables you set in the pipeline. Thanks for the comment, have a good day!
@satdanie7349
@satdanie7349 3 жыл бұрын
How to learn Jenkinsfile from scratch?
@szymonstepniak
@szymonstepniak 3 жыл бұрын
I can recommend checking @TechWorldwithNana KZbin channel and her Complete Jenkins Pipeline Tutorial video - kzbin.info/www/bejne/bXymhGpmqKiigZI Good luck and have fun!
@daherdaher2560
@daherdaher2560 2 жыл бұрын
Genius
@evhen_
@evhen_ Жыл бұрын
OMG!!!
@szymonstepniak
@szymonstepniak 11 ай бұрын
Thanks for the comment! Take care, and have a good day!
@vkhemant
@vkhemant 3 жыл бұрын
Thank You
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Thanks!
@banalanidish5343
@banalanidish5343 3 жыл бұрын
thank you
@szymonstepniak
@szymonstepniak 3 жыл бұрын
Welcome!
Jenkins Pipeline Tutorial: Declarative Pipeline vs Scripted Pipeline
12:41
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 35 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 15 МЛН
Groovy DSL scripts in 20 minutes for beginners
23:02
Szymon Stepniak
Рет қаралды 37 М.
Complete Jenkins Pipeline Tutorial | Jenkinsfile explained
35:06
TechWorld with Nana
Рет қаралды 801 М.
Jenkins Pipeline for Java using Maven and Docker
17:43
Szymon Stepniak
Рет қаралды 30 М.
Jenkins Multibranch Pipeline With Git Tutorial
29:54
CloudBeesTV
Рет қаралды 85 М.
Declarative Jenkins Pipeline OR Scripted Jenkins Pipeline?
10:27
Szymon Stepniak
Рет қаралды 7 М.
Jenkins Pipeline For Multiple Java Versions Using Matrix Feature
19:43
Vim Tips I Wish I Knew Earlier
23:00
Sebastian Daschner
Рет қаралды 82 М.
How to Use Environment Variables in Jenkins
8:33
CloudBeesTV
Рет қаралды 19 М.
How to organize your notes in Obsidian // The LATCH method
15:55
Nicole van der Hoeven
Рет қаралды 211 М.
How Do Linux Kernel Drivers Work? - Learning Resource
17:02
LiveOverflow
Рет қаралды 562 М.
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 35 МЛН