Skip to content

Commit 71b2d37

Browse files
Allow Environment variables (#76)
* 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]>
1 parent 57eb95b commit 71b2d37

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
lines changed

.circleci/config.yml

+24-24
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ jobs:
7777
- run:
7878
name: Clone nodejs repo
7979
command: |
80-
git clone https://github.com/nodejs/node.git
80+
git clone https://github.com/nodejs/node.git
8181
- run:
8282
name: Checkout v14.16.1 tag and apply patches
8383
command: |
@@ -113,7 +113,7 @@ jobs:
113113
- run:
114114
name: Remove build dir and run tests
115115
command: |
116-
rm -rf node
116+
rm -rf node
117117
npm ci
118118
npm test
119119
mkdir -p coverage-alpine
@@ -137,7 +137,7 @@ jobs:
137137
docker:
138138
# specify the version you desire here
139139
- image: alpine:3.13.5
140-
steps:
140+
steps:
141141
- run:
142142
name: Install git
143143
command: |
@@ -150,15 +150,15 @@ jobs:
150150
- run:
151151
name: Run Alpine binary -f (dry run)
152152
command: |
153-
out/codecov-alpine -f /root/project/coverage-alpine/cobertura-coverage.xml -F alpine -d -Z >> output_alpine.txt
153+
out/codecov-alpine -f /root/project/coverage-alpine/cobertura-coverage.xml -F alpine -d -Z -e CIRCLE_BRANCH >> output_alpine.txt
154154
- run:
155155
name: Run Alpine binary auto-detect (dry run)
156156
command: |
157-
out/codecov-alpine -F alpine -d -Z >> output_alpine.txt
157+
out/codecov-alpine -F alpine -d -Z -e CIRCLE_BRANCH >> output_alpine.txt
158158
- run:
159159
name: Run Alpine binary (upload)
160160
command: |
161-
out/codecov-alpine -F alpine -Z
161+
out/codecov-alpine -F alpine -Z -e CIRCLE_BRANCH
162162
163163
- persist_to_workspace:
164164
root: .
@@ -197,15 +197,15 @@ jobs:
197197
- run:
198198
name: Run Linux binary -f (dry run)
199199
command: |
200-
out/codecov-linux -f /home/circleci/project/coverage/cobertura-coverage.xml -F linux -d -Z >> output_linux.txt
200+
out/codecov-linux -f /home/circleci/project/coverage/cobertura-coverage.xml -F linux -d -Z -e CIRCLE_BRANCH >> output_linux.txt
201201
- run:
202202
name: Run Linux binary auto-detect (dry run)
203203
command: |
204-
out/codecov-linux -F linux -d -Z >> output_linux.txt
204+
out/codecov-linux -F linux -d -Z -e CIRCLE_BRANCH >> output_linux.txt
205205
- run:
206206
name: Run Linux binary (upload)
207207
command: |
208-
out/codecov-linux -F linux -Z
208+
out/codecov-linux -F linux -Z -e CIRCLE_BRANCH
209209
210210
- persist_to_workspace:
211211
root: .
@@ -222,46 +222,46 @@ jobs:
222222
- run:
223223
name: Run Linux binary (dry run)
224224
command: |
225-
out/codecov-linux -F linux-without-git -d -Z > output_linux_without_git.txt
225+
out/codecov-linux -F linux-without-git -d -Z -e CIRCLE_BRANCH > output_linux_without_git.txt
226226
- run:
227227
name: Run Linux binary (upload)
228228
command: |
229-
out/codecov-linux -F linux-without-git -Z
229+
out/codecov-linux -F linux-without-git -Z -e CIRCLE_BRANCH
230230
231231
- persist_to_workspace:
232232
root: .
233233
paths:
234234
- output_linux.txt
235235

236236
test-macos:
237-
macos:
238-
xcode: "10.0.0"
239-
steps:
237+
macos:
238+
xcode: "10.0.0"
239+
steps:
240240
- checkout
241241
- attach_workspace:
242242
at: .
243243
- run:
244244
name: Run MacOS binary -f (dry-run)
245245
command: |
246-
out/codecov-macos -f /Users/distiller/project/coverage/cobertura-coverage.xml -F macos -d -Z >> output_osx.txt
246+
out/codecov-macos -f /Users/distiller/project/coverage/cobertura-coverage.xml -F macos -d -Z -e CIRCLE_BRANCH >> output_osx.txt
247247
- run:
248248
name: Run MacOS binary auto-detect (dry-run)
249249
command: |
250-
out/codecov-macos -F macos -d -Z >> output_osx.txt
250+
out/codecov-macos -F macos -d -Z -e CIRCLE_BRANCH >> output_osx.txt
251251
- run:
252252
name: Run MacOS binary (upload)
253253
command: |
254-
out/codecov-macos -F macos -Z
254+
out/codecov-macos -F macos -Z -e CIRCLE_BRANCH
255255
256256
- persist_to_workspace:
257257
root: .
258258
paths:
259259
- output_osx.txt
260260

261261
test-macos-without-git:
262-
macos:
263-
xcode: "10.0.0"
264-
steps:
262+
macos:
263+
xcode: "10.0.0"
264+
steps:
265265
- attach_workspace:
266266
at: .
267267
- run:
@@ -289,17 +289,17 @@ jobs:
289289
command: |
290290
dir C:\Users\circleci\project\coverage\
291291
attrib C:\Users\circleci\project\coverage\cobertura-coverage.xml
292-
.\out\codecov.exe -f C:\Users\circleci\project\coverage\cobertura-coverage.xml -F windows -d -Z >> output_win.txt
292+
.\out\codecov.exe -f C:\Users\circleci\project\coverage\cobertura-coverage.xml -F windows -d -Z -e CIRCLE_BRANCH >> output_win.txt
293293
shell: cmd.exe
294294
- run:
295295
name: Run Windows binary auto-detect (dry-run)
296296
command: |
297-
.\out\codecov.exe -F windows -d -Z >> output_win.txt
297+
.\out\codecov.exe -F windows -d -Z -e CIRCLE_BRANCH >> output_win.txt
298298
shell: cmd.exe
299299
- run:
300300
name: Run Windows binary (upload)
301301
command: |
302-
.\out\codecov.exe -F windows -Z
302+
.\out\codecov.exe -F windows -Z -e CIRCLE_BRANCH
303303
shell: cmd.exe
304304

305305
- persist_to_workspace:
@@ -356,7 +356,7 @@ jobs:
356356
- store_artifacts:
357357
path: output_win.txt
358358
- store_artifacts:
359-
path: output_win_without_git.txt
359+
path: output_win_without_git.txt
360360

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

bin/codecov

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ var argv = require("yargs") // eslint-disable-line
1313
alias: "B",
1414
description: "Specify the branch manually"
1515
},
16+
env: {
17+
alias: "e",
18+
description: "Specify environment variables to be included with this build.\nAlso accepting environment variables: CODECOV_ENV=VAR,VAR2"
19+
},
1620
sha: {
1721
alias: "C",
1822
description: "Specify the commit SHA mannually"

src/helpers/files.js

+5
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ function fileHeader (filePath) {
164164
return `# path=${filePath}\n`
165165
}
166166

167+
function endEnvironmentMarker () {
168+
return '<<<<<< ENV\n'
169+
}
170+
167171
function getFilePath (projectRoot, filePath) {
168172
if (filePath.startsWith('./') ||
169173
filePath.startsWith('/') ||
@@ -182,6 +186,7 @@ module.exports = {
182186
getFileListing,
183187
endFileMarker,
184188
endNetworkMarker,
189+
endEnvironmentMarker,
185190
fileHeader,
186191
fetchGitRoot,
187192
parseGitIgnore,

src/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function dryRun (uploadHost, token, query, uploadFile) {
1919
* @param {Object} args
2020
* @param {string} args.build Specify the build number manually
2121
* @param {string} args.branch Specify the branch manually
22+
* @param {string} args.env Specify environment variables to be included with this build
2223
* @param {string} args.sha Specify the commit SHA mannually
2324
* @param {string} args.file Target file(s) to upload
2425
* @param {string} args.flags Flag the upload to group coverage metrics
@@ -124,6 +125,18 @@ async function main (args) {
124125
uploadFile = uploadFile.concat(fileHelpers.endFileMarker())
125126
}
126127

128+
// Environment variables
129+
if (args.env || envs.CODECOV_ENV) {
130+
const environmentVars = args.env || envs.CODECOV_ENV
131+
const vars = environmentVars
132+
.split(',')
133+
.filter(Boolean)
134+
.map(evar => `${evar}=${process.env[evar] || ''}\n`)
135+
.join('')
136+
uploadFile = uploadFile.concat(vars)
137+
uploadFile = uploadFile.concat(fileHelpers.endEnvironmentMarker())
138+
}
139+
127140
const gzippedFile = zlib.gzipSync(uploadFile)
128141

129142
// == Step 6: determine CI provider

test/index.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe('Uploader Core', function () {
88

99
afterEach(() => {
1010
process.env = env
11+
jest.restoreAllMocks()
1112
})
1213

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

51+
it('Can parse environment variables', async function () {
52+
process.env.SOMETHING = 'red'
53+
process.env.ANOTHER = 'blue'
54+
jest.spyOn(process, 'exit').mockImplementation(() => {})
55+
const log = jest.spyOn(console, 'log')
56+
await app.main({
57+
name: 'customname',
58+
token: 'abcdefg',
59+
url: 'https://codecov.io',
60+
dryRun: true,
61+
env: 'SOMETHING,ANOTHER'
62+
})
63+
expect(log).toHaveBeenCalledWith(expect.stringMatching(/SOMETHING=red/))
64+
expect(log).toHaveBeenCalledWith(expect.stringMatching(/ANOTHER=blue/))
65+
expect(log).toHaveBeenCalledWith(expect.stringMatching(/<<<<<< ENV/))
66+
})
67+
5068
describe('Flags', () => {
5169
it('can upload with flags', async () => {
5270
process.env.CI = 'true'

0 commit comments

Comments
 (0)