start conversion to javascript action

This commit is contained in:
David Hiendl 2023-12-02 19:30:23 +01:00
parent 23f5686850
commit dd9e7a55de
6 changed files with 17999 additions and 158 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
**/.idea/
node_modules/

View File

@ -2,7 +2,7 @@ name: 'Docker'
description: 'Build and publish docker images'
inputs:
docker-image:
docker_image:
description: docker client image to use for building images
default: "docker:latest"
@ -99,162 +99,7 @@ inputs:
outputs:
published_tags:
description: "Published tags as csv"
value: ""
runs:
using: "composite"
steps:
- name: "dummy step"
shell: bash
run: echo dummy step changed
- id: information
uses: https://gitea.dhswt.de/actions/information@master
- name: add ci registry to targets
shell: bash
if: inputs.add_ci_registry_target == 'true'
run: |
echo "steps.information.outputs.ci_hostname=${{ steps.information.outputs.ci_hostname }}"
echo "steps.information_hostname.outputs.ci_hostname=${{ steps.information_hostname.outputs.ci_hostname }}"
CI_REGISTRY="${{ steps.information.outputs.ci_hostname }}"
echo "CI_REGISTRY=$CI_REGISTRY" >> $GITHUB_ENV
echo "::debug::using CI_REGISTRY=$CI_REGISTRY"
REGISTRY_TARGETS="$CI_REGISTRY/$GITHUB_REPOSITORY:" # needs to have : suffix
echo "REGISTRY_TARGETS=$REGISTRY_TARGETS" >> $GITHUB_ENV
echo "::debug::adding $CI_REGISTRY/$GITHUB_REPOSITORY to registry targets"
- name: add additional registries
shell: bash
run: |
IFS=","
for REGISTRY in $INPUT_ADDITIONAL_REGISTRIES; do
# add ":" to registry paths missing it
if [[ "$REGISTRY" != *":"* ]]; then
REGISTRY="$REGISTRY:"
fi
echo "::debug::adding $REGISTRY to REGISTRY_TARGETS"
REGISTRY_TARGETS="$REGISTRY_TARGETS,$REGISTRY"
done
IFS="$OLD_IFS"
echo "::debug::REGISTRY_TARGETS=$REGISTRY_TARGETS" >> $GITHUB_ENV
- name: add ci registry auth
shell: bash
if: inputs.add_ci_registry_auth == 'true'
run: |
REGISTRY_AUTH_JSON="$REGISTRY_AUTH_JSON {\"auths\":{\"$CI_REGISTRY\":{\"auth\":\"$(echo -n token:$CI_REGISTRY_PASSWORD | base64)\"}}}"
echo "REGISTRY_AUTH_JSON=$REGISTRY_AUTH_JSON" >> $GITHUB_ENV
- name: merge registry auth json
shell: bash
if: inputs.merge_registry_auth_json == 'true'
run: |
if [[ ! -z "$REGISTRY_AUTH_JSON" ]]; then
REGISTRY_AUTH_JSON=$(echo "$REGISTRY_AUTH_JSON" | jq --slurp 'reduce .[] as $item ({}; . * $item)')
echo "REGISTRY_AUTH_JSON=$REGISTRY_AUTH_JSON" >> $GITHUB_ENV
fi
- name: generate registry auth json file
shell: bash
run: |
if [[ ! -z "$REGISTRY_AUTH_JSON" ]]; then
mkdir -p /home/runner/.docker
echo "$REGISTRY_AUTH_JSON" > /home/runner/.docker/config.json
fi
- name: collect tags
shell: bash
run: |
if [[ "$INPUT_TAG_COMMIT_ENABLE" == "true" ]]; then
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$INPUT_TAG_COMMIT_PREFIX$GITHUB_SHA$INPUT_TAG_SUFFIX"
fi
# TODO extract semver vars from tag/branch, candidate for refactor to common info gathering action
# add semver major tag if enabled and available, exclude "0" tag
# add semver major.minor tag if enabled and available, exclude "0.0" tag
# add semver major.minor.patch tag if enabled and available, exclude "0.0.0" tag
if [[ "$INPUT_TAG_SEMVER_MAJOR" == "true" ]] && [[ ! -z $SEMVER_MAJOR ]] && [[ "$SEMVER_MAJOR" != "0" ]]; then
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$SEMVER_MAJOR"
TAG_REF_NORMALIZED_ENABLE=0
fi
if [[ "$INPUT_TAG_SEMVER_MINOR" == "true" ]] && [[ ! -z $SEMVER_MINOR ]] && [[ "$SEMVER_MAJOR_MINOR" != "0.0" ]]; then
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$SEMVER_MAJOR_MINOR"
TAG_REF_NORMALIZED_ENABLE=0
fi
if [[ "$INPUT_TAG_SEMVER_PATCH" == "true" ]] && [[ ! -z $SEMVER_PATCH ]] && [[ "$SEMVER_MAJOR_MINOR_PATCH" != "0.0.0" ]]; then
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$SEMVER_MAJOR_MINOR_PATCH"
TAG_REF_NORMALIZED_ENABLE=0
fi
# add tag for reference if available using normalization
# - dont add tag if semver tags were added
# - attempt to build tag first
# - attempt to build branch if not a PR (if not PR for extra security, variable description on drone unclear)
if [[ "$INPUT_TAG_REF_NORMALIZED_ENABLE" == "true" ]] && [[ ! -z $DRONE_TAG ]]; then
echo "::debug::adding docker tag for git tag"
REF_TAG_NORMALIZED=$(echo $DRONE_TAG | sed s:/:-:g)
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$REF_TAG_NORMALIZED$INPUT_TAG_SUFFIX"
elif [[ "$INPUT_TAG_REF_NORMALIZED_ENABLE" == "true" ]] && [[ -z "$DRONE_PULL_REQUEST" ]] && [[ ! -z $DRONE_BRANCH ]]; then
echo "adding tag for branch"
REF_TAG_NORMALIZED=$(echo $DRONE_BRANCH | sed s:/:-:g)
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$REF_TAG_NORMALIZED$INPUT_TAG_SUFFIX"
fi
# prepare destinations by combining registries + tags
echo "# preparing destinations:"
IMAGE_DESTS=""
IMAGE_DESTS_PUSH=""
IFS=","
for REGISTRY in $REGISTRY_TARGETS; do
if [[ -z "$REGISTRY" ]]; then continue; fi
for IMAGE_TAG in $IMAGE_TAGS; do
if [[ -z "$IMAGE_TAG" ]]; then continue; fi
echo "::debug::- $REGISTRY$IMAGE_TAG"
IMAGE_DESTS="$IMAGE_DESTS --tag $REGISTRY$IMAGE_TAG"
IMAGE_DESTS_PUSH="$IMAGE_DESTS_PUSH $REGISTRY$IMAGE_TAG"
done
done
IMAGE_DESTS=$(echo $IMAGE_DESTS | xargs)
IMAGE_DESTS_PUSH=$(echo $IMAGE_DESTS_PUSH | xargs)
IFS="$OLD_IFS"
echo "IMAGE_DESTS=$IMAGE_DESTS" >> $GITHUB_ENV
echo "IMAGE_DESTS_PUSH=$IMAGE_DESTS_PUSH" >> $GITHUB_ENV
- name: prepare docker args
shell: bash
run: |
# prepare docker build args
if [[ -z "$DOCKER_ARGS" ]]; then
DOCKER_ARGS=""
fi
DOCKER_ARGS="$INPUT_CONTEXT_DIR --file $INPUT_DOCKERFILE $INPUT_DOCKER_ARGS $DOCKER_ARGS"
if [[ "$INPUT_SQUASH_LAYERS" == "true" ]]; then
DOCKER_ARGS="$DOCKER_ARGS --squash"
fi
if [[ ! -z "$IMAGE_DESTS" ]]; then
DOCKER_ARGS="$DOCKER_ARGS $IMAGE_DESTS"
fi
if [[ ! -z "$INPUT_ADDITIONAL_REGISTRY_DESTINATIONS" ]]; then
DOCKER_ARGS="$DOCKER_ARGS $INPUT_ADDITIONAL_REGISTRY_DESTINATIONS"
fi
echo "DOCKER_ARGS=$DOCKER_ARGS" >> $GITHUB_ENV
echo "::debug:: DOCKER_ARGS=$DOCKER_ARGS"
- name: debug print env
shell: bash
run: |
echo "::debug::printing env"
echo "::debug:: $(env)"
using: "node20"
main: ./dist/index.js

252
action.yml.oldbash Normal file
View File

@ -0,0 +1,252 @@
name: 'Docker'
description: 'Build and publish docker images'
inputs:
docker_image:
description: docker client image to use for building images
default: "docker:latest"
docker_args:
description: "Extra arguments to pass to docker invocation"
default: ""
tag_prefix:
description: "a prefix to add to all docker tags"
tag_suffix:
description: "a suffix to add to all docker tags"
tag_commit_enable:
description: "generate docker tags for git tag if present"
default: "true"
tag_commit_prefix:
description: "a suffix to add to docker tags that were generated from commit sha"
default: "commit-"
tag_ref_slug_enable:
description: "generate a tag from the git ref slug"
default: "false"
tag_ref_normalized_enable:
description: ""
default: "true"
tag_semver_major:
description: ""
default: "true"
tag_semver_minor:
description: ""
default: "true"
tag_semver_patch:
description: ""
default: "true"
additional_registry_destinations:
description: "a list of --destination registry/orga/repo:tag strings, space separated"
default: ""
squash_layers:
description: ""
default: "true"
additional_registries:
description: ""
default: ""
add_ci_registry_auth:
description: ""
default: "true"
add_ci_registry_target:
description: ""
default: "true"
registry_auth_json:
description: ""
default: ""
merge_registry_json:
description: ""
default: "true"
push_tags:
description: ""
default: "true"
docker_buildkit:
description: ""
default: "1"
docker_multi_arch:
description: ""
default: "false"
dockerfile:
description: "Dockerfile used to build images"
default: "Dockerfile"
docker_context_dir:
description: "${{ github.workspace }}"
ci_registry_password:
description: "password/token for default ci registry, should usually be set to secret value with ${{ secrets.someSecretName }} for gitea"
default: ""
required: false
outputs:
published_tags:
description: "Published tags as csv"
value: ""
runs:
using: "composite"
steps:
- id: information
uses: https://gitea.dhswt.de/actions/information@master
- name: add ci registry to targets
shell: bash
if: inputs.add_ci_registry_target == 'true'
run: |
CI_REGISTRY="${{ steps.information.outputs.ci_hostname }}"
echo "CI_REGISTRY=$CI_REGISTRY" >> $GITHUB_ENV
echo "::debug::using CI_REGISTRY=$CI_REGISTRY"
REGISTRY_TARGETS="$CI_REGISTRY/$GITHUB_REPOSITORY:" # needs to have : suffix
echo "REGISTRY_TARGETS=$REGISTRY_TARGETS" >> $GITHUB_ENV
echo "::debug::adding $CI_REGISTRY/$GITHUB_REPOSITORY to registry targets"
- name: add additional registries
shell: bash
run: |
IFS=","
for REGISTRY in $INPUT_ADDITIONAL_REGISTRIES; do
# add ":" to registry paths missing it
if [[ "$REGISTRY" != *":"* ]]; then
REGISTRY="$REGISTRY:"
fi
echo "::debug::adding $REGISTRY to REGISTRY_TARGETS"
REGISTRY_TARGETS="$REGISTRY_TARGETS,$REGISTRY"
done
IFS="$OLD_IFS"
echo "::debug::REGISTRY_TARGETS=$REGISTRY_TARGETS" >> $GITHUB_ENV
- name: add ci registry auth
shell: bash
if: inputs.add_ci_registry_auth == 'true'
run: |
REGISTRY_AUTH_JSON="$REGISTRY_AUTH_JSON {\"auths\":{\"$CI_REGISTRY\":{\"auth\":\"$(echo -n token:$CI_REGISTRY_PASSWORD | base64)\"}}}"
echo "REGISTRY_AUTH_JSON=$REGISTRY_AUTH_JSON" >> $GITHUB_ENV
- name: merge registry auth json
shell: bash
if: inputs.merge_registry_auth_json == 'true'
run: |
if [[ ! -z "$REGISTRY_AUTH_JSON" ]]; then
REGISTRY_AUTH_JSON=$(echo "$REGISTRY_AUTH_JSON" | jq --slurp 'reduce .[] as $item ({}; . * $item)')
echo "REGISTRY_AUTH_JSON=$REGISTRY_AUTH_JSON" >> $GITHUB_ENV
fi
- name: generate registry auth json file
shell: bash
run: |
if [[ ! -z "$REGISTRY_AUTH_JSON" ]]; then
mkdir -p /home/runner/.docker
echo "$REGISTRY_AUTH_JSON" > /home/runner/.docker/config.json
fi
- name: collect tags
shell: bash
run: |
if [[ "$INPUT_TAG_COMMIT_ENABLE" == "true" ]]; then
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$INPUT_TAG_COMMIT_PREFIX$GITHUB_SHA$INPUT_TAG_SUFFIX"
fi
# TODO extract semver vars from tag/branch, candidate for refactor to common info gathering action
# add semver major tag if enabled and available, exclude "0" tag
# add semver major.minor tag if enabled and available, exclude "0.0" tag
# add semver major.minor.patch tag if enabled and available, exclude "0.0.0" tag
if [[ "$INPUT_TAG_SEMVER_MAJOR" == "true" ]] && [[ ! -z $SEMVER_MAJOR ]] && [[ "$SEMVER_MAJOR" != "0" ]]; then
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$SEMVER_MAJOR"
TAG_REF_NORMALIZED_ENABLE=0
fi
if [[ "$INPUT_TAG_SEMVER_MINOR" == "true" ]] && [[ ! -z $SEMVER_MINOR ]] && [[ "$SEMVER_MAJOR_MINOR" != "0.0" ]]; then
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$SEMVER_MAJOR_MINOR"
TAG_REF_NORMALIZED_ENABLE=0
fi
if [[ "$INPUT_TAG_SEMVER_PATCH" == "true" ]] && [[ ! -z $SEMVER_PATCH ]] && [[ "$SEMVER_MAJOR_MINOR_PATCH" != "0.0.0" ]]; then
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$SEMVER_MAJOR_MINOR_PATCH"
TAG_REF_NORMALIZED_ENABLE=0
fi
# add tag for reference if available using normalization
# - dont add tag if semver tags were added
# - attempt to build tag first
# - attempt to build branch if not a PR (if not PR for extra security, variable description on drone unclear)
if [[ "$INPUT_TAG_REF_NORMALIZED_ENABLE" == "true" ]] && [[ ! -z $DRONE_TAG ]]; then
echo "::debug::adding docker tag for git tag"
REF_TAG_NORMALIZED=$(echo $DRONE_TAG | sed s:/:-:g)
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$REF_TAG_NORMALIZED$INPUT_TAG_SUFFIX"
elif [[ "$INPUT_TAG_REF_NORMALIZED_ENABLE" == "true" ]] && [[ -z "$DRONE_PULL_REQUEST" ]] && [[ ! -z $DRONE_BRANCH ]]; then
echo "adding tag for branch"
REF_TAG_NORMALIZED=$(echo $DRONE_BRANCH | sed s:/:-:g)
IMAGE_TAGS="$IMAGE_TAGS,$INPUT_TAG_PREFIX$REF_TAG_NORMALIZED$INPUT_TAG_SUFFIX"
fi
# prepare destinations by combining registries + tags
echo "# preparing destinations:"
IMAGE_DESTS=""
IMAGE_DESTS_PUSH=""
IFS=","
for REGISTRY in $REGISTRY_TARGETS; do
if [[ -z "$REGISTRY" ]]; then continue; fi
for IMAGE_TAG in $IMAGE_TAGS; do
if [[ -z "$IMAGE_TAG" ]]; then continue; fi
echo "::debug::- $REGISTRY$IMAGE_TAG"
IMAGE_DESTS="$IMAGE_DESTS --tag $REGISTRY$IMAGE_TAG"
IMAGE_DESTS_PUSH="$IMAGE_DESTS_PUSH $REGISTRY$IMAGE_TAG"
done
done
IMAGE_DESTS=$(echo $IMAGE_DESTS | xargs)
IMAGE_DESTS_PUSH=$(echo $IMAGE_DESTS_PUSH | xargs)
IFS="$OLD_IFS"
echo "IMAGE_DESTS=$IMAGE_DESTS" >> $GITHUB_ENV
echo "IMAGE_DESTS_PUSH=$IMAGE_DESTS_PUSH" >> $GITHUB_ENV
- name: prepare docker args
shell: bash
run: |
# prepare docker build args
if [[ -z "$DOCKER_ARGS" ]]; then
DOCKER_ARGS=""
fi
DOCKER_ARGS="$INPUT_CONTEXT_DIR --file $INPUT_DOCKERFILE $INPUT_DOCKER_ARGS $DOCKER_ARGS"
if [[ "$INPUT_SQUASH_LAYERS" == "true" ]]; then
DOCKER_ARGS="$DOCKER_ARGS --squash"
fi
if [[ ! -z "$IMAGE_DESTS" ]]; then
DOCKER_ARGS="$DOCKER_ARGS $IMAGE_DESTS"
fi
if [[ ! -z "$INPUT_ADDITIONAL_REGISTRY_DESTINATIONS" ]]; then
DOCKER_ARGS="$DOCKER_ARGS $INPUT_ADDITIONAL_REGISTRY_DESTINATIONS"
fi
echo "DOCKER_ARGS=$DOCKER_ARGS" >> $GITHUB_ENV
echo "::debug:: DOCKER_ARGS=$DOCKER_ARGS"
- name: debug print env
shell: bash
run: |
echo "::debug::printing env"
echo "::debug:: $(env)"

17568
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

37
package.json Normal file
View File

@ -0,0 +1,37 @@
{
"name": "docker",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"bundle": "npm run format:write && npm run package",
"ci-test": "jest",
"format:write": "prettier --write **/*.js",
"format:check": "prettier --check **/*.js",
"lint": "npx eslint . -c ./.github/linters/.eslintrc.yml",
"package": "ncc build src/action.js",
"package:watch": "npm run package -- --watch",
"test": "(jest && make-coverage-badge --output-path ./badges/coverage.svg) || make-coverage-badge --output-path ./badges/coverage.svg",
"all": "npm run format:write && npm run lint && npm run test && npm run package"
},
"author": "",
"license": "ISC",
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0",
"information": "file:../information"
},
"devDependencies": {
"@babel/core": "^7.23.5",
"@babel/eslint-parser": "^7.23.3",
"@babel/preset-env": "^7.23.5",
"@vercel/ncc": "^0.38.1",
"babel-preset-jest": "^29.6.3",
"eslint": "^8.54.0",
"eslint-plugin-github": "^4.10.1",
"eslint-plugin-jest": "^27.6.0",
"jest": "^29.7.0",
"make-coverage-badge": "^1.2.0",
"prettier": "^3.1.0"
}
}

138
src/lib.js Normal file
View File

@ -0,0 +1,138 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as fs from 'fs';
export function processAdditionalRegistries(targetRegistries) {
const additionalRegistries = core.getInput('additional_registries');
if (additionalRegistries != null && additionalRegistries.length > 0) {
const additionalRegistriesArr = additionalRegistries.split(',');
for (let registry of additionalRegistriesArr) {
registry = registry.trim();
if (!registry.endsWith(':')) {
registry += ':';
}
targetRegistries.push(registry);
}
}
}
function base64ToBytes(base64) {
const binString = atob(base64);
return Uint8Array.from(binString, (m) => m.codePointAt(0));
}
export function addCiRegistryAuth(ci_registry, registryAuthJson) {
if (!core.getBooleanInput('add_ci_registry_auth')) {
return;
}
if (ci_registry === false || ci_registry.length <= 0) {
console.log(
'WARNING: add_ci_registry_auth enabled but ci_registry is not set'
);
return;
}
if (
process.env['CI_REGISTRY_PASSWORD'] == null ||
process.env['CI_REGISTRY_PASSWORD'].length > 0
) {
console.log(
'WARNING: add_ci_registry_auth enabled but CI_REGISTRY_PASSWORD env is empty'
);
return;
}
registryAuthJson.auths[ci_registry] = base64ToBytes(
'token:' + process.env['CI_REGISTRY_PASSWORD']
);
}
export function mergeArgRegistryAuthJson(registryAuthJson) {
const argRegistryAuthJson = process.env['REGISTRY_AUTH_JSON'];
if (argRegistryAuthJson != null && argRegistryAuthJson.trim().length > 0) {
try {
const argRegistryAuth = JSON.parse(argRegistryAuthJson);
if (argRegistryAuth.auth != null) {
for (const key in argRegistryAuth.auth) {
if (argRegistryAuth.auth.hasOwnProperty(key)) {
registryAuthJson[key] = argRegistryAuth.auth[key];
}
}
}
}
catch (e) {
console.log('Failed to parse registry auth json', e);
core.setFailed(error.message);
process.exit(1);
}
}
}
export function writeRegistryAuthJson(registryAuthJson, path) {
fs.writeFileSync(path, JSON.stringify(registryAuthJson, null, 2));
}
export function collectTags() {
const tags = [];
let foundSemverTag = false;
let tagPrefix = (core.getInput('tag_prefix') ?? '').trim();
let tagSuffix = (core.getInput('tag_suffix') ?? '').trim();
let tagCommitPrefix = (core.getInput('tag_suffix') ?? '').trim();
// handle semver
if (
core.getBooleanInput('tag_semver_major') &&
information.semver_major != null
) {
tags.push(tagPrefix + information.semver_major);
foundSemverTag = true;
}
if (
core.getBooleanInput('tag_semver_minor') &&
information.semver_minor != null
) {
tags.push(tagPrefix + information.semver_minor);
foundSemverTag = true;
}
if (
core.getBooleanInput('tag_semver_patch') &&
information.semver_patch != null
) {
tags.push(tagPrefix + information.semver_patch);
foundSemverTag = true;
}
// handle git tag/branch
if (
core.getBooleanInput('tag_ref_normalized_enable') &&
foundSemverTag === false
) {
if (information.git_tag != null) {
// TODO normalize tag from git for docker
tags.push(tagPrefix + information.git_tag + tagSuffix);
}
if (information.git_current_branch != null) {
// TODO normalize branch from git for docker
tags.push(tagPrefix + information.git_current_branch + tagSuffix);
}
}
// handle commit sha
if (core.getBooleanInput('tag_commit_enable')) {
tags.push(tagPrefix + tagCommitPrefix + github.context.sha + tagSuffix);
}
return tags;
}
export function prepareDestinations(registries, tags) {
const destinations = [];
registries.forEach((registry) => {
tags.forEach((tag) => {
destinations.push(registry + tag);
});
});
return destinations;
}