Skip to content

Commit

Permalink
Improve multi-platform production image build + publish (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halsall authored Nov 29, 2021
1 parent b14a6a4 commit 4f6c31d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 25 deletions.
7 changes: 7 additions & 0 deletions docker-bake.hcl.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ target "app" {
"{{ @('docker.repository') }}:{{ @('docker.tagPrefix') }}{{ tag }}"
{% endfor %}
]
args = {
{% for config in @('git.config_global')|filter(v => v is not empty and ((v.when is not defined) or v.when)) %}
{% for argName, argValue in config.buildArgs|default({}) %}
"{{ argName }}": "{{ argValue }}"
{% endfor %}
{% endfor %}
}
}
1 change: 1 addition & 0 deletions harness/config/confd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ confd('harness:/'):
- { src: docker-compose.yml, dst: workspace:/docker-compose.yml }
- { src: docker-bake.hcl, dst: workspace:/docker-bake.hcl }
- { src: harness/scripts/mod_init.sh }
- { src: harness/scripts/destroy.sh }
- { src: harness/scripts/init.sh }
- { src: harness/scripts/docker/build-prod.sh }
- { src: harness/scripts/docker/publish.sh }
Expand Down
2 changes: 1 addition & 1 deletion harness/config/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ command('app publish'):
DOCKER_BUILDKIT: "1"
exec: |
#!bash(workspace:/)|@
source .my127ws/harness/scripts/docker/publish.sh
source .my127ws/harness/scripts/docker/publish.sh @('docker.registry.url') @('docker.registry.username') @('docker.registry.password')
command('app deploy <environment>'):
env:
Expand Down
5 changes: 0 additions & 5 deletions harness/scripts/destroy.sh

This file was deleted.

14 changes: 14 additions & 0 deletions harness/scripts/destroy.sh.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

function destroy() {
run docker-compose down --rmi local --volumes --remove-orphans

if [ "{{ @('docker.experimental.multiplatform_build.enabled') }}" == "yes" ]; then
run docker buildx rm "{{ @('namespace') }}" || true
fi

passthru ws cleanup built-images
run rm -f .my127ws/.flag-built
}

destroy
25 changes: 10 additions & 15 deletions harness/scripts/docker/build-prod.sh.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@
set -o pipefail

function docker_build_prod {
{% if @('docker.experimental.multiplatform_build.enabled') == "yes" %}
local context=$(docker buildx create --use)
{% endif %}

trap "clean '${context}'" EXIT

if [ "{{ @('docker.experimental.multiplatform_build.enabled') }}" == "yes" ]; then
passthru docker buildx bake --file docker-bake.hcl
local builder="{{ @('namespace') }}"
run docker buildx create --name "${builder}"
passthru docker buildx bake --file docker-bake.hcl --builder "${builder}"
else
passthru docker build --tag {{ @('docker.repository') }}:{{ @('docker.tagPrefix') }}{{ @('app.version') }} --file .my127ws/docker/image/app/Dockerfile --target prod .
passthru docker build --tag {{ @('docker.repository') }}:{{ @('docker.tagPrefix') }}{{ @('app.version') }} \
{% for config in @('git.config_global')|filter(v => v is not empty and ((v.when is not defined) or v.when)) %}
{% for argName, argValue in config.buildArgs|default({}) %}
--build-arg "{{ argName }}={{ argValue }}" \
{% endfor %}
{% endfor %}
--file .my127ws/docker/image/app/Dockerfile --target prod .
fi
}

function clean() {
context="$1"
if [[ "$context" != "" ]]; then
docker buildx rm "${CONTEXT}"
fi
}

docker_build_prod
12 changes: 8 additions & 4 deletions harness/scripts/docker/publish.sh.twig
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#!/bin/bash

function docker_publish() {
echo '{{ @('docker.registry.password') }}' | run docker login --username='{{ @('docker.registry.username') }}' --password-stdin '{{ @('docker.registry.url') }}'
local registryUrl=$1
local username=$2
local password=$3

echo "${password}" | run docker login --username="${username}" --password-stdin "${registryUrl}"

if [ "{{ @('docker.experimental.multiplatform_build.enabled') }}" == "yes" ]; then
passthru docker buildx bake --file docker-bake.hcl --push
else
run docker push {{ @('docker.repository') }}:{{ @('docker.tagPrefix')}}{{ @('app.version') }}
{% for tag in @('docker.production.additional_tags') %}
{% for tag in @('docker.production.additional_tags') %}
run docker push {{ @('docker.repository') }}:{{ @('docker.tagPrefix')}}{{ tag }}
{% endfor %}
{% endfor %}
fi

run docker logout {{ @('docker.registry.url') }}
}

docker_publish
docker_publish "$1" "$2" "$3"

0 comments on commit 4f6c31d

Please sign in to comment.