Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# install https://github.com/travis-ci/travis.rb#installation
# get API token according https://developer.travis-ci.com/authentication
$env:TRAVIS_API_TOKEN = "<replace>" # must be secure variable set in Environment tab or YAML section
$env:TimeOutMins = <replace> # how long to wait for Travis build to complete (set in Environment tab or YAML section)
$headers = @{
"Authorization" = "token $env:TRAVIS_API_TOKEN"
"Travis-API-Version" = "3"
}
[datetime]$stop = ([datetime]::Now).AddMinutes($env:TimeOutMins)
[bool]$success = $false
while(!$success -and ([datetime]::Now) -lt $stop) {
$builds = Invoke-RestMethod -Uri "https://api.travis-ci.org/builds?limit=10" -Headers $headers -Method Get
$currentBuild = $builds.builds | ? {$_.commit.sha -eq $env:APPVEYOR_REPO_COMMIT}
$success = $currentBuild.state -eq "passed"
if (!$success) {Start-sleep 5}
}
if (!$currentBuild) {throw "Could not get information about Travis build with sha $env:APPVEYOR_REPO_COMMIT"}
if (!$success) {throw "Travis build did not finished in $env:TimeOutMins minutes"}
@lipkau

This comment has been minimized.

Show comment Hide comment
@lipkau

lipkau Jan 31, 2018

Thank you for this snippet, @ilyafinkelshtyn

I have made a little changed for my use case:

function Get-TravisBuild {
    param()

    if (-not ($env:TRAVIS_API_TOKEN)) {
        throw "missing api token for Travis-CI."
    }
    if (-not ($env:APPVEYOR_ACCOUNT_NAME)) {
        throw "not an appveyor build."
    }

    Invoke-RestMethod -Uri "https://api.travis-ci.org/builds?limit=10" -Method Get -Headers @{
        "Authorization"      = "token $env:TRAVIS_API_TOKEN"
        "Travis-API-Version" = "3"
    }
}
function allCIsFinished {
    param()

    if (-not ($env:APPVEYOR_REPO_COMMIT)) {
        return $true
    }

    Write-Host "waiting on travis to finish"

    [datetime]$stop = ([datetime]::Now).AddMinutes($env:TimeOutMins)

    do {
        $builds = Get-TravisBuild
        $currentBuild = $builds.builds | Where-Object {$_.commit.sha -eq $env:APPVEYOR_REPO_COMMIT}
        switch -regex ($currentBuild.state) {
            "^passed$" {
                return $true
            }
            "^(errored|failed|canceled)" {
                throw "Travis Job ($($builds.builds.id)) failed"
            }
        }

        Start-sleep 5
    } while (([datetime]::Now) -lt $stop)
    if (!$currentBuild) {
        throw "Could not get information about Travis build with sha $REPO_COMMIT"
    }

    throw "Travis build did not finished in $env:TimeOutMins minutes"
}
allCIsFinished

The switch case "^(errored|failed|canceled)" allows the script to fail quicker; instead of always waiting for success || timeout.

lipkau commented Jan 31, 2018

Thank you for this snippet, @ilyafinkelshtyn

I have made a little changed for my use case:

function Get-TravisBuild {
    param()

    if (-not ($env:TRAVIS_API_TOKEN)) {
        throw "missing api token for Travis-CI."
    }
    if (-not ($env:APPVEYOR_ACCOUNT_NAME)) {
        throw "not an appveyor build."
    }

    Invoke-RestMethod -Uri "https://api.travis-ci.org/builds?limit=10" -Method Get -Headers @{
        "Authorization"      = "token $env:TRAVIS_API_TOKEN"
        "Travis-API-Version" = "3"
    }
}
function allCIsFinished {
    param()

    if (-not ($env:APPVEYOR_REPO_COMMIT)) {
        return $true
    }

    Write-Host "waiting on travis to finish"

    [datetime]$stop = ([datetime]::Now).AddMinutes($env:TimeOutMins)

    do {
        $builds = Get-TravisBuild
        $currentBuild = $builds.builds | Where-Object {$_.commit.sha -eq $env:APPVEYOR_REPO_COMMIT}
        switch -regex ($currentBuild.state) {
            "^passed$" {
                return $true
            }
            "^(errored|failed|canceled)" {
                throw "Travis Job ($($builds.builds.id)) failed"
            }
        }

        Start-sleep 5
    } while (([datetime]::Now) -lt $stop)
    if (!$currentBuild) {
        throw "Could not get information about Travis build with sha $REPO_COMMIT"
    }

    throw "Travis build did not finished in $env:TimeOutMins minutes"
}
allCIsFinished

The switch case "^(errored|failed|canceled)" allows the script to fail quicker; instead of always waiting for success || timeout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment