Skip to content

Commit

Permalink
Allow Environment variables (#76)
Browse files Browse the repository at this point in the history
* Custom defined name of the upload

* Allow environment variables to be sent through

* add environment variable to circleci yml

Co-authored-by: Joe Becher <[email protected]>
  • Loading branch information
eddiemoore and drazisil-codecov authored May 10, 2021
1 parent 57eb95b commit 71b2d37
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
48 changes: 24 additions & 24 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
- run:
name: Clone nodejs repo
command: |
git clone https://github.com/nodejs/node.git
git clone https://github.com/nodejs/node.git
- run:
name: Checkout v14.16.1 tag and apply patches
command: |
Expand Down Expand Up @@ -113,7 +113,7 @@ jobs:
- run:
name: Remove build dir and run tests
command: |
rm -rf node
rm -rf node
npm ci
npm test
mkdir -p coverage-alpine
Expand All @@ -137,7 +137,7 @@ jobs:
docker:
# specify the version you desire here
- image: alpine:3.13.5
steps:
steps:
- run:
name: Install git
command: |
Expand All @@ -150,15 +150,15 @@ jobs:
- run:
name: Run Alpine binary -f (dry run)
command: |
out/codecov-alpine -f /root/project/coverage-alpine/cobertura-coverage.xml -F alpine -d -Z >> output_alpine.txt
out/codecov-alpine -f /root/project/coverage-alpine/cobertura-coverage.xml -F alpine -d -Z -e CIRCLE_BRANCH >> output_alpine.txt
- run:
name: Run Alpine binary auto-detect (dry run)
command: |
out/codecov-alpine -F alpine -d -Z >> output_alpine.txt
out/codecov-alpine -F alpine -d -Z -e CIRCLE_BRANCH >> output_alpine.txt
- run:
name: Run Alpine binary (upload)
command: |
out/codecov-alpine -F alpine -Z
out/codecov-alpine -F alpine -Z -e CIRCLE_BRANCH
- persist_to_workspace:
root: .
Expand Down Expand Up @@ -197,15 +197,15 @@ jobs:
- run:
name: Run Linux binary -f (dry run)
command: |
out/codecov-linux -f /home/circleci/project/coverage/cobertura-coverage.xml -F linux -d -Z >> output_linux.txt
out/codecov-linux -f /home/circleci/project/coverage/cobertura-coverage.xml -F linux -d -Z -e CIRCLE_BRANCH >> output_linux.txt
- run:
name: Run Linux binary auto-detect (dry run)
command: |
out/codecov-linux -F linux -d -Z >> output_linux.txt
out/codecov-linux -F linux -d -Z -e CIRCLE_BRANCH >> output_linux.txt
- run:
name: Run Linux binary (upload)
command: |
out/codecov-linux -F linux -Z
out/codecov-linux -F linux -Z -e CIRCLE_BRANCH
- persist_to_workspace:
root: .
Expand All @@ -222,46 +222,46 @@ jobs:
- run:
name: Run Linux binary (dry run)
command: |
out/codecov-linux -F linux-without-git -d -Z > output_linux_without_git.txt
out/codecov-linux -F linux-without-git -d -Z -e CIRCLE_BRANCH > output_linux_without_git.txt
- run:
name: Run Linux binary (upload)
command: |
out/codecov-linux -F linux-without-git -Z
out/codecov-linux -F linux-without-git -Z -e CIRCLE_BRANCH
- persist_to_workspace:
root: .
paths:
- output_linux.txt

test-macos:
macos:
xcode: "10.0.0"
steps:
macos:
xcode: "10.0.0"
steps:
- checkout
- attach_workspace:
at: .
- run:
name: Run MacOS binary -f (dry-run)
command: |
out/codecov-macos -f /Users/distiller/project/coverage/cobertura-coverage.xml -F macos -d -Z >> output_osx.txt
out/codecov-macos -f /Users/distiller/project/coverage/cobertura-coverage.xml -F macos -d -Z -e CIRCLE_BRANCH >> output_osx.txt
- run:
name: Run MacOS binary auto-detect (dry-run)
command: |
out/codecov-macos -F macos -d -Z >> output_osx.txt
out/codecov-macos -F macos -d -Z -e CIRCLE_BRANCH >> output_osx.txt
- run:
name: Run MacOS binary (upload)
command: |
out/codecov-macos -F macos -Z
out/codecov-macos -F macos -Z -e CIRCLE_BRANCH
- persist_to_workspace:
root: .
paths:
- output_osx.txt

test-macos-without-git:
macos:
xcode: "10.0.0"
steps:
macos:
xcode: "10.0.0"
steps:
- attach_workspace:
at: .
- run:
Expand Down Expand Up @@ -289,17 +289,17 @@ jobs:
command: |
dir C:\Users\circleci\project\coverage\
attrib C:\Users\circleci\project\coverage\cobertura-coverage.xml
.\out\codecov.exe -f C:\Users\circleci\project\coverage\cobertura-coverage.xml -F windows -d -Z >> output_win.txt
.\out\codecov.exe -f C:\Users\circleci\project\coverage\cobertura-coverage.xml -F windows -d -Z -e CIRCLE_BRANCH >> output_win.txt
shell: cmd.exe
- run:
name: Run Windows binary auto-detect (dry-run)
command: |
.\out\codecov.exe -F windows -d -Z >> output_win.txt
.\out\codecov.exe -F windows -d -Z -e CIRCLE_BRANCH >> output_win.txt
shell: cmd.exe
- run:
name: Run Windows binary (upload)
command: |
.\out\codecov.exe -F windows -Z
.\out\codecov.exe -F windows -Z -e CIRCLE_BRANCH
shell: cmd.exe

- persist_to_workspace:
Expand Down Expand Up @@ -356,7 +356,7 @@ jobs:
- store_artifacts:
path: output_win.txt
- store_artifacts:
path: output_win_without_git.txt
path: output_win_without_git.txt

# TODO: add test runs for each binary outside of a git project

Expand Down
4 changes: 4 additions & 0 deletions bin/codecov
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ var argv = require("yargs") // eslint-disable-line
alias: "B",
description: "Specify the branch manually"
},
env: {
alias: "e",
description: "Specify environment variables to be included with this build.\nAlso accepting environment variables: CODECOV_ENV=VAR,VAR2"
},
sha: {
alias: "C",
description: "Specify the commit SHA mannually"
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ function fileHeader (filePath) {
return `# path=${filePath}\n`
}

function endEnvironmentMarker () {
return '<<<<<< ENV\n'
}

function getFilePath (projectRoot, filePath) {
if (filePath.startsWith('./') ||
filePath.startsWith('/') ||
Expand All @@ -182,6 +186,7 @@ module.exports = {
getFileListing,
endFileMarker,
endNetworkMarker,
endEnvironmentMarker,
fileHeader,
fetchGitRoot,
parseGitIgnore,
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function dryRun (uploadHost, token, query, uploadFile) {
* @param {Object} args
* @param {string} args.build Specify the build number manually
* @param {string} args.branch Specify the branch manually
* @param {string} args.env Specify environment variables to be included with this build
* @param {string} args.sha Specify the commit SHA mannually
* @param {string} args.file Target file(s) to upload
* @param {string} args.flags Flag the upload to group coverage metrics
Expand Down Expand Up @@ -124,6 +125,18 @@ async function main (args) {
uploadFile = uploadFile.concat(fileHelpers.endFileMarker())
}

// Environment variables
if (args.env || envs.CODECOV_ENV) {
const environmentVars = args.env || envs.CODECOV_ENV
const vars = environmentVars
.split(',')
.filter(Boolean)
.map(evar => `${evar}=${process.env[evar] || ''}\n`)
.join('')
uploadFile = uploadFile.concat(vars)
uploadFile = uploadFile.concat(fileHelpers.endEnvironmentMarker())
}

const gzippedFile = zlib.gzipSync(uploadFile)

// == Step 6: determine CI provider
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('Uploader Core', function () {

afterEach(() => {
process.env = env
jest.restoreAllMocks()
})

it('Can return version', function () {
Expand Down Expand Up @@ -47,6 +48,23 @@ describe('Uploader Core', function () {
expect(result).toEqual({ status: 'success', resultURL: 'https://results.codecov.io' })
}, 30000)

it('Can parse environment variables', async function () {
process.env.SOMETHING = 'red'
process.env.ANOTHER = 'blue'
jest.spyOn(process, 'exit').mockImplementation(() => {})
const log = jest.spyOn(console, 'log')
await app.main({
name: 'customname',
token: 'abcdefg',
url: 'https://codecov.io',
dryRun: true,
env: 'SOMETHING,ANOTHER'
})
expect(log).toHaveBeenCalledWith(expect.stringMatching(/SOMETHING=red/))
expect(log).toHaveBeenCalledWith(expect.stringMatching(/ANOTHER=blue/))
expect(log).toHaveBeenCalledWith(expect.stringMatching(/<<<<<< ENV/))
})

describe('Flags', () => {
it('can upload with flags', async () => {
process.env.CI = 'true'
Expand Down

0 comments on commit 71b2d37

Please sign in to comment.