Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

maven POM file project.version as a pipeline variable

Pascal De Cort July 2, 2024

Was looking for a solution to use the project.version from a pom.xml file in a BitBucket pipeline. It's not possible to share variables between one step: and another.

<version>1.0.4</version>

So I worked around by defining the <fileName> in pom to be a fixed filename so I can use it in a shell script in the pipeline. In the example shell script below (save-artifact-to-s3.sh) it's called my-project as example.

<build>

<finalName>${project.artifactId}</finalName>

<plugins>...

This way the same jar file would be generated


Next step is to add a tag to your commit starting with *v* - ex v1.0.4

in the pipeline the trigger would be _tags:_ 

The PROJECT_VERSION=$(BITBUCKET_TAG:1} -> :1 will assign BITBUCKET_TAG starting from position 1 to the variable.

Check that it's correct:

tags:

v*:

- step:

name: echo TAG

image: atlassian/default-image:2

script:

- PROJECT_VERSION=${BITBUCKET_TAG:1}

- echo $PROJECT_VERSION

Screenshot 2024-07-02 at 20.55.05.png

 

Make sure you're using the my-project as target artefact:

 - step:

name: build

image: maven:3.6.1

caches:

- maven

script:

- mvn install --file pom.xml

artifacts:

- "target/my-project.jar"

Now you can use this tag in a shell script:

 - step:

name: upload artifact to s3

image: mesosphere/aws-cli

script:

- echo ${BITBUCKET_TAG:1}

- rm -rf /opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes

- sh deployment/save-artifact-to-s3.sh ${BITBUCKET_TAG:1}

 

save-artifact-to-s3.sh :

 

#!/bin/sh

PROJECT_VERSION=$1

LOCAL_BUILD_ARTIFACT=target/alberts-machineconfiguration-lambda.jar

S3_ARTIFACT=my-project-$PROJECT_VERSION.jar

S3_BUCKET=artifacts.company.be/saved-configurations

S3_ARTIFACT_URL=s3://${S3_BUCKET}/${S3_ARTIFACT}

echo "Copying artifact '$LOCAL_BUILD_ARTIFACT' to S3: $S3_ARTIFACT_URL"

aws s3 cp ${LOCAL_BUILD_ARTIFACT} ${S3_ARTIFACT_URL}

echo "Done - artifact '$S3_ARTIFACT' has been copied to s3 to '$S3_BUCKET'"

 

Hope this will help someone.

What would be nice is to auto tag a commit when it's in master... hmmm

Tried also with the image provided by AWS-CLI but that did not seem to accept variables.

 

0 comments

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events