Eajaz
Eajaz

Reputation: 479

Set up upstream project in Travis CI

I'm working on project, where I have two Git Repos:

Repo1 - DevRepo, Rep02- TestRepo

My Scenario is: Whenever a commit or a PR happens on the Repo1:

Step1: Immediately Repo2 should be triggered

Step2: Once Step1 is success, Repo1 should be triggered.

Basically Repo1 should build only if Repo2 is run and it turns success.

Could someone please help me how I can set this up, much appreciated:

Upvotes: 4

Views: 512

Answers (2)

Stefan van der Walt
Stefan van der Walt

Reputation: 7253

I've now built a CI service for this purpose. It lives here: https://github.com/cesium-ml/dependent_build_server

The above code basically does the following:

  1. Wait for GitHub to let it know when a PR has changed
  2. Use the Travis-CI API to trigger a build of the project, and insert two environment variables to describe which version of the code to build.
  3. Listen for Travis-CI to report back, via webhook, the result of the build.
  4. Use the GitHub API to report the new status to the PR.

Note: You have to verify the payloads that come from GitHub and Travis-CI. This is trivial for GitHub, but slightly harder for Travis-CI which uses proper key signing.

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)

Travis-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

Upvotes: 1

Eajaz
Eajaz

Reputation: 479

Answer: I got this working:

  1. Trigger your dependent build: Repo2 by using the travis api: Create a trigger_build.sh file and add this code:
`
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. Create a new or a separate get_build_status.sh file. Poll for the status of the dependent build. Based on the status of the Repo2 build, we will either continue building Repo1 or stop building Repo1:
# 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. Your .travis.yml configuration:
script:
- chmod 777 ./trigger_build.sh
- chmod 777 ./get_build_status.sh
- ./trigger_build.sh
- ./get_build_status.sh

Upvotes: 1

Related Questions