diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 68f755c5..480871d9 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -1,10 +1,9 @@ name: Node CI -on: [push] +on: [push, pull_request] jobs: build: - runs-on: ubuntu-latest strategy: @@ -12,7 +11,7 @@ jobs: node-version: [10.x, 12.x, 14.x] steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: @@ -24,3 +23,5 @@ jobs: npm test env: CI: true + - name: Upload coverage to Codecov + run: ./bin/codecov diff --git a/lib/detect.js b/lib/detect.js index e40e0f1c..7c2e9348 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -16,6 +16,7 @@ var services = { heroku: require('./services/heroku'), teamcity: require('./services/teamcity'), codebuild: require('./services/codebuild'), + github_actions: require('./services/github_actions'), } var detectProvider = function() { diff --git a/lib/services/github_actions.js b/lib/services/github_actions.js new file mode 100644 index 00000000..8ed08806 --- /dev/null +++ b/lib/services/github_actions.js @@ -0,0 +1,27 @@ +module.exports = { + detect: function() { + return !!process.env.GITHUB_ACTIONS + }, + + configuration: function() { + // https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables + console.log(' GitHub Actions CI Detected') + + var params = { + branch: + process.env.GITHUB_HEAD_REF || + process.env.GITHUB_REF.replace('refs/heads/', ''), + build: process.env.GITHUB_RUN_ID, + commit: process.env.GITHUB_SHA, + service: 'github-actions', + slug: process.env.GITHUB_REPOSITORY, + } + + if (process.env.GITHUB_HEAD_REF) { + // PR refs are in the format: refs/pull/7/merge for pull_request events + params['pr'] = process.env.GITHUB_REF.split('/')[2] + } + + return params + }, +} diff --git a/test/services/github_actions.test.js b/test/services/github_actions.test.js new file mode 100644 index 00000000..73c886ef --- /dev/null +++ b/test/services/github_actions.test.js @@ -0,0 +1,41 @@ +var github_actions = require('../../lib/services/github_actions') + +describe('GitHub Actions CI Provider', function() { + it('can detect GitHub Actions', function() { + process.env.GITHUB_ACTIONS = '1' + expect(github_actions.detect()).toBe(true) + }) + + it('can get GitHub Actions env info on push event', function() { + delete process.env.GITHUB_HEAD_REF + process.env.GITHUB_REF = 'refs/heads/master' + process.env.GITHUB_REPOSITORY = 'codecov/codecov-repo' + process.env.GITHUB_RUN_ID = '257701960' + process.env.GITHUB_SHA = '743b04806ea677403aa2ff26c6bdeb85005de658' + + expect(github_actions.configuration()).toEqual({ + branch: 'master', + build: '257701960', + commit: '743b04806ea677403aa2ff26c6bdeb85005de658', + service: 'github-actions', + slug: 'codecov/codecov-repo', + }) + }) + + it('can get GitHub Actions env info on pull request', function() { + process.env.GITHUB_HEAD_REF = 'develop' + process.env.GITHUB_REF = 'refs/pull/7/merge' + process.env.GITHUB_REPOSITORY = 'codecov/codecov-repo' + process.env.GITHUB_RUN_ID = '257701960' + process.env.GITHUB_SHA = '743b04806ea677403aa2ff26c6bdeb85005de658' + + expect(github_actions.configuration()).toEqual({ + branch: 'develop', + build: '257701960', + commit: '743b04806ea677403aa2ff26c6bdeb85005de658', + pr: '7', + service: 'github-actions', + slug: 'codecov/codecov-repo', + }) + }) +})