1

Currently when I change the version number in my project I have to define it in my build.gradle file and several places in my bitbucket-pipelines.yml. This is because I have scripts to upload files to my project's Downloads folder. Is there a way to get the version from the build.gradle file? I would like to just update one file when I increment the version.

build.gradle

plugins {
    id 'java'
}

def buildNumber = project.properties['buildNumber'] ?:'0'
group 'edu.csusb.iset'
version "2020.1-BETA"

jar {
    manifest {
        attributes('Main-Class': 'edu.csusb.iset.projectname.Main',
            'Implementation-Title': 'ProjectName',
            'Implementation-Version': "$version, build $buildNumber")
    }

    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it)         }
    }
}

task packSrc(type: Tar) {
    archiveAppendix.set("src")
    archiveVersion.set("$project.version")
    archiveExtension.set("tar.gz")
    destinationDirectory = file("$buildDir/dist")
    compression = Compression.GZIP

    from 'build.gradle'
    from 'settings.gradle'
    from('src') {
        include '**/*'
        into "src"
    }
}

repositories {
    mavenCentral()
}

dependencies {
    ...
}

bitbucket-pipelines.yml

image: openjdk:11
pipelines:
  default:
    - step:
        name: Gradle build
        caches:
          - gradle
        script:
          - bash ./gradlew build
    - step:
        name: Deploy downloads
        trigger: manual
        caches:
          - gradle
        script:
          - bash ./gradlew build
          - pipe: atlassian/bitbucket-upload-file:0.1.6
            variables:
              BITBUCKET_USERNAME: $BITBUCKET_USERNAME
              BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
              FILENAME: build/libs/ProjectName-2020.1-BETA.jar
          - bash ./gradlew packSrc
          - pipe: atlassian/bitbucket-upload-file:0.1.6
            variables:
              BITBUCKET_USERNAME: $BITBUCKET_USERNAME
              BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
              FILENAME: build/dist/ProjectName-src-2020.1-BETA.tar.gz

1 Answers1

1

Bitbucket Pipelines is just running bash/shell commands on a Unix docker machine. So you can use the normal unix shell commands, like or , to extract text from a text file:

Use awk

build.gradle contains a line:

version "2020.1-BETA"
awk '/^version/{gsub(/\"/,"",$2);print $2}' build.gradle
Output: 2020.1-BETA

Explanation:

Another way with perl

build.gradle contains a line:

version "2020.1-BETA"
perl -nle 'print $& while m{(?<=version ").*?(?=")}g' build.gradle
Output: 2020.1-BETA

Explanation:

  • Extract the text between two words of: version " and ", using a regular expression
  • perl -nle = extract text from a line in a file, using regular expressions
  • (?<=version ") = Look for the start tag of version ", using negative lookahead ?<= to make the capture start at the end of the tag
  • .*? = Capture any string, after the first tag. Use ? to make the match 'non-greedy', which means it will stop at the first match, to match as few characters as possible. See: Regular expression to stop at first match
  • (?=") = Look for the end tag of a speech mark ", using positive lookahead ?= to make the capture stop before the tag
  • g = global search on all lines of a multi-line file
  • More info and sources: grep -P no longer works. How can I rewrite my searches? and How to use sed/grep to extract text between two words?

Use the shell commands in the yml script

You can therefore use either awk or perl in your yml script. Capture the output as an environment variable called $APP_VERSION, and reuse this environment variable:

script:
  - bash ./gradlew build
  # Extract the version string from build.gradle into an environment variable
  - export APP_VERSION=$(awk '/^version/{gsub(/\"/,"",$2);print $2}' build.gradle)
  - echo $APP_VERSION # debug to print the extracted text of "2020.1-BETA"
  - pipe: atlassian/bitbucket-upload-file:0.1.6
    variables:
      BITBUCKET_USERNAME: $BITBUCKET_USERNAME
      BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
      FILENAME: "build/libs/ProjectName-$APP_VERSION.jar" # Use the extracted text here

Alternative way:

script:
  - bash ./gradlew build
  # Extract the version string from build.gradle into an environment variable
  - export APP_VERSION=$( perl -nle 'print $& while m{(?<=version ").*?(?=")}g' build.gradle )
  - echo $APP_VERSION # debug to print the extracted text of "2020.1-BETA"
  - pipe: atlassian/bitbucket-upload-file:0.1.6
    variables:
      BITBUCKET_USERNAME: $BITBUCKET_USERNAME
      BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
      FILENAME: "build/libs/ProjectName-${APP_VERSION}.jar" # Use the extracted text here
Mr-IDE
  • 7,051
  • 1
  • 53
  • 59