Is it possible for Jenkins to automatically detect and build newly created tags in a git repo?

前端 未结 5 1251
情深已故
情深已故 2020-12-07 12:59

It would be nice for our Jenkins CI server to automatically detect, deploy and build tags as they are created in our Github repository.

Is this possible?

5条回答
  •  春和景丽
    2020-12-07 13:10

    To overcome the drawback of @oberlies' answer that all tags will be built I'm using a special trigger build instead. The trigger build uses the same git repository and branch as the main build and the following (post) build steps.

    Build -> Execute shell:

    # Get the most recent release tag.
    PATTERN="release-tag-[0-9][0-9]-[0-9][0-9][0-9][0-9]"
    TAG=$(git log --tags=$PATTERN --no-walk --pretty="format:%d" | grep -m 1 -o $PATTERN)
    
    # Due to a Jenkins limitation (https://issues.jenkins-ci.org/browse/JENKINS-8952)
    # when passing environment variables we have to write the tag to a file and
    # inject it later again.
    mv release.properties release-old.properties || true
    echo "TAG = $TAG" > release.properties
    
    # Fail the build if the most recent release tag did not change.
    ! diff release.properties release-old.properties
    

    Build -> Inject environment variables:

    Properties File Path: release.properties
    

    Post-build Actions -> : Trigger parameterized build on other projects

    Projects to build: 
    Trigger when build is: Stable
    Parameters: TAG=$TAG
    

    Finally, in your main build, tick "This build is parameterized" with the following string parameter

    Name: TAG
    Default Value: 
    

    And in the "Source Code management" section use "$TAG" in the "Branches to build" field.

提交回复
热议问题