redirect image added #110
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy multi-arch Docker images | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build-stratigy: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
- id: set-matrix | |
run: | | |
sudo apt-get install jq | |
echo "::set-output name=matrix::$(bash prepare.sh)" | |
outputs: | |
matrix: ${{ steps.set-matrix.outputs.matrix }} | |
builder: | |
needs: build-stratigy | |
strategy: | |
max-parallel: 2 | |
matrix: | |
dirs: ${{fromJson(needs.build-stratigy.outputs.matrix)}} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
- name: Init builder | |
id: pre-build | |
run: | | |
dir=${{ matrix.dirs }} | |
# dir=$(echo "$dir" | sed 's/\/*$//g') | |
echo ::set-output name=org_name::"techthinkerorg" | |
echo ::set-output name=image_name::"$dir" | |
echo ::set-output name=version::$(head -n1 "${dir}/.version") | |
echo ::set-output name=arch::$(head -n1 "${dir}/.arch") | |
echo ::set-output name=forced::$([ -f "${dir}/.forced" ] && echo 'true' || echo 'false') | |
- name: Prepare | |
id: prepare | |
run: | | |
DOCKER_IMAGE=${{steps.pre-build.outputs.org_name}}/${{steps.pre-build.outputs.image_name}} | |
DOCKER_PLATFORMS=${{steps.pre-build.outputs.arch}} | |
VERSION=${{steps.pre-build.outputs.version}} | |
TAGS="--tag ${DOCKER_IMAGE}:${VERSION}" | |
if [ $VERSION = latest -o $VERSION = nightly ]; then | |
TAGS="$TAGS --tag ${DOCKER_IMAGE}:latest" | |
fi | |
echo ::set-output name=docker_image::${DOCKER_IMAGE} | |
echo ::set-output name=version::${VERSION} | |
echo ::set-output name=buildx_args::--file ${{steps.pre-build.outputs.image_name}}/Dockerfile \ | |
--platform ${DOCKER_PLATFORMS} \ | |
--build-arg VERSION=${VERSION} \ | |
--build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \ | |
--build-arg VCS_REF=${GITHUB_SHA::8} \ | |
${TAGS} ${{steps.pre-build.outputs.image_name}}/ | |
- name: Check If Image Exists | |
id: check-image | |
run: | | |
UNAME=${{ secrets.DOCKER_USERNAME }} | |
UPASS=${{ secrets.DOCKER_PASSWORD }} | |
DOCKER_IMAGE=${{steps.pre-build.outputs.org_name}}/${{steps.pre-build.outputs.image_name}} | |
VERSION=${{steps.pre-build.outputs.version}} | |
function docker_tag_exists() { | |
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token) | |
curl --silent -f --head -lL https://hub.docker.com/v2/repositories/$1/tags/$2/ > /dev/null | |
} | |
if docker_tag_exists ${DOCKER_IMAGE} ${VERSION}; then | |
echo "Docker image ${DOCKER_IMAGE}:${VERSION} already exists" | |
echo ::set-output name=build_needed::${{ steps.pre-build.outputs.forced }} | |
else | |
echo "Docker image ${DOCKER_IMAGE}:${VERSION} does not exist" | |
echo ::set-output name=build_needed::'true' | |
fi | |
- name: Set up QEMU | |
if: ${{ steps.check-image.outputs.build_needed == 'true' }} | |
uses: docker/setup-qemu-action@v1 | |
with: | |
platforms: all | |
- name: Set up Docker Buildx | |
if: ${{ steps.check-image.outputs.build_needed == 'true' }} | |
id: buildx | |
uses: docker/setup-buildx-action@v1 | |
with: | |
version: latest | |
- name: Available platforms | |
if: ${{ steps.check-image.outputs.build_needed == 'true' }} | |
run: echo ${{ steps.buildx.outputs.platforms }} | |
- name: Docker Buildx (build) | |
if: ${{ steps.check-image.outputs.build_needed == 'true' }} | |
run: | | |
docker buildx build --no-cache --pull --output "type=image,push=false" ${{ steps.prepare.outputs.buildx_args }} | |
- name: Docker Login | |
if: ${{ steps.check-image.outputs.build_needed == 'true' && success() && github.event_name != 'pull_request' }} | |
env: | |
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} | |
run: | | |
echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USERNAME}" --password-stdin | |
- name: Docker Buildx (push) | |
if: ${{ steps.check-image.outputs.build_needed == 'true' && success() && github.event_name != 'pull_request' }} | |
run: | | |
docker buildx build --output "type=image,push=true" ${{ steps.prepare.outputs.buildx_args }} | |
- name: Docker Check Manifest | |
if: ${{ steps.check-image.outputs.build_needed == 'true' && always() && github.event_name != 'pull_request' }} | |
run: | | |
docker run --rm mplatform/mquery ${{ steps.prepare.outputs.docker_image }}:${{ steps.prepare.outputs.version }} | |
- name: Clear | |
if: ${{ steps.check-image.outputs.build_needed == 'true' && always() && github.event_name != 'pull_request' }} | |
run: | | |
rm -f ${HOME}/.docker/config.json |