Skip to content

Commit

Permalink
feat: sync with watson/ci-info (#4)
Browse files Browse the repository at this point in the history
add support for
- gerrit
- google cloud buid
- for anonymous CI_NAME env
- for heroku
- for VERCEL_URL
  • Loading branch information
gkampitakis authored Nov 19, 2022
1 parent 2bccaca commit 9911c4b
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 137 deletions.
83 changes: 43 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,46 +40,49 @@ if ciinfo.IsCI {

Officially supported CI servers:

| Name | Constant | isPR |
| ------------------------------------------------------------------------------- | -------------------- | ---- |
| [AWS CodeBuild](https://aws.amazon.com/codebuild/) | `CODEBUILD` | 🚫 |
| [AppVeyor](http://www.appveyor.com) | `APPVEYOR` ||
| [Azure Pipelines](https://azure.microsoft.com/en-us/services/devops/pipelines/) | `AZURE_PIPELINES` ||
| [Appcircle](https://appcircle.io/) | `APPCIRCLE` | 🚫 |
| [Bamboo](https://www.atlassian.com/software/bamboo) by Atlassian | `BAMBOO` | 🚫 |
| [Bitbucket Pipelines](https://bitbucket.org/product/features/pipelines) | `BITBUCKET` ||
| [Bitrise](https://www.bitrise.io/) | `BITRISE` ||
| [Buddy](https://buddy.works/) | `BUDDY` ||
| [Buildkite](https://buildkite.com) | `BUILDKITE` ||
| [CircleCI](http://circleci.com) | `CIRCLE` ||
| [Cirrus CI](https://cirrus-ci.org) | `CIRRUS` ||
| [Codefresh](https://codefresh.io/) | `CODEFRESH` ||
| [Codeship](https://codeship.com) | `CODESHIP` | 🚫 |
| [Drone](https://drone.io) | `DRONE` ||
| [dsari](https://github.com/rfinnie/dsari) | `DSARI` | 🚫 |
| [Expo Application Services](https://expo.dev/eas) | `EAS_BUILD` | 🚫 |
| [GitHub Actions](https://github.com/features/actions/) | `GITHUB_ACTIONS` ||
| [GitLab CI](https://about.gitlab.com/gitlab-ci/) | `GITLAB` ||
| [GoCD](https://www.go.cd/) | `GOCD` | 🚫 |
| [Hudson](http://hudson-ci.org) | `HUDSON` | 🚫 |
| [Jenkins CI](https://jenkins-ci.org) | `JENKINS` ||
| [LayerCI](https://layerci.com/) | `LAYERCI` ||
| [Magnum CI](https://magnum-ci.com) | `MAGNUM` | 🚫 |
| [Netlify CI](https://www.netlify.com/) | `NETLIFY` ||
| [Nevercode](http://nevercode.io/) | `NEVERCODE` ||
| [Render](https://render.com/) | `RENDER` ||
| [Sail CI](https://sail.ci/) | `SAIL` ||
| [Screwdriver](https://screwdriver.cd/) | `SCREWDRIVER` ||
| [Semaphore](https://semaphoreci.com) | `SEMAPHORE` ||
| [Shippable](https://www.shippable.com/) | `SHIPPABLE` ||
| [Solano CI](https://www.solanolabs.com/) | `SOLANO` ||
| [Strider CD](https://strider-cd.github.io/) | `STRIDER` | 🚫 |
| [TaskCluster](http://docs.taskcluster.net) | `TASKCLUSTER` | 🚫 |
| [TeamCity](https://www.jetbrains.com/teamcity/) by JetBrains | `TEAMCITY` | 🚫 |
| [Travis CI](http://travis-ci.org) | `TRAVIS` ||
| [Vercel](https://vercel.com/) | `VERCEL` | 🚫 |
| [Visual Studio App Center](https://appcenter.ms/) | `APPCENTER` | 🚫 |
| [Woodpecker](https://woodpecker-ci.org/) | `ci.WOODPECKER` ||
| Name | Constant | isPR |
| ------------------------------------------------------------------------------- | -------------------- | ---- |
| [AWS CodeBuild](https://aws.amazon.com/codebuild/) | `ci.CODEBUILD` | 🚫 |
| [AppVeyor](http://www.appveyor.com) | `ci.APPVEYOR` ||
| [Azure Pipelines](https://azure.microsoft.com/en-us/services/devops/pipelines/) | `ci.AZURE_PIPELINES` ||
| [Appcircle](https://appcircle.io/) | `ci.APPCIRCLE` | 🚫 |
| [Bamboo](https://www.atlassian.com/software/bamboo) by Atlassian | `ci.BAMBOO` | 🚫 |
| [Bitbucket Pipelines](https://bitbucket.org/product/features/pipelines) | `ci.BITBUCKET` ||
| [Bitrise](https://www.bitrise.io/) | `ci.BITRISE` ||
| [Buddy](https://buddy.works/) | `ci.BUDDY` ||
| [Buildkite](https://buildkite.com) | `ci.BUILDKITE` ||
| [CircleCI](http://circleci.com) | `ci.CIRCLE` ||
| [Cirrus CI](https://cirrus-ci.org) | `ci.CIRRUS` ||
| [Codefresh](https://codefresh.io/) | `ci.CODEFRESH` ||
| [Codeship](https://codeship.com) | `ci.CODESHIP` | 🚫 |
| [Drone](https://drone.io) | `ci.DRONE` ||
| [dsari](https://github.com/rfinnie/dsari) | `ci.DSARI` | 🚫 |
| [Expo Application Services](https://expo.dev/eas) | `ci.EAS` | 🚫 |
| [Gerrit CI](https://www.gerritcodereview.com) | `ci.GERRIT` | 🚫 |
| [GitHub Actions](https://github.com/features/actions/) | `ci.GITHUB_ACTIONS` ||
| [GitLab CI](https://about.gitlab.com/gitlab-ci/) | `ci.GITLAB` ||
| [GoCD](https://www.go.cd/) | `ci.GOCD` | 🚫 |
| [Google Cloud Build](https://cloud.google.com/build) | `ci.GOOGLE_CLOUD_BUILD` | 🚫 |
| [Heroku](https://www.heroku.com) | `ci.HEROKU` | 🚫 |
| [Hudson](http://hudson-ci.org) | `ci.HUDSON` | 🚫 |
| [Jenkins CI](https://jenkins-ci.org) | `ci.JENKINS` ||
| [LayerCI](https://layerci.com/) | `ci.LAYERCI` ||
| [Magnum CI](https://magnum-ci.com) | `ci.MAGNUM` | 🚫 |
| [Netlify CI](https://www.netlify.com/) | `ci.NETLIFY` ||
| [Nevercode](http://nevercode.io/) | `ci.NEVERCODE` ||
| [Render](https://render.com/) | `ci.RENDER` ||
| [Sail CI](https://sail.ci/) | `ci.SAIL` ||
| [Screwdriver](https://screwdriver.cd/) | `ci.SCREWDRIVER` ||
| [Semaphore](https://semaphoreci.com) | `ci.SEMAPHORE` ||
| [Shippable](https://www.shippable.com/) | `ci.SHIPPABLE` ||
| [Solano CI](https://www.solanolabs.com/) | `ci.SOLANO` ||
| [Strider CD](https://strider-cd.github.io/) | `ci.STRIDER` | 🚫 |
| [TaskCluster](http://docs.taskcluster.net) | `ci.TASKCLUSTER` | 🚫 |
| [TeamCity](https://www.jetbrains.com/teamcity/) by JetBrains | `ci.TEAMCITY` | 🚫 |
| [Travis CI](http://travis-ci.org) | `ci.TRAVIS` ||
| [Vercel](https://vercel.com/) | `ci.VERCEL` | 🚫 |
| [Visual Studio App Center](https://appcenter.ms/) | `ci.APPCENTER` | 🚫 |
| [Woodpecker](https://woodpecker-ci.org/) | `ci.WOODPECKER` ||

## API

Expand Down
30 changes: 25 additions & 5 deletions ciinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ciinfo

import (
"os"
"strings"
)

var (
Expand All @@ -24,14 +25,27 @@ func everyEnv(envs []env, check func(env) bool) bool {
return true
}

func anyEnv(envs []env, check func(env) bool) bool {
for _, env := range envs {
if check(env) {
return true
}
}
return false
}

func initialize() {
vendorsIsCI = make(map[string]bool)
IsPr = false
IsCI = false
Name = ""

for _, vendor := range vendors {
isVendor := everyEnv(vendor.env, verifyCI)
checkEnvs := everyEnv
if vendor.anyEnv {
checkEnvs = anyEnv
}
isVendor := checkEnvs(vendor.env, verifyCI)
vendorsIsCI[vendor.constant] = isVendor
if !isVendor {
continue
Expand All @@ -51,13 +65,15 @@ func initialize() {

func isCI() bool {
envKeys := []string{
"BUILD_ID", // Jenkins, Cloudbees
"BUILD_NUMBER", // Jenkins, TeamCity
"CI", // Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari
"CI_APP_ID", // Appflow
"CI_BUILD_ID", // Appflow
"CI_BUILD_NUMBER", // Appflow
"CI_NAME", // Codeship and others
"CONTINUOUS_INTEGRATION", // Travis CI, Cirrus CI
"BUILD_NUMBER", // Jenkins, TeamCity
"RUN_ID", // TaskCluster, dsari
"CI_APP_ID", // Applfow
"CI_BUILD_ID", // Applfow
"CI_BUILD_NUMBER", // Applfow
}

for _, key := range envKeys {
Expand All @@ -80,6 +96,10 @@ func verifyCI(e env) bool {
return false
}

if e.includes != "" && !strings.Contains(value, e.includes) {
return false
}

if e.eq != "" && value != e.eq {
return false
}
Expand Down
69 changes: 53 additions & 16 deletions ciinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestCI(t *testing.T) {

initialize()

assertEqual(t, 41, len(vendors), "We should have 41 vendors")
assertEqual(t, 44, len(vendors), "We should have 44 vendors")
assertEqual(t, true, IsCI)
assertEqual(t, isActualPr(), IsPr)
assertEqual(t, "GitHub Actions", Name)
Expand All @@ -90,12 +90,15 @@ func TestCI(t *testing.T) {

t.Run("Anonymous CI", func(t *testing.T) {
envKeys := []string{
"BUILD_ID", // Jenkins, Cloudbees
"BUILD_NUMBER", // Jenkins, TeamCity
"CI", // Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari
"CI_APP_ID", // Appflow
"CI_BUILD_ID", // Appflow
"CI_BUILD_NUMBER", // Appflow
"CI_NAME", // Codeship and others
"CONTINUOUS_INTEGRATION", // Travis CI, Cirrus CI
"BUILD_NUMBER", // Jenkins, TeamCity
"CI_APP_ID", // Applfow
"CI_BUILD_ID", // Applfow
"CI_BUILD_NUMBER", // Applfow
"RUN_ID", // TaskCluster, dsari
}

for _, key := range envKeys {
Expand All @@ -112,16 +115,6 @@ func TestCI(t *testing.T) {
}
})

t.Run("Not Codeship", func(t *testing.T) {
setEnv(t, "CI_NAME", "invalid")

initialize()

assertEqual(t, false, IsCI)
assertEqual(t, false, IsPr)
assertEqual(t, "", Name)
assertEqual(t, false, IsVendor("CODESHIP"))
})
for _, scenario := range []TestScenario{
{
description: "AppVeyor - PR",
Expand Down Expand Up @@ -570,7 +563,7 @@ func TestCI(t *testing.T) {
},
},
{
description: "Vercel",
description: "Vercel - NOW_BUILDER",
expected: ScenarioExpected{
isPR: false,
name: "Vercel",
Expand All @@ -580,6 +573,17 @@ func TestCI(t *testing.T) {
setEnv(t, "NOW_BUILDER", "1")
},
},
{
description: "Vercel - VERCEL_URL",
expected: ScenarioExpected{
isPR: false,
name: "Vercel",
constant: "VERCEL",
},
setup: func(t *testing.T) {
setEnv(t, "VERCEL_URL", "1")
},
},
{
description: "Nevercode - PR",
expected: ScenarioExpected{
Expand Down Expand Up @@ -731,6 +735,39 @@ func TestCI(t *testing.T) {
setEnv(t, "CI", "woodpecker")
},
},
{
description: "Heroku",
expected: ScenarioExpected{
isPR: false,
name: "Heroku",
constant: "HEROKU",
},
setup: func(t *testing.T) {
setEnv(t, "NODE", "/extra/content/app/.heroku/node/bin/node --extra --content")
},
},
{
description: "Gerrit",
expected: ScenarioExpected{
isPR: false,
name: "Gerrit",
constant: "GERRIT",
},
setup: func(t *testing.T) {
setEnv(t, "GERRIT_PROJECT", "1")
},
},
{
description: "Google Cloud Build",
expected: ScenarioExpected{
isPR: false,
name: "Google Cloud Build",
constant: "GOOGLE_CLOUD_BUILD",
},
setup: func(t *testing.T) {
setEnv(t, "BUILDER_OUTPUT", "1")
},
},
} {
t.Run(scenario.description, func(t *testing.T) {
scenario.setup(t)
Expand Down
43 changes: 26 additions & 17 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package ciinfo

// This file is generated. Run `make compile-constants` to update.

// CI is running on Appcircle
var APPCIRCLE = vendorsIsCI["APPCIRCLE"]

// CI is running on AppVeyor
var APPVEYOR = vendorsIsCI["APPVEYOR"]

// CI is running on AWS CodeBuild
var CODEBUILD = vendorsIsCI["CODEBUILD"]

// CI is running on Azure Pipelines
var AZURE_PIPELINES = vendorsIsCI["AZURE_PIPELINES"]

// CI is running on Appcircle
var APPCIRCLE = vendorsIsCI["APPCIRCLE"]

// CI is running on Bamboo
var BAMBOO = vendorsIsCI["BAMBOO"]

Expand All @@ -32,15 +35,12 @@ var CIRCLE = vendorsIsCI["CIRCLE"]
// CI is running on Cirrus CI
var CIRRUS = vendorsIsCI["CIRRUS"]

// CI is running on Codemagic
var CODEMAGIC = vendorsIsCI["CODEMAGIC"]

// CI is running on AWS CodeBuild
var CODEBUILD = vendorsIsCI["CODEBUILD"]

// CI is running on Codefresh
var CODEFRESH = vendorsIsCI["CODEFRESH"]

// CI is running on Codemagic
var CODEMAGIC = vendorsIsCI["CODEMAGIC"]

// CI is running on Codeship
var CODESHIP = vendorsIsCI["CODESHIP"]

Expand All @@ -53,6 +53,9 @@ var DSARI = vendorsIsCI["DSARI"]
// CI is running on Expo Application Services
var EAS = vendorsIsCI["EAS"]

// CI is running on Gerrit
var GERRIT = vendorsIsCI["GERRIT"]

// CI is running on GitHub Actions
var GITHUB_ACTIONS = vendorsIsCI["GITHUB_ACTIONS"]

Expand All @@ -62,15 +65,21 @@ var GITLAB = vendorsIsCI["GITLAB"]
// CI is running on GoCD
var GOCD = vendorsIsCI["GOCD"]

// CI is running on LayerCI
var LAYERCI = vendorsIsCI["LAYERCI"]
// CI is running on Google Cloud Build
var GOOGLE_CLOUD_BUILD = vendorsIsCI["GOOGLE_CLOUD_BUILD"]

// CI is running on Hudson
var HUDSON = vendorsIsCI["HUDSON"]

// CI is running on Heroku
var HEROKU = vendorsIsCI["HEROKU"]

// CI is running on Jenkins
var JENKINS = vendorsIsCI["JENKINS"]

// CI is running on LayerCI
var LAYERCI = vendorsIsCI["LAYERCI"]

// CI is running on Magnum CI
var MAGNUM = vendorsIsCI["MAGNUM"]

Expand All @@ -86,12 +95,12 @@ var RENDER = vendorsIsCI["RENDER"]
// CI is running on Sail CI
var SAIL = vendorsIsCI["SAIL"]

// CI is running on Semaphore
var SEMAPHORE = vendorsIsCI["SEMAPHORE"]

// CI is running on Screwdriver
var SCREWDRIVER = vendorsIsCI["SCREWDRIVER"]

// CI is running on Semaphore
var SEMAPHORE = vendorsIsCI["SEMAPHORE"]

// CI is running on Shippable
var SHIPPABLE = vendorsIsCI["SHIPPABLE"]

Expand All @@ -116,11 +125,11 @@ var VERCEL = vendorsIsCI["VERCEL"]
// CI is running on Visual Studio App Center
var APPCENTER = vendorsIsCI["APPCENTER"]

// CI is running on Woodpecker
var WOODPECKER = vendorsIsCI["WOODPECKER"]

// CI is running on Xcode Cloud
var XCODE_CLOUD = vendorsIsCI["XCODE_CLOUD"]

// CI is running on Xcode Server
var XCODE_SERVER = vendorsIsCI["XCODE_SERVER"]

// CI is running on Woodpecker
var WOODPECKER = vendorsIsCI["WOODPECKER"]
Loading

0 comments on commit 9911c4b

Please sign in to comment.