From 7b87e8dd4665d3b667e0cf1633241a66e3abd396 Mon Sep 17 00:00:00 2001 From: Jean Carlos Taveras Date: Thu, 28 May 2020 11:42:58 -0400 Subject: [PATCH] Update Heroku Authenticaiton Process (#5) --- README.md | 17 ++--- action.yml | 6 +- dist/index.js | 172 +++++++++++++++++++++++++++----------------------- index.js | 68 ++++++++++++-------- package.json | 2 +- 5 files changed, 147 insertions(+), 118 deletions(-) diff --git a/README.md b/README.md index 085f64c..5a9a2ae 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,14 @@ jobs: email: ${{ secrets.HEROKU_EMAIL }} # your heroku email api_key: ${{ secrets.HEROKU_API_KEY }} # your heroku api key app_name: ${{ secrets.HEROKU_APP_NAME }} # you aplication name - dockerfile: '' # set the path to the folder wher the Dokerfile is located + dockerfile_path: '' # set the path to the folder wher the Dokerfile is located + options: '' # Docker Build Options ``` -| Variables | Required | -|:----------:|:-------------:| -| email | ✅| -| api_key | ✅| -| app_name | ✅| -| dockerfile | ✅| -| options | ❌| \ No newline at end of file +| Variables | Description | Required | +|:----------------:|:-----------------------------:|:-------------:| +| email | Heroku Email Account | ✅ | +| api_key | Heroku API Key | ✅ | +| app_name | Heroku App Name | ✅ | +| dockerfile_path | Path where your Docker File | ✅ | +| options | Docker Build Options | ❌ | diff --git a/action.yml b/action.yml index 91f84fe..6f1e599 100644 --- a/action.yml +++ b/action.yml @@ -2,8 +2,8 @@ name: 'Deploy Docker Image to Heroku App' author: 'Jean Carlos Taveras' description: 'A simple action to build, push and deploy a Docker Image to your Heroku app.' inputs: - email: - description: 'Email linked to your Heroku account' + email: + description: 'Email Linked to your Heroku Account' required: true api_key: description: 'Your Heroku API Key' @@ -11,7 +11,7 @@ inputs: app_name: description: 'Your Heroku App Name' required: true - dockerfile: + dockerfile_path: description: 'Dokerfile path' required: true options: diff --git a/dist/index.js b/dist/index.js index 907f987..fbef0b8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -40,7 +40,7 @@ module.exports = /******/ // the startup function /******/ function startup() { /******/ // Load entry module and return exports -/******/ return __webpack_require__(713); +/******/ return __webpack_require__(738); /******/ }; /******/ /******/ // run startup @@ -49,7 +49,21 @@ module.exports = /************************************************************************/ /******/ ({ -/***/ 74: +/***/ 87: +/***/ (function(module) { + +module.exports = require("os"); + +/***/ }), + +/***/ 129: +/***/ (function(module) { + +module.exports = require("child_process"); + +/***/ }), + +/***/ 620: /***/ (function(__unusedmodule, exports, __webpack_require__) { "use strict"; @@ -134,21 +148,92 @@ function escapeProperty(s) { /***/ }), -/***/ 87: +/***/ 622: /***/ (function(module) { -module.exports = require("os"); +module.exports = require("path"); /***/ }), -/***/ 129: +/***/ 669: /***/ (function(module) { -module.exports = require("child_process"); +module.exports = require("util"); /***/ }), -/***/ 298: +/***/ 738: +/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) { + +const core = __webpack_require__(976); +const { promisify } = __webpack_require__(669); + +const exec = promisify(__webpack_require__(129).exec); + +async function loginHeroku() { + const login = core.getInput('email'); + const password = core.getInput('api_key'); + + try { + await exec(`echo ${password} | docker login --username=${login} registry.heroku.com --password-stdin`); + console.log('Logged in succefully ✅'); + } catch (error) { + core.setFailed(`Authentication process faild. Error: ${error.message}`); + } +} + +async function buildPushAndDeploy() { + const appName = core.getInput('app_name'); + const dockerFilePath = core.getInput('dockerfile_path'); + const buildOptions = core.getInput('options') || ''; + const herokuAction = herokuActionSetUp(appName); + + try { + await exec(`cd ${dockerFilePath}`); + + await exec(`docker build . --file Dockerfile ${buildOptions} --tag registry.heroku.com/${appName}/web`); + console.log('Image built 🛠'); + + await exec(herokuAction('push')); + console.log('Container pushed to Heroku Container Registry ⏫'); + + await exec(herokuAction('release')); + console.log('App Deployed successfully 🚀'); + } catch (error) { + core.setFailed(`Something went wrong building your image. Error: ${error.message}`); + } +} + +/** + * + * @param {string} appName - Heroku App Name + * @returns {function} + */ +function herokuActionSetUp(appName) { + /** + * @typedef {'push' | 'release'} Actions + * @param {Actions} action - Action to be performed + * @returns {string} + */ + return function herokuAction(action) { + const HEROKU_API_KEY = core.getInput('api_key'); + const exportKey = `HEROKU_API_KEY=${HEROKU_API_KEY}`; + + return `${exportKey} heroku container:${action} web --app ${appName}` + } +} + +loginHeroku() + .then(() => buildPushAndDeploy()) + .catch((error) => { + console.log({ message: error.message }); + core.setFailed(error.message); + }) + + +/***/ }), + +/***/ 976: /***/ (function(__unusedmodule, exports, __webpack_require__) { "use strict"; @@ -170,7 +255,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", { value: true }); -const command_1 = __webpack_require__(74); +const command_1 = __webpack_require__(620); const os = __importStar(__webpack_require__(87)); const path = __importStar(__webpack_require__(622)); /** @@ -362,77 +447,6 @@ function getState(name) { exports.getState = getState; //# sourceMappingURL=core.js.map -/***/ }), - -/***/ 622: -/***/ (function(module) { - -module.exports = require("path"); - -/***/ }), - -/***/ 669: -/***/ (function(module) { - -module.exports = require("util"); - -/***/ }), - -/***/ 713: -/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) { - -const core = __webpack_require__(298); -const { promisify } = __webpack_require__(669); - -const exec = promisify(__webpack_require__(129).exec); - -async function loginHeroku() { - const login = core.getInput('email'); - const password = core.getInput('api_key'); - - try { - await exec(`cat >~/.netrc <~/.netrc < buildPushAndDeploy()) + .catch((error) => { + console.log({ message: error.message }); + core.setFailed(error.message); + }) diff --git a/package.json b/package.json index 7f733a1..498a4b2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deploy-heroku", - "version": "1.2.1", + "version": "2.0.0", "description": "A simple action to deploy container to Heroku Container Registry", "main": "index.js", "scripts": {},