From 183233801f3976ab19e586455ce90684f6e968ea Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 18 Jun 2020 13:16:43 +0200 Subject: [PATCH] action.yml: checkout the source, build a Docker image and push it --- Dockerfile | 1 + action.Dockerfile | 20 +++++++++++++++++++ action.bash | 50 +++++++++++++++++++++++++++++++++++++++++++++++ action.yml | 4 ++++ 4 files changed, 75 insertions(+) create mode 100644 Dockerfile create mode 100644 action.Dockerfile create mode 100755 action.bash create mode 100644 action.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..524e6cd --- /dev/null +++ b/Dockerfile @@ -0,0 +1 @@ +FROM debian:buster-slim diff --git a/action.Dockerfile b/action.Dockerfile new file mode 100644 index 0000000..831e9a0 --- /dev/null +++ b/action.Dockerfile @@ -0,0 +1,20 @@ +FROM buildpack-deps:scm as clone +SHELL ["/bin/bash", "-exo", "pipefail", "-c"] + +RUN mkdir actions ;\ + cd actions ;\ + git clone --bare https://github.com/actions/checkout.git ;\ + git -C checkout.git archive --prefix=checkout/ v2 |tar -x ;\ + git clone --bare https://github.com/actions/upload-artifact.git ;\ + git -C upload-artifact.git archive --prefix=upload-artifact/ v2 |tar -x ;\ + rm -rf *.git + + +FROM docker + +RUN ["apk", "add", "bash", "grep", "nodejs"] +COPY --from=clone /actions /actions + +COPY action.bash Dockerfile / + +CMD ["/action.bash"] diff --git a/action.bash b/action.bash new file mode 100755 index 0000000..9ded191 --- /dev/null +++ b/action.bash @@ -0,0 +1,50 @@ +#!/bin/bash +set -exo pipefail + +TARGET=icinga/icinga2 + +mkimg () { + test -n "$TAG" + + node /actions/checkout/dist/index.js |grep -vFe ::add-matcher:: + docker build -f /Dockerfile -t "${TARGET}:$TAG" . + STATE_isPost=1 node /actions/checkout/dist/index.js + + docker save "${TARGET}:$TAG" |gzip >docker-save.tgz + INPUT_NAME=docker-image INPUT_PATH=docker-save.tgz node /actions/upload-artifact/dist/index.js + rm docker-save.tgz +} + +push () { + test -n "$TAG" + + if [ "$(tr -d '\n' <<<"$DOCKER_HUB_PASSWORD" |wc -c)" -gt 0 ]; then + docker login -u icingaadmin --password-stdin <<<"$DOCKER_HUB_PASSWORD" + docker push "${TARGET}:$TAG" + docker logout + fi +} + +case "$GITHUB_EVENT_NAME" in + pull_request) + grep -qEe '^refs/pull/[0-9]+' <<<"$GITHUB_REF" + TAG="pr$(grep -oEe '[0-9]+' <<<"$GITHUB_REF")" + mkimg + ;; + push) + grep -qEe '^refs/heads/.' <<<"$GITHUB_REF" + TAG="$(cut -d / -f 3- <<<"$GITHUB_REF")" + mkimg + push + ;; + release) + grep -qEe '^refs/tags/v[0-9]' <<<"$GITHUB_REF" + TAG="$(cut -d v -f 2- <<<"$GITHUB_REF")" + mkimg + push + ;; + *) + echo "Unknown event: $GITHUB_EVENT_NAME" >&2 + false + ;; +esac diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..29ba6af --- /dev/null +++ b/action.yml @@ -0,0 +1,4 @@ +name: Icinga 2 Docker image +runs: + using: docker + image: action.Dockerfile