在Travis CI建立上游项目

时间:2016-02-02 23:44:01

标签: github continuous-integration travis-ci

我正在开展项目,我有两个Git Repos:

Repo1 - DevRepo,Rep02- TestRepo

我的情景是: 无论何时在Repo1上发生提交或PR:

步骤1:应立即触发Repo2

步骤2:Step1成功后,应触发Repo1。

基本上Repo1只有在Repo2运行并且成功时才会构建。

有人可以帮助我如何设置它,非常感谢:

  • 我应配置.travis.yml文件以满足我的情况。
  • 我可以在.travis.yml文件中编写的确切配置步骤

2 个答案:

答案 0 :(得分:1)

答案:我有这个工作:

  1. 使用travis api触发您的依赖构建:Repo2: 创建一个trigger_build.sh文件并添加以下代码:
  2. `
    body='{
    "request": {
      "branch":"master"
    }}'
    
    curl -s -X POST \
      -H "Content-Type: application/json" \
      -H "Accept: application/json" \
      -H "Travis-API-Version: 3" \
      -H "Authorization: token ..git_hub_login_token.." \
      -d "$body" \
      https://api.travis-ci.org/repo/xxxx%2Fyyyy/requests
    
    #The 15s sleep is to allow Travis to trigger the dependent build:
    sleep 15`
    
    1. 创建一个新的或单独的get_build_status.sh文件。轮询依赖构建的状态。根据Repo2构建的状态,我们将继续构建Repo1或停止构建Repo1:
    2. # Polling for the Repo2 build status
      # Setting a maximum time for the Repo2 build to run, however once we get the status to passed/failed, we will return to the Repo1 build run
      
      `i=1
      max=300
      while [ $i -lt $max ]
      do
      
      echo "--------------------------------------------"
      echo "Polling for the tests run build status..."`
      
      curl -i -H "Accept: application/vnd.travis-ci.2+json" "https://api.travis-ci.org/repos/xxxx/yyyy/builds" > test.json
      LATEST_STATE=$(grep -o '"state":.[a-z\"]*' test.json | head -1)
      #LATEST_ID=$(grep -o '"id":.[0-9]*' test.json | head -1 | grep ':.[0-9]*')
      
      get_state_value=${LATEST_STATE#*:}
      STATE="${get_state_value//\"}"
      
      if [ $STATE == "passed" ]
      then
        echo "TESTS RUN... $STATE :-) "
        break #As soon as the Repo2 run pass, we break and return back to the Repo1 build run
      elif [ $STATE == "failed" ]
      then
       echo "TESTS RUN... $STATE :-("
       echo "Stop building elements"
       exit 1 #As soon as the Repo2 run fail, we stop building Repo1
      fi
      
      true $(( i++ ))
      sleep 1 #This 1s is required to poll the build status for every second
      done
      
      
      1. 您的.travis.yml配置:
      2. script:
        - chmod 777 ./trigger_build.sh
        - chmod 777 ./get_build_status.sh
        - ./trigger_build.sh
        - ./get_build_status.sh
        
        

答案 1 :(得分:1)

我现在为此目的建立了CI服务。它住在这里:https://github.com/cesium-ml/dependent_build_server

以上代码基本上执行以下操作:

  1. 等待GitHub在PR更改时通知
  2. 使用Travis-CI API触发项目的构建,并插入两个环境变量来描述要构建的代码版本。
  3. 听取Travis-CI通过webhook报告构建的结果。
  4. 使用GitHub API向PR报告新状态。
  5. 注意:您必须验证来自GitHub和Travis-CI的有效负载。这对于GitHub来说是微不足道的,但对于使用正确密钥签名的Travis-CI来说稍微困难一些。

    GitHub的:

    import hmac
    
    def verify_signature(payload, signature, secret):
        expected = 'sha1=' + hmac.new(secret.encode('ascii'),
                                      payload, 'sha1').hexdigest()   
        return hmac.compare_digest(signature, expected)
    

    特拉维斯-CI:

    from OpenSSL import crypto
    
    def verify_signature(payload, signature):
    
        # Get this by querying the Travis-CI config API
        public_key = crypto.load_publickey(crypto.FILETYPE_PEM, pubkey)
        certificate = crypto.X509()
        certificate.set_pubkey(public_key)
    
        try:
            crypto.verify(certificate, signature, payload, 'sha1')
            return True
        except crypto.Error:
            return False