Exercise 2 - Task 2 - Release Automation with Gradle

Now let's automate the just manually performed process with the help of gradle.

The goals of this task are the following:

  • we can simply trigger through gradle the build of the docker image
  • we can simply trigger through gradle the publishing of the built image to docker hub
  • the version tag of our image is automatically computed from git versioning information as follows:
    • the branch name as a prefix (replace special characters by -)
    • followed by the git describe version which is the latest tag followed by number of commits ahead since that tag and the commit hash
    • some version examples: master-1.0.0, develop-1.0.0-10-g445c7f83, feature-your-branch-name-1.0.0-10-g445c7f83

2.2.1 Automate the Docker Build

Pick one of the following options, depending what you like to learn more:

  • Option A: Write your own gradle task to trigger the docker build shell command
  • Option B: Use the gradle plugin for docker

Both are more or less easy. It depends what you like to learn more. Just follow the implementation hints for your choosen option in the next two sections.

Option A - Gradle Automation with Shell Commands

  1. Define your own task docker in the gradle build file (see course slides)
  2. Make the groovy task execute the usual shell command for building
    • command to execute: docker build -t exercise4automation/project-automation:<put-your-version-here>
    • See course slides for how to trigger a shell command from docker
  3. Instead of using your hardcoded tag let the task use the property version for the tag
  4. You can trigger and test the docker build by using the new gradle task docker

Option B - Use Gradle Plugin for Docker

  1. Integrate the plugin in your gradle build
    • Name of the plugin: com.palantir.docker
    • Latest Version: 0.22.1
  2. Add most simple configuration in gradle build file to work with what you started so far:
        docker {
            name "exercise4automation/project-automation:<put-your-personal-tag-here>"
            copySpec.from("build/libs").into("build/libs")                     
        }
    
    • Per default the plugin takes Dockerfile from root of your project, but you could set a different path to the Dockerfile as a file through property dockerfile if you want
    • The copySpec config is needed to copy the needed jar files from the build directory into the build context directory from where the plugin starts the build and to have the jar into same relative directory there, as expected.
  3. Instead of using your hardcoded tag let the task use the property version for the tag
  4. You can trigger and test the docker build by using the new gradle task docker

More documentation and options for the plugin see https://github.com/palantir/gradle-docker

2.2.2 Automate the Docker Hub Publishing

This one should be easy - depending on what option you went for:

Option A - Gradle Automation with Shell Commands

  1. Define another task dockerPush to trigger the following shell command:
       docker push exercise4automation/project-automation:<put-your-version-here>
    
  2. Use the before defined variable for the tag in the task
  3. Simply run the gradle task to test it
  4. Caution: you will still need to login with docker login manually before! this part is not part of the gradle automation because it might not be a good idea to commit your password for dockerhub to a repo (especially if your repo is open source!)

Option B - Use Gradle Plugin for Docker

  1. Nothing special to do
  2. Simply run the gradle task dockerPush
  3. Caution: you will still need to login with docker login manually before! This part is not part of the gradle automation because it might not be a good idea to commit your password for dockerhub to a repo (especially if your repo is open source!)

2.2.3 Automate Version Tag Calculation

When you use git tags in your git repository for your releases then you can easily use git command line tooling to automatically calculate the current version of your software that you checked out, as follows:

  1. Put the script for doing the calculation into a separate file gradle/version.gradle
  2. Write a function gitDescribe to call the shell command git describe --allways to get the current version you checked out
  3. Write a function gitBranch to call the shell command git symbolic-ref --short HEAD to get the name of your current feature branch
  4. Write a function 'getVersionFromGit()' to do the following:
    • call the two above listed shell commands and get their results (see some gradle hints below)
    • take the feature branch name and replace all special characters like / with - by simply using .replaceAll("\\/", "-")); method on the result (the escaped backslash is needed, because slash needs to be escaped in the passed regex in first argument)
    • append the git describe result to this sanitized feature branch name
    • return this calculated version tag
  5. Export a global variable from this script that will hold the calculation result as follows in the end of the version.gradle file:
    ext.versionFromGit = getVersionFromGit()
    
  6. Import the just written version script with an apply from: in your build.gradle
  7. Now use the just defined global property in the build.gradle file as value for the verison property that you already use for the tag name of your built docker image
  8. Also let your build script output the calculated variable value with a simple println command for debugging
  9. Test it by running the docker build task again

When you're done you have successfully automated most of the relase process, the rest of it will be done in the jenkins pipeline directly in your next exercise task.

results matching ""

    No results matching ""