Skip to content

Commit

Permalink
feat: added support for list of labels and env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipe Forattini committed Jul 28, 2022
1 parent 7c231c7 commit 11533fb
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/actions/create-dockerfile/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .github/actions/create-dockerfile/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@actions/tool-cache": "^2.0.1",
"@vercel/ncc": "^0.34.0",
"linguist-js": "^2.5.2",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"qs": "^6.11.0"
}
}
46 changes: 33 additions & 13 deletions src/actions/create-dockerfile/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
const fs = require("fs");
const _ = require('lodash');
const qs = require("qs");
const _ = require("lodash");
const path = require("path");
const core = require("@actions/core");

const stubs = require('./stubs')
const stubs = require("./stubs");

const encode = (obj) => qs.stringify(obj, { arrayFormat: "brackets" });
const decode = (str) => qs.parse(str);

async function action() {
let preset = core.getInput('preset', { required: true })
let writeSummary = core.getBooleanInput('writeSummary', { required: true });
let preset = core.getInput("preset", { required: true });
let writeSummary = core.getBooleanInput("writeSummary", { required: true });

if (!stubs[preset]) core.error(new Error(`preset "${preset}" doesnt exists`))
if (!stubs[preset]) core.error(new Error(`preset "${preset}" doesnt exists`));

const { defaultValues, files = {}, stub } = stubs[preset]
const { defaultValues, files = {}, stub } = stubs[preset];

for (const filename of _.keys(files)) {
fs.writeFileSync(path.join(process.cwd(), filename), files[filename]);
}

const template = _.template(stub)
const content = template({
generatedAt: new Date().toISOString(),
...defaultValues,
});
const template = _.template(stub);

let data = _.merge(
defaultValues,
decode(
[
encode({
generatedAt: new Date().toISOString(),
...defaultValues,
}),
encode({
labels: [],
environmentVariables: [],
}),
].join("&")
)
);

data = _.mapValues(data, (v) => (!_.isArray(v) ? v : v.join(`\\\n\t`)));

const content = template(data);

fs.writeFileSync(path.join(process.cwd(), "Dockerfile"), content);

Expand All @@ -34,9 +54,9 @@ async function action() {
"<details><summary>Dockerfile:</summary>\n\n```dockerfile \n",
content,
" \n\n ``` \n</details>",
].join(''),
].join(""),
true
)
)
.write();
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/actions/create-dockerfile/stubs/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module.exports = {
# This file was autogenerated at <%= generatedAt %>.
FROM <%= image %>:<%= tag %>
LABEL <%= labels %>
<% labels.length && print("LABEL " + labels.join(' \\\n\t')) %>
ENV <%= environmentVariables %>
<% environmentVariables.length && print("ENV "+ environmentVariables.join(' \\\n\t')) %>
WORKDIR /svc
COPY . /svc
Expand All @@ -19,8 +19,8 @@ CMD ["<%= command %>"]
defaultValues: {
image: "node",
tag: "17-alpine",
labels: "build=auto",
environmentVariables: "OS=Alpine",
labels: ["builder=pipeline"],
environmentVariables: ["OS=Alpine"],
dependencyCommand: "npm install",
entrypoint: "npm",
command: "start",
Expand Down
55 changes: 55 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ brace-expansion@^1.1.7:
balanced-match "^1.0.0"
concat-map "0.0.1"

call-bind@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/call-bind/-/call-bind-1.0.2.tgz#b1d4e89e688119c3c9a903ad30abb2f6a919be3c"
integrity sha512-7O+FbCihrB5WGbFYesctwmTKae6rOiIzmz1icreWJ+0aA7LJfuqhEso2T9ncpcFtzMQtzXf2QGGueWJGTYsqrA==
dependencies:
function-bind "^1.1.1"
get-intrinsic "^1.0.2"

clone@2.x:
version "2.1.2"
resolved "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz"
Expand Down Expand Up @@ -429,6 +437,20 @@ fs.realpath@^1.0.0:
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==

function-bind@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d"
integrity sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==

get-intrinsic@^1.0.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/get-intrinsic/-/get-intrinsic-1.1.2.tgz#336975123e05ad0b7ba41f152ee4aadbea6cf598"
integrity sha512-Jfm3OyCxHh9DJyc28qGk+JmfkpO41A4XkneDSujN9MDXrm4oDKdHvndhZ2dN94+ERNfkYJWDclW6k2L/ZGHjXA==
dependencies:
function-bind "^1.1.1"
has "^1.0.3"
has-symbols "^1.0.3"

glob@^7.1.3:
version "7.2.3"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b"
Expand All @@ -441,6 +463,18 @@ glob@^7.1.3:
once "^1.3.0"
path-is-absolute "^1.0.0"

has-symbols@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/has-symbols/-/has-symbols-1.0.3.tgz#bb7b2c4349251dce87b125f7bdf874aa7c8b39f8"
integrity sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==

has@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/has/-/has-1.0.3.tgz#722d7cbfc1f6aa8241f16dd814e011e1f41e8796"
integrity sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==
dependencies:
function-bind "^1.1.1"

ignore@^5.2.0:
version "5.2.0"
resolved "https://registry.npmjs.org/ignore/-/ignore-5.2.0.tgz"
Expand Down Expand Up @@ -533,6 +567,11 @@ node-fetch@2.6.7, node-fetch@^2.6.7:
dependencies:
whatwg-url "^5.0.0"

object-inspect@^1.9.0:
version "1.12.2"
resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.12.2.tgz#c0641f26394532f28ab8d796ab954e43c009a8ea"
integrity sha512-z+cPxW0QGUp0mcqcsgQyLVRDoXFQbXOwBaqyF7VIgI4TWNQsDHrBpUQslRmIfAoYWdYzs6UlKJtB2XJpTaNSpQ==

once@^1.3.0, once@^1.4.0:
version "1.4.0"
resolved "https://registry.npmjs.org/once/-/once-1.4.0.tgz"
Expand Down Expand Up @@ -560,6 +599,13 @@ punycode@^2.1.1:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==

qs@^6.11.0:
version "6.11.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.11.0.tgz#fd0d963446f7a65e1367e01abd85429453f0c37a"
integrity sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==
dependencies:
side-channel "^1.0.4"

rimraf@^3.0.0:
version "3.0.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a"
Expand All @@ -577,6 +623,15 @@ semver@^6.1.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

side-channel@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/side-channel/-/side-channel-1.0.4.tgz#efce5c8fdc104ee751b25c58d4290011fa5ea2cf"
integrity sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==
dependencies:
call-bind "^1.0.0"
get-intrinsic "^1.0.2"
object-inspect "^1.9.0"

tmp-promise@^3.0.2:
version "3.0.3"
resolved "https://registry.yarnpkg.com/tmp-promise/-/tmp-promise-3.0.3.tgz#60a1a1cc98c988674fcbfd23b6e3367bdeac4ce7"
Expand Down

0 comments on commit 11533fb

Please sign in to comment.