Build image if not present #50
Workflow file for this run
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: Docker Images | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- main | |
- feature/* | |
jobs: | |
version: | |
runs-on: ubuntu-latest | |
outputs: | |
version: ${{ steps.version.outputs.version }} | |
steps: | |
- | |
name: Checkout | |
uses: actions/checkout@v4 | |
- | |
name: Latest Version | |
id: version | |
run: | | |
. ./.github/bin/reg-tags/image_api.sh | |
_releases() { | |
# Ask GH for the list of releases matching the tag pattern, then fool the sort | |
# -V option to properly understand semantic versioning. Arrange for latest | |
# version to be at the top. See: https://stackoverflow.com/a/40391207 | |
github_releases -r 'v?[0-9]+\.[0-9]+\.[0-9]+' "$1" | | |
sed '/-/!{s/$/_/}' | | |
sort -Vr | | |
sed 's/_$//' | |
} | |
version=$(_releases cloudflare/cloudflared | head -n 1) | |
printf "version=%s\n" "$version" >> "$GITHUB_OUTPUT" | |
# When pushing to the main branch, we (re)generate images, tagged with | |
# "latest" | |
ghcr: | |
runs-on: ubuntu-latest | |
if: github.ref_name == 'main' | |
needs: version | |
steps: | |
- | |
name: Login to GHCR | |
uses: docker/login-action@v1 | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- | |
name: Set up QEMU | |
uses: docker/setup-qemu-action@v2 | |
- | |
name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- | |
name: Check Presence | |
id: present | |
run: | | |
if docker manifest inspect "ghcr.io/${{ github.repository_owner }}/cloudflared:${{ needs.version.outputs.version }}" 2>/dev/null; then | |
printf "present=true\n" >> "$GITHUB_OUTPUT" | |
else | |
printf "present=false\n" >> "$GITHUB_OUTPUT" | |
fi | |
- | |
name: Build cloudflared Image | |
uses: docker/build-push-action@v3 | |
id: cloudflared | |
if: steps.present.outputs.present == 'false' | |
with: | |
push: true | |
file: Dockerfile.cloudflared | |
platforms: linux/amd64,linux/arm64,linux/i386 | |
tags: | | |
ghcr.io/${{ github.repository_owner }}/cloudflared:${{ needs.version.outputs.version }} | |
build-args: | | |
CLOUDFLARED_VERSION=${{ needs.version.outputs.version }} | |
- | |
name: Build and push Base Image | |
uses: docker/build-push-action@v3 | |
with: | |
push: true | |
file: Dockerfile.base | |
platforms: linux/amd64,linux/arm64,linux/i386 | |
build-args: | | |
BASEIMAGE=ghcr.io/${{ github.repository_owner }}/cloudflared:${{ needs.version.outputs.version }} | |
tags: | | |
ghcr.io/${{ github.repository }}-base:${{ needs.version.outputs.version }} | |
ghcr.io/${{ github.repository }}-base:latest | |
- | |
name: Build and push DevEnv Image | |
uses: docker/build-push-action@v3 | |
with: | |
push: true | |
file: Dockerfile | |
platforms: linux/amd64,linux/arm64,linux/i386 | |
build-args: | | |
BASEIMAGE=ghcr.io/${{ github.repository }}-base:${{ needs.version.outputs.version }} | |
tags: | | |
ghcr.io/${{ github.repository }}:${{ needs.version.outputs.version }} | |
ghcr.io/${{ github.repository }}:latest | |
# When pushing to feature branches, we test stuff on the platform of the | |
# runner only, using a local registry so as to be able to push and pull to | |
# Docker from the builder steps. | |
build: | |
runs-on: ubuntu-latest | |
if: github.ref_name != 'main' | |
needs: version | |
services: | |
registry: | |
image: registry:2 | |
ports: | |
- 5000:5000 | |
steps: | |
- | |
name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
with: | |
driver-opts: network=host | |
- | |
name: Check Presence | |
id: present | |
run: | | |
if docker manifest inspect "ghcr.io/${{ github.repository_owner }}/cloudflared:${{ needs.version.outputs.version }}" 2>/dev/null; then | |
printf "present=true\n" >> "$GITHUB_OUTPUT" | |
else | |
printf "present=false\n" >> "$GITHUB_OUTPUT" | |
fi | |
- | |
name: Build cloudflared Image | |
uses: docker/build-push-action@v3 | |
id: cloudflared | |
if: steps.present.outputs.present == 'false' | |
with: | |
push: true | |
file: Dockerfile.cloudflared | |
tags: | | |
localhost:5000/${{ github.repository_owner }}/cloudflared:${{ needs.version.outputs.version }} | |
build-args: | | |
CLOUDFLARED_VERSION=${{ needs.version.outputs.version }} | |
- | |
name: Build Base Image | |
uses: docker/build-push-action@v3 | |
id: base | |
with: | |
push: true | |
file: Dockerfile.base | |
tags: | | |
localhost:5000/${{ github.repository }}-base:${{ needs.version.outputs.version }} | |
localhost:5000/${{ github.repository }}-base:latest | |
build-args: | | |
BASEIMAGE=localhost:5000/${{ github.repository_owner }}/cloudflared:${{ needs.version.outputs.version }} | |
- | |
name: Build DevEnv Image | |
uses: docker/build-push-action@v3 | |
with: | |
push: true | |
build-args: | | |
BASEIMAGE=localhost:5000/${{ github.repository }}-base:${{ needs.version.outputs.version }} | |
file: Dockerfile | |
tags: | | |
localhost:5000/${{ github.repository }}:${{ needs.version.outputs.version }} | |
localhost:5000/${{ github.repository }}:latest |