Concourse: Read git resource meta-data

1.7k Views Asked by At

I have a concourse pipeline:

groups: []
resources:
- name: source-code
  type: git
resource_types: []
jobs:
- name: build
  public: true
  plan:
  - get: source-code
  - task: build
    privileged: true
    config:
      platform: linux
      image_resource:
        type: docker-image
        source:
          repository: java
          tag: openjdk-8-alpine
      run:
        path: sh
        args:
        - -exc
        - |
          set -e -u -x

          MVN_VERSION=$(cat pom.xml | grep "^    <version>.*</version>$" | awk -F'[><]' '{print $3}')

          **CUSTOM_VERSION = [POM_FILE_VERSION]-build.[NO_OF_COMMITS_IN_BRANCH]** 
      inputs:
      - name: source-code

Using Unix commands, I want to create the custom version:

CUSTOM_VERSION = [POM_FILE_VERSION]-build.[NO_OF_COMMITS_IN_BRANCH]

For [POM_FILE_VERSION], I have found the code:

MVN_VERSION=$(cat pom.xml | grep "^    <version>.*</version>$" | awk -F'[><]' '{print $3}')

But for [NO_OF_COMMITS_IN_BRANCH], i don't know how get that data from git resource. Can anyone help please ?

1

There are 1 best solutions below

1
On

The Concourse git resource doesn't provide the number of commits in the branch.

I suggest to use something more meaningful like the commit hash, which the Concourse git resource makes available as the .git/short_ref file.

If you really need to use the number of commits (but why?), then you need to run the git command directly in the Concourse task, for example git rev-list --count HEAD.

In the pipeline above, the task is using the openjdk-8-alpine Docker image from the java repository. If that image contains git, then you are all set. If not, I suggest first to see if the java repository offers a variation of openjdk-8-alpine with git installed. If not, you have to create your own Docker image, based off openjdk-8-alpine.

Security note: you have privileged: true specified. Probably you don't need it.