1

We are working on multiple feature, one develop, release and master GIT branches. We create separate feature branches for all JIRA development task. I need to have Jenkins build job to poll all feature branch, and build those specific ones which have recent PUSH events. After the build job is initiated, I want that specific GIT feature branch name (i.e. JIRA-1234, not origin/feature/JIRA-1234) to be picked up, included this in artifact file name as APP-0.0.1-JIRA-1234-SNAPSHOT, and save this to Nexus.

NB: POM.XML file is configured with APP-0.0.1-SNAPSHOT.

For each feature branch build, we want separate jar files saved to Nexus. For ex: Development task under JIRA-0101 would build and save jar in nexus as APP-0.0.1-JIRA-0101-SNAPSHOT

I have included ${branch} parameter in POM.xml as shown below: APP-0.0.1${branch}-SNAPSHOT

and pass this value from Jenkins build command as - mvn clean install -Dbranch=-JIRA-0101

I then parametrised the jenkins build task, after which I can get feature branch name as "origin/feature/JIRA-0101", but I cannot parse this whole string in Jenkins to pick only JIRA task.

Secondly, mvn jgitflow:release-start and mvn jgitflow:release-finish commands would create complications having "APP-0.0.1${branch}-SNAPSHOT" in POM.xml. So, using ${branch} in POM is not effective.

5
  • Hi! I'm having a little trouble understanding exactly what you are asking.. Your Jenkins process is triggering when there is a change to a specific branch, so you already have access to the local git branch, but you are just trying to figure out how to properly add it to the 'version' being used for publishing, and you are tied to using the jgitflow maven plugin? Also, are you using the Jenkinsfile Scripted/Declarative pipeline, or a freestyle job? Commented May 30, 2019 at 12:53
  • After making the build job parametrised, I am able to list all feature branches in a drop down list -- then I select the feature branch I need to build which is picked up as "origin/feature/JIRA-0101". I assign this branch (i.e. "origin/feature/JIRA-0101") to a BRANCH_NAME variable in Jenkins. But, while executing maven build command (mvn clean install -Dbranch=$BRANCH_NAME), I am unable to parse BRANCH_NAME to only branch name i.e. JIRA-0101. I am using freestyle job in Jenkins. Commented May 30, 2019 at 13:02
  • If your only problem is needing to parse out a pattern from the branch name, we can do that as a part of a simple shell step. Are you envoking maven in a shell step, or with the maven plugin? Commented May 30, 2019 at 13:11
  • I am invoking maven from jenkins using plugin. Can you please help with shell steps to only pick the branch name Commented May 30, 2019 at 13:23
  • Updated answer to reflect using a shell step to extract the ticket ID, as well as info about the envinject plugin for passing information between stages Commented May 30, 2019 at 13:33

2 Answers 2

0

If I get it right you want your Jenkins to automatically build on push events, and on success to publish the target folder to your nexus. If that is the case I would suggest looking into something a bit more advanced called JenkinsFile (https://jenkins.io/doc/book/pipeline/jenkinsfile/) with a couple of jenkins plugins as Multibranch pipeline https://wiki.jenkins.io/display/JENKINS/Pipeline+Multibranch+Plugin which can automatically detect all the branches in your repository and setup a job for each one. Then you would need webhooks (both bitbucket and github have them) and those hooks would inform your jenkins about the push events ( or any other event you want. like commit / comment etc. ) which will trigger the build job. Some plugins ( like Bitbucket branch source plugin https://wiki.jenkins.io/display/JENKINS/Bitbucket+Branch+Source+Plugin ) offer a service that handle those webhooks so you wont have to do it manually ( manual : you have to type the job url and the execute command like ci:port/yourJob/?build [can't recall the whole url been ages since I used it ], automatic POST ci:port/bitbucket-scmsource-hook/notify/ which is universal and the plugin will redirect the hook to the appropriate job. ). As a last step of your jenkinsfile pipeline you can add the push command to your nexus which will add the file / files you want in your nexus. !

I hope my answer helped :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Michael, I would give this a try and get back if some confusions arise, but this seems to take a while analyzing
0

I'm not familiar with the jgitflow process, and I'll try to read up on that and tailor my answer, but for now I wanted to point you to this really great article on doing maven deployments - Maven Release Plugin: Dead and Buried. The article talks about using an externally set version in their pom file - they embed a 'sane default' property into their codebase.

    <properties>
        <!-- Sane default when no revision property is passed in from the commandline -->
        <revision>0-SNAPSHOT</revision>
    </properties>

They then reference that property in their <build> description

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-scm-plugin</artifactId>
                <version>1.9.4</version>
                <configuration>
                    <tag>${project.artifactId}-${project.version}</tag>
                </configuration>
            </plugin>
        </plugins>
    </build>

When invoking maven, they pass a custom parameter to it from their CI server - mvn deploy scm:tag -Drevision=$BUILD_NUMBER

I'm sure that with the jgitflow plugin, you may be able to have an internal property similar to the above configuration, but use it as a part of your jgitflow version with the branch information passed into your build.

--- Response to feedback ---

If you are just trying to extract the last part of the branch identifier, there are several ways you can do it. If you just want everything after the last /, you could use the basename utility.

$ BRANCH="origin/feature/JIRA-0101"
$ basename $BRANCH
JIRA-0101

You could be more specific with a regular expression that expects the ASDF-1234 style pattern

$ echo $BRANCH | sed -e 's|.*/\([A-Z]*-[0-9]*\)$|\1|g'
JIRA-0101

And if you want to capture the above into a new variable, you can use a subshell, either directly in your call to maven

mvn clean install -Dbranch=$(echo $BRANCH | sed -e 's|.*/\([A-Z]*-[0-9]*\)$|\1|g')

Or storing it in an intermediate variable that you could test to verify is nonzero length

JIRA_ID=$(echo $BRANCH | sed -e 's|.*/\([A-Z]*-[0-9]*\)$|\1|g')
[[ -z "${JIRA_ID}" ]] && echo "Unable to determine JIRA_ID" && exit 1
mvn clean install -Dbranch=${JIRA_ID}

If you need to pass the variable to a different step (if directly using the maven plugin), you will need a plugin similar to the envinject environment injection plugin, described in this answer

1 Comment

Thanks a lot :) this fixed the problem and I am now able to get the JIRA task number passed on to maven build. But, I then ran into another complication which I am investigating now - the new artifact version would only be in target directory, and it would be moved to Nexus using the OLD file name: [INFO] Installing /app/jenkins/jobs/build-feature-eis-services/workspace/eis-services/target/4.38.0-SNAPSHOT-JIRA-0101.jar to /app/jenkins/.m2/repository/com/project/integration/eis-services/4.38.0-SNAPSHOT/eis-services-4.38.0-SNAPSHOT.jar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.