Skip to content

Commit

Permalink
feat(dockerfile): added option to build images from dockerfiles
Browse files Browse the repository at this point in the history
Signed-off-by: Petu Eusebiu <peusebiu@cisco.com>
  • Loading branch information
eusebiu-constantin-petu-dbk committed Nov 13, 2023
1 parent 438bfcd commit a6c256a
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 19 deletions.
26 changes: 22 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
services:
registry:
image: registry:2
image: ghcr.io/project-zot/zot-linux-amd64:latest
ports:
- 5000:5000
name: Test stacker-build-push-action
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
with:
file: 'test/stacker_wo_subs.yaml'
tags: v1 latest
url: docker://localhost:5000/name/app
url: docker://localhost:5000/one/app
skip-tls: true

- name: Run stacker-build with push, tags and build-args
Expand All @@ -65,7 +65,7 @@ jobs:
SUB2=VAR2
SUB3=VAR3
tags: v1 latest
url: docker://localhost:5000/name/app
url: docker://localhost:5000/two/app
skip-tls: true

- name: Run stacker-build with push, tags, build-args and layer-type
Expand All @@ -77,6 +77,24 @@ jobs:
SUB2=VAR2
SUB3=VAR3
tags: v1 latest
url: docker://localhost:5000/name/app
url: docker://localhost:5000/three/app
layer-type: 'tar squashfs'
skip-tls: true

- name: Run stacker-build with a Dockerfile
uses: ./
with:
dockerfile: 'test/Dockerfile.yaml'
tags: v1 latest
url: docker://localhost:5000/four/app
layer-type: 'tar squashfs'
skip-tls: true

- name: Run stacker recursive-build
uses: ./
with:
dir: 'test/builds'
tags: v2
url: docker://localhost:5000/five/app
layer-type: 'tar squashfs'
skip-tls: true
36 changes: 33 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@ For more information about stacker tool see: https://github.com/project-stacker/

## Action Inputs

<a id="dockerfile-build-inputs"></a>
<a id="build-inputs"></a>

| Input Name | Type | Description | Default |
| ---------- | ---- |----------- | ------- |
| file | string | the yaml file to be built as an OCI image, example: [stacker.yaml](./test/stacker.yaml) | stacker.yaml
| file | string | path to yaml file to be built as an OCI image, example: [stacker.yaml](./test/stacker.yaml) | stacker.yaml
| dockerfile| string | path to dockerfile to be build as an OCI image | None
| cache-dir | string | stacker's cache directory | ./.stacker
| dir | string | directory under which to recursive search for stackerfiles to build, use either file or dir | None
| file-pattern| string | regex pattern to use when searching for stackerfile paths, used only with dir arg | \\/stacker.yaml$
| layer-type | list | output layer type (supported values: tar, squashfs), ca be both separated by whitespace | tar
| build-args | list | the list of build-time arguments (subtitutes) separated by newline, see [stacker.yaml doc](https://github.com/project-stacker/stacker/blob/master/doc/stacker_yaml.md) | None
| build-args | list | the list of build-time arguments (subtitutes) separated by newline FOO=bar format, see [stacker.yaml doc](https://github.com/project-stacker/stacker/blob/master/doc/stacker_yaml.md) | None
| build-args-file | string | path to yaml file where build-args are set 'FOO: bar' yaml format | None
| url | string | remote OCI registry + repo name eg: docker://ghcr.io/project-stacker/ | None
| tags | list | one or more tags to give the new image, separated by whitespace | None
| username | string | used to login to registry | None
| password | string | used to login to registry | None
| skip-tls | bool | used with unsecure (http) registries | false
| token | string | github token used to authenticate against a repository for Git context | ${{ github.token }}
| version | string | stacker version, see [stacker releases](https://github.com/project-stacker/stacker/releases) | latest



Expand Down Expand Up @@ -93,3 +96,30 @@ Build and push example to localhost:
The above action will build test/stacker.yaml and push it to
1. docker://localhost:5000/test:test


Build and push a Dockerfile:

```
- name: Run stacker-build with a Dockerfile
uses: ./
with:
dockerfile: 'test/Dockerfile.yaml'
tags: v1 latest
url: docker://localhost:5000/four/app
layer-type: 'tar squashfs'
skip-tls: true
```


Build recursively all stacker files under dir/ and push all:

```
- name: Run stacker recursive-build
uses: ./
with:
dir: 'test/builds'
tags: v2
url: docker://localhost:5000/five/app
layer-type: 'tar squashfs'
skip-tls: true
```
35 changes: 25 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function run(): Promise<void> {
}

// get stacker cli
const stackerPath = await io.which("stacker", true);
const stackerPath = await io.which(stackerBin, true);
const cli: StackerCLI = new StackerCLI(stackerPath);

// print stacker version
Expand All @@ -41,7 +41,10 @@ export async function run(): Promise<void> {
const cachedir = core.getInput("cache-dir");

// get stacker file path from input
const stackerfile = core.getInput("file");
var stackerfile = core.getInput("file");

// get dockerfile path from input if any
const dockerfile = core.getInput('dockerfile');

// get stacker dir to recursive search for stacker files
const stackerdir = core.getInput("dir");
Expand All @@ -52,21 +55,33 @@ export async function run(): Promise<void> {
// get build-args from input
const substitutes = utils.getInputList("build-args");

// get build-args file from input
var subfile = core.getInput("build-args-file");

// get layer-type from input
const layerTypes = utils.getSpaceSeparatedInput("layer-type");

await cli.build(stackerfile, cachedir, stackerdir, stackerfilePattern, layerTypes, substitutes);
if (dockerfile) {
let [cmdRes, convertRes] = await cli.convertDockerfile(dockerfile);

// get tags from input
const tags = utils.getSpaceSeparatedInput("tags");
if (convertRes && (await cmdRes).exitCode == 0) {
// update stackerfile, subfile values
stackerfile = convertRes.stackerfile
subfile = convertRes.subfile
}
}

const registryURL = core.getInput("url");
const username = core.getInput("username");
const password = core.getInput("password");
const skipTLS = core.getInput("skip-tls") === "true";
await cli.build(stackerfile, cachedir, stackerdir, stackerfilePattern, layerTypes, substitutes, subfile);

const registryURL = core.getInput("url");
if (registryURL) {
await cli.publish(stackerfile, cachedir, stackerdir, stackerfilePattern, layerTypes, substitutes,
// get tags from input
const tags = utils.getSpaceSeparatedInput("tags");
const username = core.getInput("username");
const password = core.getInput("password");
const skipTLS = core.getInput("skip-tls") === "true";

await cli.publish(stackerfile, cachedir, stackerdir, stackerfilePattern, layerTypes, substitutes, subfile,
registryURL, tags, username, password, skipTLS);
}
}
Expand Down
54 changes: 52 additions & 2 deletions src/stacker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export type CommandResult = {
error: string
};

export type ConvertResult = {
stackerfile: string
subfile: string
}

export class StackerCLI {
private readonly executable: string;
Expand All @@ -20,8 +24,44 @@ export class StackerCLI {
this.executable = executable;
}

async convertDockerfile(dockerfile: string): Promise<[Promise<CommandResult>, ConvertResult]> {
const args: string[] = ["--debug"];

const stackerfile = "stacker.yaml"
const subfile = "stacker-subs.yaml"

args.push("convert");
args.push("--docker-file");
args.push(dockerfile);

args.push("--output-file");
args.push(stackerfile);

args.push("--substitute-file");
args.push(subfile);

const res = this.execute(args).then((res) => {
if (res.exitCode == 0) {
core.info("printing resulting stacker.yaml after converting dockerfile");
exec.exec('/bin/bash -c "cat stacker.yaml | jq"', []);

core.info("printing resulting substitutes file after converting dockerfile");
exec.exec('/bin/bash -c "cat stacker-subs.yaml | jq"', []);
}

return res;
})

const cres : ConvertResult = {
stackerfile: stackerfile,
subfile: subfile,
}

return [res, cres];
}

async build(stackerfile: string, cachedir: string, stackerdir: string, stackerfilePattern: string,
layerType: string[], substitutes: string[]): Promise<CommandResult> {
layerType: string[], substitutes: string[], subfile: string): Promise<CommandResult> {
const args: string[] = ["--debug"];

args.push("--stacker-dir");
Expand All @@ -48,6 +88,11 @@ export class StackerCLI {
args.push(substitute);
})

if (subfile) {
args.push("--substitute-file");
args.push(subfile);
}

const res = this.execute(args).then((res) => {
if (res.exitCode == 0) {
core.info("printing oci layout index.json");
Expand All @@ -61,7 +106,7 @@ export class StackerCLI {
}

async publish(stackerfile: string, cachedir: string, stackerdir: string, stackerfilePattern: string, layerType: string[], substitutes: string[],
url: string, tags: string[], username: string, password: string, skipTLS: boolean): Promise<CommandResult> {
subfile: string, url: string, tags: string[], username: string, password: string, skipTLS: boolean): Promise<CommandResult> {
const args: string[] = ["--debug"];

args.push("--stacker-dir");
Expand All @@ -79,6 +124,11 @@ export class StackerCLI {
args.push(substitute);
});

if (subfile) {
args.push("--substitute-file");
args.push(subfile);
}

args.push("--url");
args.push(url);

Expand Down
25 changes: 25 additions & 0 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM alpine:3.16

VOLUME /out

ARG VERSION=1.0.0
LABEL AUTHOR="unknown"

ENV ENV_VERSION1 \$VERSION
ENV ENV_VERSION2=\$VERSION
ENV ENV_VERSION3=\$\{VERSION\}
ENV TEST_PATH="/usr/share/test/bin:$PATH" \
TEST_PATHS_CONFIG="/etc/test/test.ini" \
TEST_PATHS_DATA="/var/lib/test" \
TEST_PATHS_HOME="/usr/share/test" \
TEST_PATHS_LOGS="/var/log/test" \
TEST_PATHS_PLUGINS="/var/lib/test/plugins" \
TEST_PATHS_PROVISIONING="/etc/test/provisioning"
ENV COMMIT_SHA=${COMMIT_SHA}

RUN echo \$VERSION
RUN echo \$\{VERSION\}

RUN apk add --no-cache lua5.3 lua-filesystem lua-lyaml lua-http

ENTRYPOINT [ "/usr/local/bin/fetch-latest-releases.lua" ]

0 comments on commit a6c256a

Please sign in to comment.