Skip to content

Commit

Permalink
Removed build only script and add support for build arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Pietro Dazzi committed Feb 16, 2020
1 parent 7e1f9c9 commit c85b5f0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The accepted inputs are:
| `tag` | String | `latest` | No | Tags (*comma separated*) to apply to the image |
| `imageName` | String | | Yes | Name of the image |
| `dockerFile` | String | `Dockerfile` | No | Name of the Dockerfile |
| `buildArg` | String | | No | Build arguments (*comma separated*) used to build the image |
| `publish` | Boolean | `false` | No | Indicate if the builded image should be published on Docker HUB |
| `platform` | String | `linux/amd64,linux/arm64,linux/arm/v7` | No | Platforms (*comma separated*) that should be used to build the image | |
| `dockerHubUser` | String | | Only if `publish` is true | User that will publish the image |
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ inputs:
dockerFile:
description: "Name of the Dockerfile"
default: "Dockerfile"
buildArg:
description: "Build arguments (comma separated) used to build the image"
default: "none"
publish:
description: "Indicate if the builded image should be published on Docker HUB"
default: "false"
Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ async function docker_buildx() {
const dockerFile = extractInput('dockerFile', false, 'Dockerfile');
const publish = extractInput('publish', false, 'false').toLowerCase() === 'true';
const platform = extractInput('platform', false, 'linux/amd64,linux/arm64,linux/arm/v7');
const buildArg = extractInput('buildArg', false, '');
const buildFunction = publish ? buildAndPublish : buildOnly;
await buildFunction(platform, imageName, imageTag, dockerFile);
await buildFunction(platform, imageName, imageTag, dockerFile, buildArg);
cleanMyself();
} catch (error) {
core.setFailed(error.message);
Expand Down Expand Up @@ -44,15 +45,15 @@ async function executeShellScript(scriptName, ...parameters) {
child_process.execSync(command, {stdio: 'inherit'});
}

async function buildAndPublish(platform, imageName, imageTag, dockerFile) {
async function buildAndPublish(platform, imageName, imageTag, dockerFile, buildArg) {
const dockerHubUser = extractInput('dockerHubUser', true);
const dockerHubPassword = extractInput('dockerHubPassword', true);
await executeShellScript('dockerhub_login', dockerHubUser, dockerHubPassword);
await executeShellScript('docker_build_push', platform, imageName, imageTag, dockerFile);
await executeShellScript('docker_build', platform, imageName, imageTag, dockerFile, true, buildArg);
}

async function buildOnly(platform, imageName, imageTag, dockerFile) {
await executeShellScript('docker_build', platform, imageName, imageTag, dockerFile);
async function buildOnly(platform, imageName, imageTag, dockerFile, buildArg) {
await executeShellScript('docker_build', platform, imageName, imageTag, dockerFile, false, buildArg);
}

function cloneMyself() {
Expand Down
19 changes: 16 additions & 3 deletions scripts/docker_build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#!/bin/sh -l
export DOCKER_CLI_EXPERIMENTAL=enabled
DOCKER_BUILDX_TAG_OPTS=
TAGS=
for TAG in $(echo "$3" | tr ',' ' ')
do
DOCKER_BUILDX_TAG_OPTS="$DOCKER_BUILDX_TAG_OPTS --tag $2:$TAG"
TAGS="$TAGS --tag $2:$TAG"
done
docker buildx build --platform $1 $DOCKER_BUILDX_TAG_OPTS -f $4 .

PUSH=
if [ $5 = true ];
then
PUSH="--push"
fi

BUILD_ARGS=
for ARG in $(echo "$6" | tr ',' ' ')
do
BUILD_ARGS="$BUILD_ARGS --build-arg $ARG"
done

docker buildx build --platform $1 $PUSH $TAGS $BUILD_ARGS -f $4 .
8 changes: 0 additions & 8 deletions scripts/docker_build_push.sh

This file was deleted.

0 comments on commit c85b5f0

Please sign in to comment.