Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use git repo name as ENV variable in jenkins job

Tags:

git

jenkins

I am trying to figure out a way to get the remote git project name in a jenkins job as a env variable. I would like to use the project name as the "local subdirectory name" when jenkins clones the repo. is there a way to do this?

maybe something like ${GIT_REPO_NAME}

I know that there is ${GIT_REPO_URL} and it includes the project name, but I need to be able to use this in the local sub directory field.

if someone has a better solution for this I am all ears.

thank you!

like image 834
arrowill12 Avatar asked Aug 01 '14 20:08

arrowill12


People also ask

How do I add a Git repository to Jenkins?

Step 1: go to your GitHub repository and click on 'Settings'. Step 2: Click on Webhooks and then click on 'Add webhook'. Step 3: In the 'Payload URL' field, paste your Jenkins environment URL. At the end of this URL add /github-webhook/.

How do I use local Git repository in Jenkins?

Access local git repository in Jenkins docker container To mount your local git repository folder to a Jenkins container run the container with an additional -v or --volume flag. The basic docker run statement from the official docker image documentation would then look like. The :ro option is optional.

How do I pass a Git branch as parameter in Jenkins?

If you want to be able to dynamically give a Git branch to use in a Jenkins build then you'll need to do a couple of things. Then, in your Pipeline configuration, under Branches to build, add your parameter name inside the Branch Specifier box, surrounded by ${} . Jenkins will expand your variable when the job runs.


2 Answers

Here's a simple one-liner:

env.GIT_REPO_NAME = env.GIT_URL.replaceFirst(/^.*\/([^\/]+?).git$/, '$1')

/^.*\/([^\/]+?).git$/ is a regex that creates a back-reference to just the repo name.

like image 89
ScrappyDev Avatar answered Nov 14 '22 22:11

ScrappyDev


^.*?(?::\/\/.*?\/|:)(.*).git$ use this pattern for both http and ssh if you want to get full repo path {owner}/{repo_name}

like image 34
Oleg Plakida Avatar answered Nov 14 '22 22:11

Oleg Plakida