├── .editorconfig ├── .github └── FUNDING.yml ├── .gitignore ├── .npmignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js ├── index.test.js ├── package.json └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: ai 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | yarn-error.log 3 | 4 | coverage/ 5 | api.md 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | yarn-error.log 2 | yarn.lock 3 | 4 | .editorconfig 5 | .travis.yml 6 | 7 | coverage/ 8 | *.test.js 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | cache: yarn 3 | node_js: 4 | - node 5 | - "12" 6 | - "10" 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | This project adheres to [Semantic Versioning](http://semver.org/). 3 | 4 | ## 1.2.2 5 | * Remove GitHub Actions support as it wasn’t working as intended. 6 | 7 | ## 1.2.1 8 | * Reduce package size. 9 | 10 | ## 1.2 11 | * Add `CI_JOB_NUMBER` environment variable. 12 | 13 | ## 1.1 14 | * Add GitLab CI (by Denis Talakevich). 15 | 16 | ## 1.0 17 | * Add GitHub Actions support (by Aleksandr Anokhin). 18 | 19 | ## 0.3.1 20 | * Reduce package size. 21 | 22 | ## 0.3 23 | * Add Semaphore support (by Vasily Polovnyov). 24 | 25 | ## 0.2 26 | * Add Circle CI support (by Sergey Ponomarev). 27 | 28 | ## 0.1.1 29 | * Add JSDoc. 30 | 31 | ## 0.1 32 | * Initial release. 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright 2017 Andrey Sitnik 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CI Job Number 2 | 3 | Return CI job number to run huge tests only on first job. 4 | 5 | Often we test different Node.js versions on Travis CI. 6 | But Node.js version doesn’t affect on many tests. For example, using 7 | external API for docs spelling check. 8 | 9 | Since Travis CI is a free common resource, we should be responsible. 10 | So we can run big tasks only on first Node.js version. 11 | 12 | ```js 13 | const ciJobNumber = require('ci-job-number') 14 | 15 | if (ciJobNumber() === 1) { 16 | runSpellingCheck() 17 | } else { 18 | console.warn('To speed up CI spelling check runs only in first job') 19 | } 20 | ``` 21 | 22 | 23 | Sponsored by Evil Martians 25 | 26 | 27 | 28 | ## CI Support 29 | 30 | * AppVeyor 31 | * CircleCI 32 | * GitLab CI 33 | * Semaphore 34 | * Travis CI 35 | 36 | 37 | ## Who Use It 38 | 39 | * [`check-dts`](https://github.com/ai/check-dts) 40 | * [`eslint-ci`](https://github.com/JLHwung/eslint-ci) 41 | * [`jest-ci`](https://github.com/ai/jest-ci) 42 | * [`size-limit`](https://github.com/ai/size-limit) 43 | * [`spech`](https://github.com/megahertz/spech) 44 | * [`yaspeller-ci`](https://github.com/ai/yaspeller-ci) 45 | 46 | 47 | ## Override Default Behaviour 48 | 49 | `CI_JOB_NUMBER` environment variable will override CI job number. 50 | It is the best way to change default behaviour and run task on all CI jobs: 51 | 52 | ```yaml 53 | - name: Build and test 54 | run: yarn test 55 | env: 56 | CI_JOB_NUMBER: 1 57 | ``` 58 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | /** 4 | * Returns job number if project is run on CI or `1` on local. 5 | * 6 | * @return {number} CI job number 7 | * 8 | * @example 9 | * const ciJobNumber = require('ci-job-number') 10 | * 11 | * if (ciJobNumber() === 1) { 12 | * runSpellingCheck() 13 | * } else { 14 | * console.warn('To speed up CI spelling check runs only in first job') 15 | * } 16 | */ 17 | module.exports = function ciJobNumber () { 18 | if (process.env.CI_JOB_NUMBER) { 19 | return parseInt(process.env.CI_JOB_NUMBER) 20 | } else if (process.env.TRAVIS) { 21 | return parseInt(process.env.TRAVIS_JOB_NUMBER.split('.')[1]) 22 | } else if (process.env.APPVEYOR) { 23 | return parseInt(process.env.APPVEYOR_JOB_NUMBER) 24 | } else if (process.env.CIRCLECI) { 25 | return parseInt(process.env.CIRCLE_NODE_INDEX) + 1 26 | } else if (process.env.SEMAPHORE) { 27 | return parseInt(process.env.SEMAPHORE_CURRENT_THREAD) 28 | } else if (process.env.GITLAB_CI) { 29 | return parseInt(process.env.CI_NODE_INDEX || '1') 30 | } else { 31 | return 1 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /index.test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const ciJobNumber = require('./') 4 | 5 | let originEnv = process.env 6 | delete originEnv.TRAVIS 7 | beforeEach(() => { 8 | process.env = { } 9 | for (let i in originEnv) process.env[i] = originEnv[i] 10 | }) 11 | 12 | it('returns 1 when run without CI', () => { 13 | expect(ciJobNumber()).toEqual(1) 14 | }) 15 | 16 | it('supports Travis CI', () => { 17 | process.env.TRAVIS = '1' 18 | process.env.TRAVIS_JOB_NUMBER = '207.2' 19 | expect(ciJobNumber()).toEqual(2) 20 | }) 21 | 22 | it('supports AppVeyor', () => { 23 | process.env.APPVEYOR = '1' 24 | process.env.APPVEYOR_JOB_NUMBER = '3' 25 | expect(ciJobNumber()).toEqual(3) 26 | }) 27 | 28 | it('supports CircleCI', () => { 29 | process.env.CIRCLECI = 'true' 30 | process.env.CIRCLE_NODE_INDEX = '3' 31 | expect(ciJobNumber()).toEqual(4) 32 | }) 33 | 34 | it('supports Semaphore', () => { 35 | process.env.SEMAPHORE = 'true' 36 | process.env.SEMAPHORE_CURRENT_THREAD = '5' 37 | expect(ciJobNumber()).toEqual(5) 38 | }) 39 | 40 | it('supports Gitlab CI with parallel', () => { 41 | process.env.GITLAB_CI = 'true' 42 | process.env.CI_NODE_INDEX = '7' 43 | expect(ciJobNumber()).toEqual(7) 44 | }) 45 | 46 | it('supports Gitlab CI without parallel', () => { 47 | process.env.GITLAB_CI = 'true' 48 | expect(ciJobNumber()).toEqual(1) 49 | }) 50 | 51 | it('supports own variable', () => { 52 | process.env.CI_JOB_NUMBER = '8' 53 | process.env.TRAVIS = '1' 54 | process.env.TRAVIS_JOB_NUMBER = '207.2' 55 | expect(ciJobNumber()).toEqual(8) 56 | }) 57 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ci-job-number", 3 | "version": "1.2.2", 4 | "description": "Return CI job number to run huge tests only on first job", 5 | "keywords": [ 6 | "CI", 7 | "Travis", 8 | "AppVeyor", 9 | "Circle", 10 | "Semaphore", 11 | "job" 12 | ], 13 | "scripts": { 14 | "spell": "yaspeller-ci *.md", 15 | "test": "jest-ci --coverage && eslint-ci . && yarn spell" 16 | }, 17 | "author": "Andrey Sitnik ", 18 | "license": "MIT", 19 | "repository": "ai/ci-job-number", 20 | "devDependencies": { 21 | "@logux/eslint-config": "^36.1.2", 22 | "clean-publish": "^1.1.7", 23 | "eslint": "^6.8.0", 24 | "eslint-ci": "^1.0.0", 25 | "eslint-config-standard": "^14.1.1", 26 | "eslint-plugin-es5": "^1.5.0", 27 | "eslint-plugin-import": "^2.20.2", 28 | "eslint-plugin-jest": "^23.8.2", 29 | "eslint-plugin-node": "^11.1.0", 30 | "eslint-plugin-prefer-let": "^1.0.1", 31 | "eslint-plugin-promise": "^4.2.1", 32 | "eslint-plugin-security": "^1.4.0", 33 | "eslint-plugin-standard": "^4.0.1", 34 | "eslint-plugin-unicorn": "^18.0.1", 35 | "husky": "^4.2.5", 36 | "jest": "^25.3.0", 37 | "jest-ci": "^0.1.1", 38 | "lint-staged": "^10.1.4", 39 | "pre-commit": "^1.2.2", 40 | "yaspeller-ci": "^1.0.2" 41 | }, 42 | "eslintConfig": { 43 | "extends": "@logux/eslint-config" 44 | }, 45 | "lint-staged": { 46 | "*.md": "yaspeller", 47 | "*.js": "eslint" 48 | }, 49 | "husky": { 50 | "hooks": { 51 | "pre-commit": "lint-staged" 52 | } 53 | }, 54 | "yaspeller": { 55 | "lang": "en", 56 | "ignoreCapitalization": true, 57 | "ignoreText": [ 58 | " \\(by [^)]+\\)." 59 | ], 60 | "dictionary": [ 61 | "Versioning", 62 | "Travis", 63 | "AppVeyor", 64 | "CircleCI", 65 | "CI", 66 | "JSDoc", 67 | "js", 68 | "GitLab", 69 | "GitHub" 70 | ] 71 | } 72 | } 73 | --------------------------------------------------------------------------------