Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub Action + tag suffix + build options #13

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,30 @@ jobs:
run: ./build.sh
```

Alternatively you may skip the local `build.sh` script and use this repository
as a step action directly.

```yaml
jobs:
build:
runs-on: ubuntu-latest
# needs: test # you can declare a `test` job and uncomment this to test the app before building
env:
DOCKER_REPOSITORY: 'dockerhub_org/dockerhub_repo'
DOCKER_USER: ${{ secrets.DOCKER_USER }}
DOCKER_PASS: ${{ secrets.DOCKER_PASS }}
steps:
- uses: actions/checkout@v2
- uses: manastech/ci-docker-builder@<sha1>
# with:
# skip-login: <true|false>
# repository: ""
# repository-suffix: ""
# tag-suffix: ""
# build-directory: ""
# build-options: ""
```

## Functions

### `dockerSetup [--skip-login] [latest]`
Expand All @@ -138,7 +162,9 @@ It can receive these optional arguments:

* `-r <repo>`: Override the repository where the image is pushed
* `-s <suffix>`: Override the repository by adding a suffix to the one specified by the environment variable
* `-t <suffix>`: Append a suffix to the image tags (e.g. `-next`)
* `-d <dir>`: Build from the specified directory
* `-o "<options>"`: Options to directly pass to the docker build command

This function can be called several times to build different images within the same build. For example:

Expand Down
35 changes: 35 additions & 0 deletions action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const process = require("process")
const { spawn } = require("child_process")
const fs = require("fs")

// basic implementation of @action/core.getInput()
function getInput(name) {
return process.env[`INPUT_${name.toUpperCase()}`].trim()
}

// collect action inputs:
let setupOpts = []
if (getInput("skip-login") === "true") setupOpts.push("--skip-login")
if (getInput("latest") === "true") setupOpts.push("latest")

let buildOpts = [], value
if ((value = getInput("repository")) !== "") buildOpts.push(`-r ${value}`)
if ((value = getInput("repository-suffix")) !== "") buildOpts.push(`-s ${value}`)
if ((value = getInput("tag-suffix")) !== "") buildOpts.push(`-t ${value}`)
if ((value = getInput("build-directory")) !== "") buildOpts.push(`-d ${value}`)
if ((value = getInput("build-options")) !== "") buildOpts.push(`-o "${value}"`)

// generate a local build.sh script:
fs.writeFileSync("build.sh", `#! bash
source ${__dirname}/build.sh
dockerSetup ${setupOpts.join(" ")}
echo $VERSION > VERSION
dockerBuildAndPush ${buildOpts.join(" ")}
`)

// execute it:
const build = spawn("bash", ["build.sh"], { maxBuffer: 100 * 1024 * 1024 })
build.stdout.on("data", data => { process.stdout.write(data) })
build.stderr.on("data", data => { process.stderr.write(data) })
build.on("error", error => { console.error(error) })
build.on("exit", code => { process.exit(code) })
30 changes: 30 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Metadata for the GitHub Action
name: CI Docker Builder
author: Manas
description: Build and publish Docker image(s)

inputs:
skip-login:
type: boolean

latest:
type: boolean

repository:
type: string

repository-suffix:
type: string

tag-suffix:
type: string

build-directory:
type: string

build-options:
type: string

runs:
using: node16
main: action.js
17 changes: 13 additions & 4 deletions build.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ dockerBuildAndPush() {
local REPO=$DOCKER_REPOSITORY
local DIR="."
local OPTIND
local BUILD_OPTS

while getopts ":r:d:s:" opt "$@"; do
while getopts ":r:d:s:t:o:" opt "$@"; do
case ${opt} in
r)
REPO=$OPTARG
Expand All @@ -117,21 +118,29 @@ dockerBuildAndPush() {
DIR=$OPTARG
;;

t)
TAG_SUFFIX=$OPTARG
;;

o)
BUILD_OPTS=$OPTARG
;;

*)
;;
esac
done

local IMAGE="${REPO}:${DOCKER_TAG}"
local IMAGE="${REPO}:${DOCKER_TAG}${TAG_SUFFIX}"

echo "Building image ${IMAGE} from ${DIR}"
docker build -t "${IMAGE}" "${DIR}"
docker build ${BUILD_OPTS} -t "${IMAGE}" "${DIR}"

echo "Pushing ${IMAGE}"
docker push "${IMAGE}"

if [[ -n "$EXTRA_DOCKER_TAG" ]]; then
local EXTRA_IMAGE="${REPO}:${EXTRA_DOCKER_TAG}"
local EXTRA_IMAGE="${REPO}:${EXTRA_DOCKER_TAG}${TAG_SUFFIX}"
echo "Tagging also as $EXTRA_IMAGE"
docker tag "${IMAGE}" "${EXTRA_IMAGE}"

Expand Down