Get the latest git tag in a job

Hello,

I just started playing around with JetBrains Space and especially the Automation feature and could need some help here.

I'd like to run a script that starts whenever someone creates a tag in the master branch (e.g release/1.0):

  • start a container
  • run some tests
  • get the latest tag from git
  • publish a package to the Space Package Manager (and take the version number from the tag)

My script currently looks like this:

job("publish new package version") {
    startOn {
        gitPush {
            tagFilter {
                +"release/*"
            }
        }
    }
    container(displayName="PythonContainer", image = "python") {
        shellScript {
            content = """
                python3 -m unittest
              VERSION=${'$'}(git tag --list | sort -V | tail -n1) //save the latest tag to a variable
                echo ${'$'}VERSION
echo "Finished..."
//Do the rest...
            """
        }
    }
}

The log looks like that:

[Bootstrap]      * branch            2ca4ea4b62c54f6b828a033884e9089bd2265b02 -> FETCH_HEAD
[Bootstrap] Run git command `-c advice.detachedHead=false checkout FETCH_HEAD` in `work/pythonProject` directory
[Bootstrap]     HEAD is now at 2ca4ea4 test
[Bootstrap] Set permissions [OR, OtR, GR, GW, OE, OtW, OW, OtE, GE] for `work/pythonProject/gradlew`
[Bootstrap] Download from Space endpoint pipelines/system/gradle/init-script into `system/gradle/init.gradle` 
[Bootstrap] Download from Space endpoint pipelines/system/gradle/listener into `system/gradle/circlet-gradle-listener.jar` 
[Bootstrap] Download from Space endpoint pipelines/system/docker/config into `system/docker/config.json` 
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
Finished...
OK

 

My questions:

  • Why is there no output for the variable in the log?
  • Is there a better way to fetch tags?
  • What's the best approach to work and test automation scripts without messing up the git history? Just working in a branch and squashing together all the commits for changes made to the script?

 

Thank you in advance! :)

0
2 comments

Hello,

Thank you for your question!

This issue seems to be related to the way we clone the repository. We don't clone the whole history by default, so it's worth customizing the clone depth if you'd like to see all tags.
As another option, getting the tag name from the `JB_SPACE_GIT_BRANCH` env variable can make sense.

As a side note, getting the latest tag is theoretically wrong if your goal is to get the tag that triggered the job: if two different tags are pushed quickly enough, the job for the first tag could clone the repo after the 2nd push, and thus get the 2nd tag as "latest tag" instead of the tag that triggered the job.

Please let us know if further assistance is needed.

0

For people who stumbled upon this problem too, working piece of code:

JOB - will be triggered on tag push (to see the job in Space, you have to go to project's jobs and use the branch dropdown to select the tag)

job("Deploy") {
startOn {
gitPush {
branchFilter {
+"refs/heads/master"
}
tagFilter {
+"release/*"
}
}
}
...

Kotlin

val version = api.gitBranch().split("/")[3] // for input e.g. refs/tags/release/1.1.10 output: 1.1.10

Shell

VERSION=${'$'}(echo ${'$'}JB_SPACE_GIT_BRANCH | cut -d / -f 4) # // for input e.g. refs/tags/release/1.1.10 output: 1.1.10
1

Please sign in to leave a comment.