Skip to content

Commit 5fabdf2

Browse files
Added unit test cases
1 parent 74d9a7f commit 5fabdf2

File tree

7 files changed

+386
-265
lines changed

7 files changed

+386
-265
lines changed

.eslintrc.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"env": {
3+
"commonjs": true,
4+
"es6": true,
5+
"node": true
6+
},
7+
"extends": "eslint:recommended",
8+
"globals": {
9+
"Atomics": "readonly",
10+
"SharedArrayBuffer": "readonly"
11+
},
12+
"parserOptions": {
13+
"ecmaVersion": 2018
14+
},
15+
"rules": {
16+
}
17+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
email: ${{ secrets.HEROKU_EMAIL }} # your heroku email
2222
api_key: ${{ secrets.HEROKU_API_KEY }} # your heroku api key
2323
docker_compose_file: './src/docker-compose.heroku.yml' # set the path to the folder where the docker-compose file is located
24-
heroku_apps: '[{"imagename":"App1","appname":"App1","apptype":"web"},{"imagename":"App2","appname":"App2","apptype":"web"},{"imagename":"App3","appname":"App2","apptype":"worker"}]' # List of Docker Image name, Heroku app and Heroku app type
24+
heroku_apps: '[{"imagename":"app1","appname":"app1","apptype":"web"},{"imagename":"app2","appname":"app2","apptype":"web"},{"imagename":"app3","appname":"app2","apptype":"worker"}]' # List of Docker Image name, Heroku app and Heroku app type
2525
```
2626
2727
| Variables | Required |

app.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const core = require('@actions/core');
2+
const { promisify } = require('util');
3+
4+
const exec = promisify(require('child_process').exec);
5+
6+
const asyncForEach = async (array, callback) => {
7+
for (let index = 0; index < array.length; index++) {
8+
await callback(array[index], index, array)
9+
}
10+
}
11+
12+
let loginToHeroku = async function loginToHeroku(login, password) {
13+
try {
14+
15+
await exec(`cat >~/.netrc <<EOF
16+
machine api.heroku.com
17+
login ${login}
18+
password ${password}
19+
EOF`);
20+
21+
console.log('.netrc file create ✅');
22+
23+
await exec(`echo ${password} | docker login --username=${login} registry.heroku.com --password-stdin`);
24+
25+
console.log('Logged in succefully ✅');
26+
}
27+
catch (error) {
28+
core.setFailed(`Authentication process faild. Error: ${error.message}`);
29+
}
30+
}
31+
32+
let getImageAppNameList = async function getImageAppNameList(heroku_apps) {
33+
try {
34+
return JSON.parse(heroku_apps);
35+
}
36+
catch (error) {
37+
core.setFailed(`Invalid input for heroku app. Error: ${error.message}`);
38+
}
39+
}
40+
41+
let buildDockerCompose = async function buildDockerCompose(dockerComposeFilePath) {
42+
try {
43+
console.log('docker image build started.');
44+
await exec(`docker-compose -f ${dockerComposeFilePath} build`);
45+
console.log('docker image build finished.');
46+
}
47+
catch (error) {
48+
core.setFailed(`Somthing went wrong building your image. Error: ${error.message}`);
49+
}
50+
}
51+
52+
let pushAndDeployAllImages = async function pushAndDeployAllImages(imageList) {
53+
try {
54+
if (imageList.length > 0) {
55+
await asyncForEach(imageList, async (item) => {
56+
console.log('Processing image -' + item.imagename);
57+
await exec(`docker tag ${item.imagename} registry.heroku.com/${item.appname}/${item.apptype}`);
58+
console.log('Container tagged for image - ' + item.imagename);
59+
await exec(`docker push registry.heroku.com/${item.appname}/web`);
60+
console.log('Container pushed for image - ' + item.imagename);
61+
await exec(`heroku container:release ${item.apptype} --app ${item.appname}`);
62+
console.log('Container deployed for image - ' + item.imagename);
63+
});
64+
console.log('App Deployed successfully ✅');
65+
} else {
66+
core.setFailed(`No image given to process.`);
67+
}
68+
}
69+
catch (error) {
70+
core.setFailed(`Somthing went wrong while pushing and deploying your image. Error: ${error.message}`);
71+
}
72+
}
73+
74+
let buildAndDeploy = async function buildAndDeploy(login, password, dockerComposeFilePath, imageListString)
75+
{
76+
await loginToHeroku(login, password);
77+
await buildDockerCompose(dockerComposeFilePath);
78+
const imageList = await getImageAppNameList(imageListString);
79+
await pushAndDeployAllImages(imageList);
80+
}
81+
82+
module.exports.loginToHeroku = loginToHeroku;
83+
module.exports.getImageAppNameList = getImageAppNameList;
84+
module.exports.buildDockerCompose = buildDockerCompose;
85+
module.exports.pushAndDeployAllImages = pushAndDeployAllImages;
86+
module.exports.buildAndDeploy = buildAndDeploy;

0 commit comments

Comments
 (0)