update and simplify github build action

This commit is contained in:
viktorstrate
2023-07-10 18:37:18 +02:00
parent 228f2cc1e7
commit b2a79a4c9d

View File

@@ -2,9 +2,9 @@ name: Docker builds
on: on:
pull_request: pull_request:
branches: master branches: [master, fix-build-pipeline]
push: push:
branches: master branches: [master, fix-build-pipeline]
tags: tags:
- v* - v*
@@ -18,14 +18,14 @@ jobs:
name: Build Docker Image name: Build Docker Image
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04
strategy: # strategy:
fail-fast: false # fail-fast: false
matrix: # matrix:
target_platform: # target_platform:
- "linux/amd64" # - "linux/amd64"
- "linux/arm64" # - "linux/arm64"
- "linux/arm/v7" # - "linux/arm/v7"
- "linux/arm/v6" # - "linux/arm/v6"
steps: steps:
- name: Checkout - name: Checkout
@@ -48,114 +48,143 @@ jobs:
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1 uses: docker/setup-buildx-action@v1
- name: Prepare # - name: Prepare
id: prepare # id: prepare
run: | # run: |
DOCKER_PLATFORMS=${{ matrix.target_platform }} # DOCKER_PLATFORMS=${{ matrix.target_platform }}
VERSION=edge # VERSION=edge
if [[ $GITHUB_REF == refs/tags/* ]]; then # if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/v} # VERSION=${GITHUB_REF#refs/tags/v}
fi # fi
OUTPUT_PLATFORM=$(echo ${{ matrix.target_platform }} | sed 's/\//-/g') # OUTPUT_PLATFORM=$(echo ${{ matrix.target_platform }} | sed 's/\//-/g')
echo ::set-output name=output_platform::${OUTPUT_PLATFORM} # echo ::set-output name=output_platform::${OUTPUT_PLATFORM}
TAG="--tag ${DOCKER_IMAGE}:${OUTPUT_PLATFORM}-${GITHUB_SHA::8}-${{ github.event_name }}" # TAG="--tag ${DOCKER_IMAGE}:${OUTPUT_PLATFORM}-${GITHUB_SHA::8}-${{ github.event_name }}"
echo ::set-output name=docker_username::${DOCKER_USERNAME} # echo ::set-output name=docker_username::${DOCKER_USERNAME}
echo ::set-output name=docker_image::${DOCKER_IMAGE} # echo ::set-output name=docker_image::${DOCKER_IMAGE}
echo ::set-output name=version::${VERSION} # echo ::set-output name=version::${VERSION}
echo ::set-output name=buildx_args::--platform ${DOCKER_PLATFORMS} \ # echo ::set-output name=buildx_args::--platform ${DOCKER_PLATFORMS} \
--progress=plain \ # --progress=plain \
--cache-from "type=local,src=/tmp/.buildx-cache" \ # --cache-from "type=local,src=/tmp/.buildx-cache" \
--cache-to "type=local,dest=/tmp/.buildx-cache" \ # --cache-to "type=local,dest=/tmp/.buildx-cache" \
--build-arg VERSION=${VERSION} \ # --build-arg VERSION=${VERSION} \
--build-arg COMMIT_SHA=${GITHUB_SHA} \ # --build-arg COMMIT_SHA=${GITHUB_SHA} \
--build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \ # --build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \
--build-arg VCS_REF=${GITHUB_SHA::8} \ # --build-arg VCS_REF=${GITHUB_SHA::8} \
${TAG} --file Dockerfile . # ${TAG} --file Dockerfile .
- name: Docker Build # - name: Docker Build
run: | # run: |
docker buildx build --output "type=image,push=false" ${{ steps.prepare.outputs.buildx_args }} # docker buildx build --output "type=image,push=false" ${{ steps.prepare.outputs.buildx_args }}
- name: Docker Login - name: Docker Login
if: success() && github.event_name != 'pull_request' && github.repository == 'photoview/photoview' if: success() && github.event_name != 'pull_request' && github.repository == 'photoview/photoview'
run: | uses: docker/login-action@v2
echo "${DOCKER_PASSWORD}" | docker login --username "${{ steps.prepare.outputs.docker_username }}" --password-stdin with:
username: ${{ env.DOCKER_USERNAME }}
password: ${{ env.DOCKER_PASSWORD }}
# run: |
# echo "${DOCKER_PASSWORD}" | docker login --username "${{ steps.prepare.outputs.docker_username }}" --password-stdin
- name: Push to Docker Hub - name: Docker meta
if: success() && github.event_name != 'pull_request' && github.repository == 'photoview/photoview' id: docker_meta
run: | uses: docker/metadata-action@v3
docker buildx build --output "type=image,push=true" ${{ steps.prepare.outputs.buildx_args }} with:
# list of Docker images to use as base name for tags
images: ${{ env.DOCKER_IMAGE }}
# Docker tags based on the following events/attributes
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
- name: Clear - name: Build and push
if: always() && github.event_name != 'pull_request' && github.repository == 'photoview/photoview' uses: docker/build-push-action@v2
run: | with:
rm -f ${HOME}/.docker/config.json context: .
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
push: ${{ github.event_name != 'pull_request' && github.repository == 'photoview/photoview' }}
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
docker-manifest: # - name: Push to Docker Hub
name: Combine Docker Images # if: success() && github.event_name != 'pull_request' && github.repository == 'photoview/photoview'
runs-on: ubuntu-20.04 # run: |
if: github.event_name != 'pull_request' && github.repository == 'photoview/photoview' # docker buildx build --output "type=image,push=true" ${{ steps.prepare.outputs.buildx_args }}
defaults:
run:
shell: bash
needs: [build] # - name: Clear
# if: always() && github.event_name != 'pull_request' && github.repository == 'photoview/photoview'
# run: |
# rm -f ${HOME}/.docker/config.json
steps: # docker-manifest:
- name: Docker Login # name: Combine Docker Images
run: | # runs-on: ubuntu-20.04
echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USERNAME}" --password-stdin # if: github.event_name != 'pull_request' && github.repository == 'photoview/photoview'
# defaults:
# run:
# shell: bash
- name: Create Manifests # needs: [build]
run: |
DOCKER_IMAGES="${DOCKER_IMAGE}:linux-amd64-${GITHUB_SHA::8}-${{ github.event_name }}"
DOCKER_IMAGES="${DOCKER_IMAGES} ${DOCKER_IMAGE}:linux-arm64-${GITHUB_SHA::8}-${{ github.event_name }}"
DOCKER_IMAGES="${DOCKER_IMAGES} ${DOCKER_IMAGE}:linux-arm-v7-${GITHUB_SHA::8}-${{ github.event_name }}"
DOCKER_IMAGES="${DOCKER_IMAGES} ${DOCKER_IMAGE}:linux-arm-v6-${GITHUB_SHA::8}-${{ github.event_name }}"
VERSION=edge # steps:
if [[ $GITHUB_REF == refs/tags/* ]]; then # - name: Docker Login
VERSION=${GITHUB_REF#refs/tags/v} # run: |
fi # echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USERNAME}" --password-stdin
TAGS=("${VERSION}") # - name: Create Manifests
# run: |
# DOCKER_IMAGES="${DOCKER_IMAGE}:linux-amd64-${GITHUB_SHA::8}-${{ github.event_name }}"
# DOCKER_IMAGES="${DOCKER_IMAGES} ${DOCKER_IMAGE}:linux-arm64-${GITHUB_SHA::8}-${{ github.event_name }}"
# DOCKER_IMAGES="${DOCKER_IMAGES} ${DOCKER_IMAGE}:linux-arm-v7-${GITHUB_SHA::8}-${{ github.event_name }}"
# DOCKER_IMAGES="${DOCKER_IMAGES} ${DOCKER_IMAGE}:linux-arm-v6-${GITHUB_SHA::8}-${{ github.event_name }}"
if [[ $VERSION =~ ^(([0-9]{1,3})\.[0-9]{1,3})\.[0-9]{1,3}$ ]]; then # VERSION=edge
VERSION_MINOR=${BASH_REMATCH[1]} # if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION_MAJOR=${BASH_REMATCH[2]} # VERSION=${GITHUB_REF#refs/tags/v}
TAGS+=("${VERSION_MAJOR}" "${VERSION_MINOR}" "latest") # fi
fi
for TAG in ${TAGS[*]}; do # TAGS=("${VERSION}")
echo "Creating tag: ${TAG}"
docker manifest create ${DOCKER_IMAGE}:${TAG} ${DOCKER_IMAGES}
docker manifest push ${DOCKER_IMAGE}:${TAG}
done
- name: Cleanup tags # if [[ $VERSION =~ ^(([0-9]{1,3})\.[0-9]{1,3})\.[0-9]{1,3}$ ]]; then
run: | # VERSION_MINOR=${BASH_REMATCH[1]}
ACCESS_TOKEN=$(curl -X POST \ # VERSION_MAJOR=${BASH_REMATCH[2]}
-H "Content-Type: application/json" \ # TAGS+=("${VERSION_MAJOR}" "${VERSION_MINOR}" "latest")
-H "Accept: application/json" \ # fi
-d "{\"username\":\"${DOCKER_USERNAME}\",\"password\":\"${DOCKER_PASSWORD}\"}" \
https://hub.docker.com/v2/users/login/ | jq --raw-output '.token')
PLATFORMS=("amd64" "arm64" "arm-v7" "arm-v6") # for TAG in ${TAGS[*]}; do
for PLATFORM in ${PLATFORMS[@]}; do # echo "Creating tag: ${TAG}"
TAG="linux-${PLATFORM}-${GITHUB_SHA::8}-${{ github.event_name }}" # docker manifest create ${DOCKER_IMAGE}:${TAG} ${DOCKER_IMAGES}
echo "Deleting tag: ${DOCKER_IMAGE}:${TAG}" # docker manifest push ${DOCKER_IMAGE}:${TAG}
# done
curl -X DELETE \ # - name: Cleanup tags
-H "Accept: application/json" \ # run: |
-H "Authorization: JWT ${ACCESS_TOKEN}" \ # ACCESS_TOKEN=$(curl -X POST \
https://hub.docker.com/v2/repositories/${DOCKER_IMAGE}/tags/${TAG}/ # -H "Content-Type: application/json" \
done # -H "Accept: application/json" \
# -d "{\"username\":\"${DOCKER_USERNAME}\",\"password\":\"${DOCKER_PASSWORD}\"}" \
# https://hub.docker.com/v2/users/login/ | jq --raw-output '.token')
- name: Clear # PLATFORMS=("amd64" "arm64" "arm-v7" "arm-v6")
if: always() # for PLATFORM in ${PLATFORMS[@]}; do
run: | # TAG="linux-${PLATFORM}-${GITHUB_SHA::8}-${{ github.event_name }}"
rm -f ${HOME}/.docker/config.json # echo "Deleting tag: ${DOCKER_IMAGE}:${TAG}"
# curl -X DELETE \
# -H "Accept: application/json" \
# -H "Authorization: JWT ${ACCESS_TOKEN}" \
# https://hub.docker.com/v2/repositories/${DOCKER_IMAGE}/tags/${TAG}/
# done
# - name: Clear
# if: always()
# run: |
# rm -f ${HOME}/.docker/config.json