Search code examples
gitjenkins-pipeline

How to specify git smudge filtering for Jenkins Pipeline?


In my git repository I use files with $Id$ content that shall get smudged/cleaned automatically. For this I configured .gitattributes to activate the ident filter as documented in the Keyword Expansion chapter of https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes. So far I shall be happy with the commit numbers.

The documented behaviour works on my Windows machine both natively and in cygwin, and on a Ubuntu 20 box when I checkout/commit manually. I also have a Jenkins pipeline that would process the files - but it seems Jenkins only sees the 'unsmudged' files - all files contain $Id$.

How can I make the Jenkins pipeline run git such that smudge filters are applied?


Solution

  • So to solve the puzzle above:

    • First I checked the scripting languages offered by the docker container that would run my Jenkins pipeline/maven build, and decided to go with Python 2.7.
    • Using that language I wrote a smudge/clean filter which essentially extends and reduces the $Id$ tag similar the way SVN used to do it.
    • I declared git to use 'some' filtering by an entry in the .gitattributes file
    • I configured git so that 'some' filtering would actually call my script in smudge or in clean mode respectively. This configuration needs to go either into .git/config or raised via git commands and is performed within the Jenkinsfile, just before checking out the files again.

    This way git checks out the files and runs the smudge filter where necessary. Another advantage is that this way the configuration of the smudge filter only occurs inside the docker container/linux environment. Which means me and my colleagues who are working in Windows environment and usually without a python installation do not have to care at all about the filtering - it will neither be configured nor invoked there.

    Credit goes to @phd. Without his hints I would not have been able to crack this nut.