docker/.github/workflows/containers.yml

405 lines
13 KiB
YAML

name: Build and push containers
on:
# push:
pull_request:
workflow_dispatch:
inputs:
ref:
description: "Tag or branch to deploy (empty for main)"
default: "main"
required: false
target:
description: "Single target to build (empty for all)"
required: false
schedule:
- cron: "0 5 * * *" # daily snapshot
- cron: "0 6 * * 0" # weekly 22.03-SNAPSHOT
- cron: "0 7 * * 0" # weekly 21.02-SNAPSHOT
jobs:
generate_matrix:
name: Set matrix
runs-on: ubuntu-latest
outputs:
imagebuilders: ${{ steps.find_targets.outputs.imagebuilders }}
rootfs: ${{ steps.find_targets.outputs.rootfs }}
sdks: ${{ steps.find_targets.outputs.sdks }}
ref: ${{ steps.find_targets.outputs.ref }}
version: ${{ steps.find_targets.outputs.version }}
version_path: ${{ steps.find_targets.outputs.version_path }}
steps:
- name: Set relase to 22.03-SNAPSHOT
if: github.event.schedule == '0 6 * * 0'
run: |
echo "VERSION=22.03-SNAPSHOT" >> "$GITHUB_ENV"
echo "VERSION_PATH=releases/22.03-SNAPSHOT" >> "$GITHUB_ENV"
echo "REF=openwrt-22.03" >> "$GITHUB_ENV"
- name: Set relase to 21.02-SNAPSHOT
if: github.event.schedule == '0 7 * * 0'
run: |
echo "VERSION=21.02-SNAPSHOT" >> "$GITHUB_ENV"
echo "VERSION_PATH=releases/21.02-SNAPSHOT" >> "$GITHUB_ENV"
echo "REF=openwrt-21.02" >> "$GITHUB_ENV"
- name: Set relase manually
if: github.event.inputs.ref != ''
run: |
export REF=${{ github.event.inputs.ref == 'master' && 'main' || github.event.inputs.ref }}
echo "REF=$REF" >> "$GITHUB_ENV"
case $REF in
main)
VERSION=SNAPSHOT
echo "VERSION_PATH=snapshots" >> "$GITHUB_ENV"
;;
openwrt-*)
VERSION=${REF//openwrt-/}-SNAPSHOT
echo "VERSION_PATH=releases/$VERSION" >> "$GITHUB_ENV"
;;
v*)
VERSION=${REF//v/}
echo "VERSION_PATH=releases/$VERSION" >> "$GITHUB_ENV"
;;
*)
echo "No tag or branch found"
exit 1
;;
esac
echo "VERSION=$VERSION" >> "$GITHUB_ENV"
- name: Checkout
uses: actions/checkout@v3
with:
repository: openwrt/openwrt
ref: ${{ env.REF}}
- name: Set targets
id: find_targets
run: |
TARGET_FILTER="${{ github.event.inputs.target }}"
# imagebuilders & rootfs
JSON_IB='['
FIRST_IB=1
JSON_ROOTFS='['
FIRST_ROOTFS=1
while read -r line;
do
TARGET=$(echo "$line" | cut -d " " -f 1)
ARCH=$(echo "$line" | cut -d " " -f 2)
[[ $FIRST_IB -ne 1 ]] && JSON_IB="$JSON_IB"','
FIRST_IB=0
JSON_IB="$JSON_IB"'{"target":"'"$TARGET"'"}'
case "$TARGET" in
x86/*|arm*|malta/be|mvebu/cortexa9)
[[ $FIRST_ROOTFS -ne 1 ]] && JSON_ROOTFS="$JSON_ROOTFS"','
FIRST_ROOTFS=0
JSON_ROOTFS="$JSON_ROOTFS"'{"target":"'"$TARGET"'","arch":"'"$ARCH"'"}'
;;
esac
done <<< $(perl ./scripts/dump-target-info.pl targets 2>/dev/null | grep "$TARGET_FILTER")
JSON_IB='{"include":'"$JSON_IB"']}'
echo -e "\n---- imagebuilders ----\n"
echo "$JSON_IB" | jq
echo -e "\n---- imagebuilders ----\n"
echo "imagebuilders=$JSON_IB" >> "$GITHUB_OUTPUT"
JSON_ROOTFS='{"include":'"$JSON_ROOTFS"']}'
echo -e "\n---- rootfs ----\n"
echo "$JSON_ROOTFS" | jq
echo -e "\n---- rootfs ----\n"
echo "rootfs=$JSON_ROOTFS" >> "$GITHUB_OUTPUT"
# sdks
JSON='['
FIRST=1
while read -r line;
do
ARCH=$(echo "$line" | cut -d " " -f 1)
TARGET=$(echo "$line" | cut -d " " -f 2)
TARGETS=$(echo "$line" | cut -d " " -f 2- | sed -e 's/ /\\n/g')
[[ $FIRST -ne 1 ]] && JSON="$JSON"','
FIRST=0
JSON="$JSON"'{"arch":"'"$ARCH"'","target":"'"$TARGET"'","tags":"'"$ARCH"'\n'"$TARGETS"'"}'
done <<< $(perl ./scripts/dump-target-info.pl architectures 2>/dev/null | grep "$TARGET_FILTER")
JSON='{"include":'"$JSON"']}'
echo -e "\n---- sdks ----\n"
echo "$JSON" | jq
echo -e "\n---- sdks ----\n"
echo "sdks=$JSON" >> "$GITHUB_OUTPUT"
echo "ref=${REF:-main}" >> "$GITHUB_OUTPUT"
echo "version=${VERSION:-SNAPSHOT}" >> "$GITHUB_OUTPUT"
echo "version_path=${VERSION_PATH:-snapshots}" >> "$GITHUB_OUTPUT"
push-imagebuilder-container:
name: ImageBuilder
runs-on: ubuntu-latest
needs: generate_matrix
strategy:
fail-fast: False
matrix: ${{fromJson(needs.generate_matrix.outputs.imagebuilders)}}
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Docker.io Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Login to Quay.io Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: quay.io
username: ${{ secrets.QUAY_USER }}
password: ${{ secrets.QUAY_TOKEN }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: |
ghcr.io/openwrt/imagebuilder
docker.io/openwrt/imagebuilder
quay.io/openwrt/imagebuilder
flavor: latest=false
tags: |
${{ matrix.target }}-${{ needs.generate_matrix.outputs.ref }}
${{ matrix.target }}-${{ needs.generate_matrix.outputs.version }}
${{ matrix.target }}-master,enable=${{ needs.generate_matrix.outputs.ref == 'main' }}
${{ matrix.target }},enable=${{ needs.generate_matrix.outputs.version == 'SNAPSHOT' }}
latest,enable=${{ needs.generate_matrix.outputs.version == 'SNAPSHOT' && matrix.target == 'x86/64'}}
- name: Build and push
uses: docker/build-push-action@v3
with:
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
DOWNLOAD_FILE=imagebuilder-.*x86_64.tar.xz
VERSION_PATH=${{ needs.generate_matrix.outputs.version_path }}
TARGET=${{ matrix.target }}
push-sdk-container:
name: SDK
runs-on: ubuntu-latest
needs: generate_matrix
strategy:
fail-fast: False
matrix: ${{fromJson(needs.generate_matrix.outputs.sdks)}}
steps:
- name: Login to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Docker.io Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Login to Quay.io Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: quay.io
username: ${{ secrets.QUAY_USER }}
password: ${{ secrets.QUAY_TOKEN }}
- name: Docker meta (tag or branch)
id: meta_ref
uses: docker/metadata-action@v4
with:
images: |
ghcr.io/openwrt/sdk
docker.io/openwrt/sdk
quay.io/openwrt/sdk
flavor: |
latest=false
suffix=-${{ needs.generate_matrix.outputs.ref }}
tags: ${{ matrix.tags }}
- name: Docker meta (version)
id: meta_version
uses: docker/metadata-action@v4
with:
images: |
ghcr.io/openwrt/sdk
docker.io/openwrt/sdk
quay.io/openwrt/sdk
flavor: |
latest=false
suffix=-${{ needs.generate_matrix.outputs.version }}
tags: ${{ matrix.tags }}
- name: Docker meta (master)
if: needs.generate_matrix.outputs.ref == 'main'
id: meta_master
uses: docker/metadata-action@v4
with:
images: |
ghcr.io/openwrt/sdk
docker.io/openwrt/sdk
quay.io/openwrt/sdk
flavor: |
latest=false
suffix=-master
tags: ${{ matrix.tags }}
- name: Docker meta (target and arch)
if: needs.generate_matrix.outputs.version == 'SNAPSHOT'
id: meta_target_arch
uses: docker/metadata-action@v4
with:
images: |
ghcr.io/openwrt/sdk
docker.io/openwrt/sdk
quay.io/openwrt/sdk
flavor: |
latest=false
tags: ${{ matrix.tags }}
- name: Build and push
uses: docker/build-push-action@v3
with:
push: ${{ github.event_name != 'pull_request' }}
tags: |
${{ steps.meta_ref.outputs.tags }}
${{ steps.meta_version.outputs.tags }}
${{ steps.meta_master.outputs.tags }}
${{ steps.meta_target_arch.outputs.tags }}
build-args: |
DOWNLOAD_FILE=sdk-.*.Linux-x86_64.tar.xz
VERSION_PATH=${{ needs.generate_matrix.outputs.version_path }}
TARGET=${{ matrix.target }}
labels: ${{ steps.meta_ref.outputs.labels }}
push-rootfs-container:
name: RootFS
runs-on: ubuntu-latest
needs: generate_matrix
strategy:
fail-fast: False
matrix: ${{fromJson(needs.generate_matrix.outputs.rootfs)}}
steps:
- name: Set up QEMU
run: |
sudo apt-get update
sudo apt-get install -y qemu-user-static binfmt-support
sudo update-binfmts --import
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Docker.io Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Login to Quay.io Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: quay.io
username: ${{ secrets.QUAY_USER }}
password: ${{ secrets.QUAY_TOKEN }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: |
ghcr.io/openwrt/rootfs
docker.io/openwrt/rootfs
quay.io/openwrt/rootfs
flavor: latest=false
tags: |
${{ matrix.target }}-${{ needs.generate_matrix.outputs.ref }}
${{ matrix.target }}-${{ needs.generate_matrix.outputs.version }}
${{ matrix.target }}-master,enable=${{ needs.generate_matrix.outputs.ref == 'main' }}
${{ matrix.target }},enable=${{ needs.generate_matrix.outputs.version == 'SNAPSHOT' }}
${{ matrix.arch }}-${{ needs.generate_matrix.outputs.ref }}
${{ matrix.arch }}-${{ needs.generate_matrix.outputs.version }}
${{ matrix.arch }}-master,enable=${{ needs.generate_matrix.outputs.ref == 'main' }}
${{ matrix.arch }},enable=${{ needs.generate_matrix.outputs.version == 'SNAPSHOT' }}
latest,enable=${{ needs.generate_matrix.outputs.version == 'SNAPSHOT' && matrix.target == 'x86/64'}}
- name: Generate build args
id: build_args
run: |
echo 'args<<EOF
DOWNLOAD_FILE=openwrt-.*-rootfs.tar.gz
WORKDIR=/
USER=root
VERSION_PATH=${{ needs.generate_matrix.outputs.version_path }}
TARGET=${{ matrix.target }}
BASE_IMAGE=scratch
CMD=ash
EOF' >> $GITHUB_OUTPUT
- name: Build
id: build
uses: docker/build-push-action@v3
with:
push: false
tags: ${{ steps.meta.outputs.tags }}
load: true
build-args: ${{ steps.build_args.outputs.args }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/${{ matrix.arch }}
- name: Smoke test
run: docker run --platform=linux/${{ matrix.arch }} ${{ steps.build.outputs.imageid }} uname -m
- name: Push
if: github.event_name != 'pull_request'
uses: docker/build-push-action@v3
with:
push: true
tags: ${{ steps.meta.outputs.tags }}
build-args: ${{ steps.build_args.outputs.args }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/${{ matrix.arch }}