Compare commits
11 commits
inside-ci_
...
master
Author | SHA1 | Date | |
---|---|---|---|
a79e6cca09 | |||
9fb1f9e926 | |||
67da8c7d19 | |||
ebb04485c8 | |||
58c0b1e613 | |||
0ef8c6e641 | |||
d46e0d834e | |||
95f00d8700 | |||
6e6d989a50 | |||
e866e58dda | |||
b5312312ba |
11 changed files with 98 additions and 84 deletions
|
@ -16,17 +16,26 @@ jobs:
|
|||
run: pnpm run test --ignore-only
|
||||
- name: Sanity check it detects this CI
|
||||
run: |
|
||||
if node index.js; then
|
||||
echo "Success"
|
||||
if node in.js; then
|
||||
echo "in.js: Success"
|
||||
else
|
||||
echo "Failed for forgejo"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if node not.js; then
|
||||
echo "Failed for forgejo"
|
||||
exit 1
|
||||
else
|
||||
echo "not.js: Success"
|
||||
fi
|
||||
- name: Deply if new version
|
||||
run: |
|
||||
echo ""
|
||||
echo "------------------------------------"
|
||||
echo ""
|
||||
./build.sh
|
||||
|
||||
CURR_VER="$(cat package.json | jq -r .name)_v$(cat package.json | jq -r .version)"
|
||||
CURR_NAME="$(cat package.json | jq -r .name) v$(cat package.json | jq -r .version)"
|
||||
|
||||
|
|
1
.npmrc
Normal file
1
.npmrc
Normal file
|
@ -0,0 +1 @@
|
|||
package-lock=false
|
23
README.md
23
README.md
|
@ -1,22 +1,25 @@
|
|||
# inside-ci
|
||||
|
||||
Quick tool to check if we are inside a CI environment
|
||||
Returns true if code is running inside CI, otherwise false.
|
||||
|
||||
This also works as a drop-in replacement for anyone currently using `is-ci`.
|
||||
|
||||
If your CI is not being detected, feel free to [add an issue for it](https://git.nfp.is/TheThing/inside-ci/issues) or hit me up on [X (formerly Twitter)](https://x.com/TheThing89) or on discord as `thething_89`.
|
||||
|
||||
# API
|
||||
|
||||
`insideCi()`
|
||||
|
||||
Returns true if inside CI. Otherwise returns false.
|
||||
`const inside = require('inside-ci')`
|
||||
|
||||
# CLI
|
||||
|
||||
`inside-ci`
|
||||
## `inside-ci` or `in-ci`
|
||||
|
||||
Returns code 0 if inside CI. Otherwise returns an error code of 1.
|
||||
Returns error code 0 if inside CI.
|
||||
|
||||
`inside-ci || echo 'We are not inside CI, install some stuff'`
|
||||
`inside-ci && echo 'We are inside CI, install some stuff'`
|
||||
`in-ci && echo 'running inside CI'`
|
||||
|
||||
Example:
|
||||
## `not-ci`
|
||||
|
||||
`is-ci || husky install`
|
||||
Inverse of the above:
|
||||
|
||||
`not-ci && husky install`
|
||||
|
|
5
build.sh
Executable file
5
build.sh
Executable file
|
@ -0,0 +1,5 @@
|
|||
#!/bin/bash
|
||||
|
||||
mv package.json package.json.backup
|
||||
|
||||
jq -c 'pick(.name, .version, .description, .main, .bin, .repository, .keywords, .author, .license, .files)' package.json.backup > package.json
|
10
in.js
Executable file
10
in.js
Executable file
|
@ -0,0 +1,10 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
// Bail out if CI is overrided
|
||||
let is = module.exports = process.env.CI === 'false'
|
||||
? false
|
||||
: !!['CI','CI_APP_ID','BUILD_NUMBER','CI_NAME','RUN_ID'].some(x => process.env[x])
|
||||
|
||||
if (require.main === module) {
|
||||
process.exit(is ? 0 : 1)
|
||||
}
|
2
index.d.ts
vendored
2
index.d.ts
vendored
|
@ -1,2 +0,0 @@
|
|||
/** Returns true if current environment is running inside CI. Otherwise returns false. */
|
||||
export function insideCi()
|
22
index.js
22
index.js
|
@ -1,22 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
/** Returns true if current environment is running inside CI. Otherwise returns false. */
|
||||
function insideCi() {
|
||||
// Bail out if this is specifically overwritten to false.
|
||||
// Some users seem to wanna be able to do that
|
||||
if (process.env.CI === 'false') return false
|
||||
|
||||
// Most CI use env.CI (travis, Gitlab, etc.)
|
||||
// There are some exceptions though:
|
||||
// CI_APP_ID is used by Appflow: https://ionic.io/docs/appflow/package/environments#predefined-environments
|
||||
// CI_NAME is used by Codeship:https://docs.cloudbees.com/docs/cloudbees-codeship/latest/pro-builds-and-configuration/environment-variables
|
||||
// BUILD_NUMBER is used by TeamCity: https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html#Predefined+Server+Build+Parameters
|
||||
// RUN_ID is used by Taskcluster: https://docs.taskcluster.net/docs/reference/workers/docker-worker/environment
|
||||
return Boolean(['CI','CI_APP_ID','BUILD_NUMBER','CI_NAME','RUN_ID'].some(x => process.env[x]))
|
||||
}
|
||||
|
||||
module.exports.insideCi = insideCi
|
||||
|
||||
if (require.main === module) {
|
||||
process.exit(insideCi() ? 0 : 1)
|
||||
}
|
2
not.js
Executable file
2
not.js
Executable file
|
@ -0,0 +1,2 @@
|
|||
#!/usr/bin/env node
|
||||
process.exit(!require("./in.js") ? 0 : 1)
|
26
package-lock.json
generated
26
package-lock.json
generated
|
@ -1,26 +0,0 @@
|
|||
{
|
||||
"name": "inside-ci",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "inside-ci",
|
||||
"version": "1.0.0",
|
||||
"license": "WTFPL",
|
||||
"devDependencies": {
|
||||
"eltro": "^1.6.1"
|
||||
}
|
||||
},
|
||||
"node_modules/eltro": {
|
||||
"version": "1.6.1",
|
||||
"resolved": "https://registry.npmjs.org/eltro/-/eltro-1.6.1.tgz",
|
||||
"integrity": "sha512-g291jOxdUPnXBexOedGFNhswQ4127EuPOmYG6UMkPnowCUw91JRyogoEQKxtIrnhrfGR60b98C54GUzi3pJ44Q==",
|
||||
"dev": true,
|
||||
"license": "WTFPL",
|
||||
"bin": {
|
||||
"eltro": "cli.mjs"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
18
package.json
18
package.json
|
@ -1,18 +1,20 @@
|
|||
{
|
||||
"name": "inside-ci",
|
||||
"version": "1.0.0",
|
||||
"version": "2.0.1",
|
||||
"description": "Quick tool to check if we are inside CI",
|
||||
"main": "index.js",
|
||||
"main": "in.js",
|
||||
"scripts": {
|
||||
"test": "eltro -r dot test.mjs",
|
||||
"test:watch": "eltro -r dot -w test test.mjs"
|
||||
},
|
||||
"bin": {
|
||||
"inside-ci": "./index.js"
|
||||
"inside-ci": "./in.js",
|
||||
"in-ci": "./in.js",
|
||||
"not-ci": "./not.js"
|
||||
},
|
||||
"watch": {
|
||||
"test": {
|
||||
"patterns": ["index.js", "test.mjs"]
|
||||
"patterns": ["in.js", "test.mjs"]
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
|
@ -21,9 +23,7 @@
|
|||
},
|
||||
"keywords": [
|
||||
"ci",
|
||||
"is-ci",
|
||||
"inside-ci",
|
||||
"environment"
|
||||
"is-ci"
|
||||
],
|
||||
"author": "Jonatan Nilsson",
|
||||
"license": "WTFPL",
|
||||
|
@ -31,8 +31,8 @@
|
|||
"eltro": "^1.6.1"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts",
|
||||
"in.js",
|
||||
"not.js",
|
||||
"README.md",
|
||||
"LICENSE"
|
||||
]
|
||||
|
|
60
test.mjs
60
test.mjs
|
@ -1,8 +1,15 @@
|
|||
import { exec } from 'child_process'
|
||||
import { Eltro as t, assert } from 'eltro'
|
||||
import { insideCi } from './index.js'
|
||||
import { createRequire } from 'node:module';
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
t.describe('#insideCi()', function () {
|
||||
t.describe('in.js', function () {
|
||||
// Most CI use env.CI (travis, Gitlab, etc.)
|
||||
// There are some exceptions though:
|
||||
// CI_APP_ID is used by Appflow: https://ionic.io/docs/appflow/package/environments#predefined-environments
|
||||
// CI_NAME is used by Codeship:https://docs.cloudbees.com/docs/cloudbees-codeship/latest/pro-builds-and-configuration/environment-variables
|
||||
// BUILD_NUMBER is used by TeamCity: https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html#Predefined+Server+Build+Parameters
|
||||
// RUN_ID is used by Taskcluster: https://docs.taskcluster.net/docs/reference/workers/docker-worker/environment
|
||||
const testVariables = [
|
||||
'CI',
|
||||
'CI_APP_ID',
|
||||
|
@ -17,27 +24,35 @@ t.describe('#insideCi()', function () {
|
|||
}
|
||||
})
|
||||
|
||||
let n = 1
|
||||
function insideCi() {
|
||||
Object.keys(require.cache).forEach(key => {
|
||||
delete require.cache[key]
|
||||
})
|
||||
return import(`./in.js?${n++}`).then(x => x.default)
|
||||
}
|
||||
|
||||
testVariables.forEach(name => {
|
||||
t.test(`env.${name} should return true if set`, function () {
|
||||
t.test(`env.${name} should return true if set`, async function () {
|
||||
process.env[name] = 'asdf'
|
||||
assert.ok(insideCi())
|
||||
assert.ok(await insideCi())
|
||||
})
|
||||
})
|
||||
|
||||
t.test('should return false by default', function() {
|
||||
assert.notOk(insideCi())
|
||||
t.test('should return false by default', async function() {
|
||||
assert.notOk(await insideCi())
|
||||
})
|
||||
|
||||
t.test('should return false if all are empty strings', function () {
|
||||
t.test('should return false if all are empty strings', async function () {
|
||||
for (let name of testVariables) {
|
||||
process.env[name] = ''
|
||||
}
|
||||
assert.notOk(insideCi())
|
||||
assert.notOk(await insideCi())
|
||||
})
|
||||
|
||||
t.test('should return false if env.CI is specifically "false"', function () {
|
||||
t.test('should return false if env.CI is specifically "false"', async function () {
|
||||
process.env.CI = 'false'
|
||||
assert.notOk(insideCi())
|
||||
assert.notOk(await insideCi())
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -51,9 +66,9 @@ function runCommand(command, options) {
|
|||
})
|
||||
}
|
||||
|
||||
t.describe('CLI', function() {
|
||||
t.describe('CLI in-ci', function() {
|
||||
t.test('should return success/code 0 if CI is filled', async function () {
|
||||
let result = await runCommand('node index.js', {
|
||||
let result = await runCommand('node in.js', {
|
||||
env: { CI: 'true' }
|
||||
})
|
||||
|
||||
|
@ -61,7 +76,7 @@ t.describe('CLI', function() {
|
|||
})
|
||||
|
||||
t.test('should return error code 1 if CI is false', async function () {
|
||||
let result = await runCommand('node index.js', {
|
||||
let result = await runCommand('node in.js', {
|
||||
env: { CI: 'false' }
|
||||
})
|
||||
|
||||
|
@ -69,3 +84,22 @@ t.describe('CLI', function() {
|
|||
assert.strictEqual(result.err.code, 1)
|
||||
})
|
||||
})
|
||||
|
||||
t.describe('CLI not-ci', function() {
|
||||
t.test('should return error code 1 if CI is filled', async function () {
|
||||
let result = await runCommand('node not.js', {
|
||||
env: { CI: 'true' }
|
||||
})
|
||||
|
||||
assert.ok(result.err)
|
||||
assert.strictEqual(result.err.code, 1)
|
||||
})
|
||||
|
||||
t.test('should return success/code 0 if CI is false', async function () {
|
||||
let result = await runCommand('node not.js', {
|
||||
env: { CI: 'false' }
|
||||
})
|
||||
|
||||
assert.notOk(result.err)
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue