push-to-repo
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -15,3 +15,3 @@ #!/usr/bin/env node | ||
let FILE_NAME = '' | ||
let COMMIT_MESSAGE = `Update ${FILE_NAME} [skip ci]` | ||
let COMMIT_MESSAGE = '' | ||
let EXIT_CODE = 0 | ||
@@ -70,3 +70,5 @@ let DEBUG = false | ||
function exit(reason, error = undefined) { | ||
console.error(reason) | ||
if (reason) { | ||
console.error(reason) | ||
} | ||
if (error) { | ||
@@ -79,18 +81,26 @@ console.error(error) | ||
async function pushFile() { | ||
process.stdout.write(`Pushing updates to ${FILE_NAME}.. \n`) | ||
if (DEBUG) { | ||
process.stdout.write('DEBUG mode: ') | ||
} | ||
process.stdout.write(`Pushing ${FILE_NAME} updates to ${BRANCH}.. `) | ||
try { | ||
const filePath = `${CI_VARS.projectDir}/${FILE_NAME}` | ||
debug('filePath: ', filePath) | ||
const fileContents = await fs.readFile(filePath, 'utf8') | ||
await api.put( | ||
`/projects/${CI_VARS.projectId}/repository/files/${FILE_NAME}`, | ||
{ | ||
branch: BRANCH, | ||
content: fileContents, | ||
commit_message: COMMIT_MESSAGE, | ||
} | ||
) | ||
const url = `/projects/${CI_VARS.projectId}/repository/files/${FILE_NAME}` | ||
const payload = { | ||
branch: BRANCH, | ||
content: fileContents, | ||
commit_message: COMMIT_MESSAGE, | ||
} | ||
debug('payload: ', payload) | ||
await api.put(url, payload) | ||
process.stdout.write('Done.\n') | ||
} catch (error) { | ||
if (error.response && error.response.status === 400) { | ||
exit('Push failed with status code 400. File not modified?') | ||
debug('Error pushing: ', error) | ||
process.stdout.write( | ||
'Push failed with status code 400. File not modified?\n' | ||
) | ||
exit() | ||
} else { | ||
@@ -107,10 +117,11 @@ exit('Push failed', error) | ||
if (program.branch !== BRANCH) { | ||
if (program.branch !== 'CI_COMMIT_BRANCH') { | ||
BRANCH = program.branch | ||
} else { | ||
BRANCH = CI_VARS.branch | ||
} | ||
if (program.branch !== BRANCH) { | ||
BRANCH = program.branch | ||
} | ||
if (program.message === 'Update <filename> [skip ci]') { | ||
if (program.message !== 'Update <filename> [skip ci]') { | ||
COMMIT_MESSAGE = program.message | ||
} else { | ||
COMMIT_MESSAGE = `Update ${FILE_NAME} [skip ci]` | ||
} | ||
@@ -117,0 +128,0 @@ if (program.failOnError) { |
# Changelog | ||
## v0.0.2 - 2020-12-16 | ||
- Bugfixes. | ||
## v0.0.1 - 2020-12-16 | ||
- Initial version. |
{ | ||
"name": "push-to-repo", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Commit changes back to the repo from a GitLab CI job.", | ||
@@ -5,0 +5,0 @@ "main": "bin/index.js", |
6280
119