Skip to content

Commit

Permalink
Update Heroku Authenticaiton Process (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Carlos Taveras authored May 28, 2020
1 parent 7367799 commit 7b87e8d
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 118 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | ❌|
| 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 | ❌ |
6 changes: 3 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ 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'
required: true
app_name:
description: 'Your Heroku App Name'
required: true
dockerfile:
dockerfile_path:
description: 'Dokerfile path'
required: true
options:
Expand Down
172 changes: 93 additions & 79 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Expand Down Expand Up @@ -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";
Expand All @@ -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));
/**
Expand Down Expand Up @@ -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 <<EOF
machine api.heroku.com
login ${login}
password ${password}
EOF`);
console.log('.netrc file create ✅');

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');
const buildOptions = core.getInput('options') || '';

try {
await exec(`(cd ${dockerFilePath}; docker build . --file Dockerfile ${buildOptions} --tag registry.heroku.com/${appName}/web)`);
console.log('Image built ✅');

await exec(`(cd ${dockerFilePath}; docker push registry.heroku.com/${appName}/web)`);
console.log('Container pushed to Heroku Container Registry ✅');

await exec(`(cd ${dockerFilePath}; heroku container:release web --app ${appName})`);
console.log('App Deployed successfully ✅');
} catch (error) {
core.setFailed(`Somthing went wrong building your image. Error: ${error.message}`);
}
}

try {
loginHeroku();
buildPushAndDeploy();
} catch (error) {
console.log({ message: error.message });
core.setFailed(error.message);
}


/***/ })

/******/ });
68 changes: 41 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,58 @@ async function loginHeroku() {
const login = core.getInput('email');
const password = core.getInput('api_key');

try {
await exec(`cat >~/.netrc <<EOF
machine api.heroku.com
login ${login}
password ${password}
EOF`);
console.log('.netrc file create ✅');

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}`);
}
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');
const dockerFilePath = core.getInput('dockerfile_path');
const buildOptions = core.getInput('options') || '';
const herokuAction = herokuActionSetUp(appName);

try {
await exec(`(cd ${dockerFilePath}; docker build . --file Dockerfile ${buildOptions} --tag registry.heroku.com/${appName}/web)`);
console.log('Image built ✅');
await exec(`cd ${dockerFilePath}`);

await exec(`docker build . --file Dockerfile ${buildOptions} --tag registry.heroku.com/${appName}/web`);
console.log('Image built 🛠');

await exec(`(cd ${dockerFilePath}; docker push registry.heroku.com/${appName}/web)`);
console.log('Container pushed to Heroku Container Registry ');
await exec(herokuAction('push'));
console.log('Container pushed to Heroku Container Registry ');

await exec(`(cd ${dockerFilePath}; heroku container:release web --app ${appName})`);
console.log('App Deployed successfully ');
await exec(herokuAction('release'));
console.log('App Deployed successfully 🚀');
} catch (error) {
core.setFailed(`Somthing went wrong building your image. Error: ${error.message}`);
core.setFailed(`Something went wrong building your image. Error: ${error.message}`);
}
}

try {
loginHeroku();
buildPushAndDeploy();
} catch (error) {
console.log({ message: error.message });
core.setFailed(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);
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {},
Expand Down

0 comments on commit 7b87e8d

Please sign in to comment.