kit-image-deployer
Advanced tools
Comparing version 3.2.0 to 3.2.1
{ | ||
"name": "kit-image-deployer", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"description": "A service that can be used to update given yaml files within a git repository with a new docker image path.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -109,3 +109,2 @@ <p align="center"> | ||
| `IMAGE` | The full path and tag to the docker image (must provide this or `CI_COMMIT_ID`) | no | *empty* | | ||
| `RETRIES` | Specify the number of times you want to automatically retry the commit if it fails | yes | `10` | | ||
@@ -112,0 +111,0 @@ ## Contributing |
@@ -12,3 +12,2 @@ "use strict"; | ||
this.options = _.merge({ | ||
retries: 10, | ||
docker: { | ||
@@ -44,136 +43,94 @@ registry: undefined, | ||
getImageFile(config, branch) { | ||
const self = this; | ||
const imageFilePath = path.join(config.images.path.replace(/^\//, ""), self.options.docker.repo, branch + ".yaml"); | ||
const property = config.images.property; | ||
deployImage(image, branch, committer, message, save) { | ||
var self = this; | ||
return new Promise(function(resolve, reject) { | ||
var imageFilePath, property; | ||
var configRequest = { | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: "kit.yaml" | ||
}; | ||
const imageRequest = { | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: imageFilePath | ||
}; | ||
self.github.getContent(configRequest) | ||
.then(function(configResponse) { | ||
var rawConfig = new Buffer(configResponse.content, configResponse.encoding).toString("ascii"); | ||
var config = yaml.safeLoad(rawConfig); | ||
imageFilePath = path.join(config.images.path.replace(/^\//, ""), self.options.docker.repo, branch + ".yaml"); | ||
property = config.images.property; | ||
return self.github | ||
.getContent(imageRequest) | ||
.then((imageResponse) => { | ||
// get | ||
const rawFile = new Buffer(imageResponse.content, imageResponse.encoding).toString("ascii"); | ||
const file = yaml.safeLoad(rawFile); | ||
var imageRequest = { | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: imageFilePath | ||
}; | ||
return self.github.getContent(imageRequest) | ||
.then(function(imageResponse) { | ||
// get | ||
var rawFile = new Buffer(imageResponse.content, imageResponse.encoding).toString("ascii"); | ||
var file = yaml.safeLoad(rawFile); | ||
// update specified property in file | ||
if (!_.isObject(file)) { | ||
throw new Error("Only support updating yaml type files"); | ||
} | ||
if (!property) { | ||
throw new Error("Require configuration missing: 'property'"); | ||
} | ||
return { | ||
path: imageFilePath, | ||
property: property, | ||
file: file, | ||
sha: imageResponse.sha | ||
} | ||
}) | ||
} | ||
// update specified property in file | ||
if (!_.isObject(file)) { | ||
throw new Error("Only support updating yaml type files"); | ||
} | ||
if (!property) { | ||
throw new Error("Require configuration missing: 'property'"); | ||
} | ||
attemptCommit(config, image, branch, committer, message, save) { | ||
return new Promise((resolve, reject) => { | ||
const self = this; | ||
var tries = 0; | ||
function attempt(lastError) { | ||
tries++; | ||
if (tries > self.options.retries) { | ||
return reject(lastError); | ||
} | ||
// only save if the image has changed | ||
if (file[property] != image) { | ||
var commitMsg = "committed " + property + ": '" + image + "' to " + imageFilePath; | ||
self | ||
.getImageFile(config, branch) | ||
.then((imageFile) => { | ||
// only save if the image has changed | ||
if (imageFile.file[imageFile.property] !== image) { | ||
const commitMsg = "committed " + imageFile.property + ": '" + image + "' to " + imageFile.path; | ||
// only continue saving if commit is enabled | ||
if (!save) { | ||
return "Commit disabled, but would have " + commitMsg; | ||
} | ||
imageFile.file[imageFile.property] = image; | ||
const updatedFile = new Buffer(yaml.safeDump(imageFile.file)).toString("base64"); | ||
file[property] = image; | ||
var updatedFile = new Buffer(yaml.safeDump(file)).toString("base64"); | ||
// only continue saving if commit is enabled | ||
if (!save) { | ||
return "Commit disabled, but would have " + commitMsg; | ||
return self.github.updateFile({ | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: imageFilePath, | ||
message: message, | ||
content: updatedFile, | ||
committer: committer, | ||
sha: imageResponse.sha | ||
}).then(function() { | ||
return "Successfully " + commitMsg; | ||
}); | ||
} else { | ||
return "No changes found"; | ||
} | ||
}) | ||
.catch(function(err) { | ||
if (err && err.code === 404) { | ||
// create file if it does not exist yet | ||
var newFile = {}; | ||
newFile[property] = image; | ||
var createdFile = new Buffer(yaml.safeDump(newFile)).toString("base64"); | ||
var commitMsg = "committed " + property + ": '" + image + "' to " + imageFilePath; | ||
self.github | ||
.updateFile({ | ||
// only continue saving if commit is enabled | ||
if (!save) { | ||
return "Commit disabled, but would have " + commitMsg; | ||
} | ||
return self.github.createFile({ | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: imageFile.path, | ||
path: imageFilePath, | ||
message: message, | ||
content: updatedFile, | ||
committer: committer, | ||
sha: imageFile.sha | ||
content: createdFile, | ||
committer: committer | ||
}) | ||
.then(function() { | ||
resolve("Successfully " + commitMsg); | ||
}) | ||
.catch((err) => { | ||
if (err && err.code && err.code === 404) { | ||
// create file if it does not exist yet | ||
var newFile = {}; | ||
newFile[property] = image; | ||
var createdFile = new Buffer(yaml.safeDump(newFile)).toString("base64"); | ||
var commitMsg = "committed " + imageFile.property + ": '" + image + "' to " + imageFile.path; | ||
// only continue saving if commit is enabled | ||
if (!save) { | ||
return "Commit disabled, but would have " + commitMsg; | ||
} | ||
return self.github.createFile({ | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: imageFile.path, | ||
message: message, | ||
content: createdFile, | ||
committer: committer | ||
}) | ||
.then(() => { | ||
return "Successfully " + commitMsg; | ||
}) | ||
.catch(() => { | ||
// try again | ||
attempt(err); | ||
}); | ||
} else { | ||
// try again | ||
attempt(err); | ||
} | ||
return "Successfully " + commitMsg; | ||
}); | ||
} else { | ||
resolve("No changes found for " + imageFile.property + ": '" + image + "' to " + imageFile.path); | ||
} | ||
}) | ||
.catch((err) => { | ||
// try again | ||
attempt(err); | ||
}); | ||
} | ||
// start trying! | ||
attempt(); | ||
}); | ||
} | ||
deployImage(image, branch, committer, message, save) { | ||
const self = this; | ||
return new Promise(function(resolve, reject) { | ||
const configRequest = { | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: "kit.yaml" | ||
}; | ||
self.github.getContent(configRequest) | ||
.then(function(configResponse) { | ||
const rawConfig = new Buffer(configResponse.content, configResponse.encoding).toString("ascii"); | ||
const config = yaml.safeLoad(rawConfig); | ||
return self.attemptCommit(config, image, branch, committer, message, save); | ||
} else { | ||
// if it's not a `404` error, then throw again | ||
throw err; | ||
} | ||
}); | ||
}) | ||
@@ -180,0 +137,0 @@ .then(resolve) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13541
132
117