How to use Azure variable $(build.buildnumber)-$(build.sourcebranchname) in Dockerfile as build artifact/jar is getting generated with buildNumber?

Dev Gosain

I am writing a azure pipeline that will build a jar and a docker image. Most of part is working but I am not able to substitute $version variable in Dockerfile. And I am declaring artifact version as $(build.buildnumber)-$(build.sourcebranchname) as given below. I can not hard code it as with every run of build, builnumber will change. How can I use either $version or $(build.buildnumber)-$(build.sourcebranchname) in Dcokerfile where I am copying jar to container folder.

My generated artifact name is like this someservice-api-$(build.buildnumber)-$(build.sourcebranchname).jar

Azure Pipeline:

pool:
  vmImage: 'windows-latest'

steps:

- task: DownloadSecureFile@1
  displayName: 'Download gradle.properties secure file'
  inputs:
    secureFile: gradle.properties

- powershell: |
    New-Item -Type Directory -Force "${HOME}/.gradle"
    $version = "$(build.buildnumber)-$(build.sourcebranchname)"
    Add-Content "$(Agent.WorkFolder)/_temp/gradle.properties" "`nartifactVersion=$version"
    Add-Content "$(Agent.WorkFolder)/_temp/gradle.properties" "`nartifactDir=$path"
    Copy-Item -Force "$(Agent.WorkFolder)/_temp/gradle.properties" "${HOME}/.gradle/gradle.properties"

other task like gradlew build, copy, publish to staging directory

Dockerfile:

FROM openjdk:8
WORKDIR /usr/src/webapp/clientscore
COPY ./build/libs/someservice-api-$version.jar api.jar
CMD ["java", "-jar", "api.jar"]

I have tried with following already

COPY ./build/libs/someservice-api-$version.jar api.jar
COPY ./build/libs/someservice-api-${version}.jar api.jar
COPY ./build/libs/someservice-api-$(build.buildnumber)-$(build.sourcebranchname).jar api.jar
COPY ./build/libs/someservice-api-${build.buildnumber}-${build.sourcebranchname}.jar api.jar

Azure Docker task:

- task: Docker@2
  inputs:
    command: 'buildAndPush'
    Dockerfile: '**/Dockerfile'
Sweetie Panav

The Azure task, Docker "BuildAndPush", that you are using does not accept build args. You need to define Docker "build" task then you can pass build args and use it accordingly in Dockerfile.

--build-arg varname="$(dynamicversion)"

Have a lookt at this

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Azure DevOps Build Pipeline - Unable to access $(Build.BuildNumber) in Script

azure devops: copy and publish build artifacts - rename artifact by buildnumber

##[error]Could not find version number data in the following environment variable: BUILD_BUILDNUMBER

how to access $(Build.BuildNumber) from different project in tfs2015

Azure DevOps Use Build.SourceBranchName As NuGet patch version for pre-release testing

Docker how to use env_file variable in Dockerfile during build

Pipeline Yaml: BuildNumber is updated but the build description doesn't seem to interpret the variables

How to yarn build on Dockerfile

How to use build in "period" variable?

Pass environment variable to dockerfile build

Azure DevOps Pipeline - Updating BuildNumber

How to build an image with Dockerfile in Kitematic?

How to build Dockerfile with R & Java

How to build Dockerfile with custom names

Dockerfile: How to replace a placeholder in environment variable with build-arg's?

How to use awk build in variable in shell script

how to use build.binariesdirectory variable

how to use environnement variable in cli build

How to use Dockerfile with different name in OCP Git source build

Generated test sources not getting invoked as part of the build

Google Cloud Build - Pass environment variable for Dockerfile

How to build Variable Array

How to use templates to build compile-time generated metadata

how to use static build of cpprest-sdk generated by vcpkg?

How to use japicmp to compare between artifacts generated in current and previous build

How to use post build action in a dynamically generated jenkins stage

Azure Pipeline: how to delete a variable inside Azure build pipeline

How to use the build cause in build name?

"npm run build" in Dockerfile: dist folder is generated but disappears