簡體   English   中英

Jenkins是否可以在git倉庫中自動檢測和構建新創建的標簽?

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

我們的Jenkins CI服務器在我們的Github存儲庫中創建標簽時會自動檢測,部署和構建標簽。

這可能嗎?

通過以下配置,您可以創建作業構建所有標記:

  1. 使作業獲取標簽就像它們是分支一樣:單擊存儲庫URL下方的“高級”按鈕,然后輸入Refspec +refs/tags/*:refs/remotes/origin/tags/*
  2. 使用Branch Specifier */tags/*構建所有標記“branches”
  3. 啟用S​​CM輪詢,以便作業檢測到新標記。

這種方法有一個缺點:作業將構建所有標簽而不僅僅是新添加的標簽。 因此,在創建作業后,將為每個現有標記觸發一次。 因此,您可能希望一開始就不執行任務,然后等待所有現有標記都已處理完畢,然后再配置要為每個新標記完成的構建步驟。

由於標簽在git中沒有變化,因此每個新標簽只會觸發一次作業。

為了克服@oberlies回答所有標簽將被構建的缺點,我正在使用特殊的觸發器構建。 觸發器構建使用與主構建和以下(post)構建步驟相同的git存儲庫和分支。

構建 - >執行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

構建 - > 注入環境變量

Properties File Path: release.properties

構建后操作 - >: 在其他項目上觸發參數化構建

Projects to build: <your main project>
Trigger when build is: Stable
Parameters: TAG=$TAG

最后,在主構建中,使用以下字符串參數勾選“此構建已參數化”

Name: TAG
Default Value: <your release branch>

在“源代碼管理”部分中,在“要構建的分支”字段中使用“$ TAG”。

您可以安裝post-receive掛鈎,檢查是否提交了標記,並在jenkins中創建構建。

鈎子看起來像這樣[*]:

#!/usr/bin/env python

import sys
from subprocess import Popen, PIPE, check_call

def call_git(command, args):
    return Popen(['git', command] + args, stdout=PIPE).communicate()[0]
JENKINS = 'http://localhost:8002/jenkins'
TOKEN = 'asdf8saffwedssdf'
jobname = 'project-tag'

def handle_ref(old, new, ref):
     print 'handle_ref(%s, %s, %s)' % (old, new, ref)
     if not ref.startswith('refs/tags/'):
          return
     url = '%s/job/%s/buildWithParameters?token=%s&branch=%s' % (
        JENKINS, jobname, TOKEN, new)
     print "queueing jenkins job " + jobname + " for " + new
     check_call(["wget", "-O/dev/null", "--quiet", url])
if __name__ == '__main__':
    for line in sys.stdin:
        handle_ref(*line.split())

[*]注意:這只是一個稍微不同的腳本的快速轉換,所以很可能這里有一些小錯誤。 這主要是為了表明這個想法。

在jenkins方面,您需要配置參數化作業。 唯一的參數是'branch'。

  1. 檢查'此構建是否已參數化'並添加參數
  2. 在'源代碼管理 - >分支構建'put'$ branch'

這提供了一種相當安全和強大的構建方式。 要測試,通過Web界面運行構建,它將詢問參數的值。

在現代(?)多分支管道的世界中,建築標簽的工作原理如下。

  1. 添加“行為”以發現標記:
    在此輸入圖像描述
  2. 使用插件基本分支構建策略為標記添加“構建策略”: 在此輸入圖像描述 不要忘記為分支添加構建策略; 插件完全禁用默認值!

您可以使用選項“Git Publisher”作為Git插件的一部分,在成功構建/部署后創建標記。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM