Feat/first nib images #2
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 Build | |
on: | |
pull_request: | |
branches: [ main ] | |
paths: | |
- '**/Dockerfile' | |
permissions: | |
contents: read | |
packages: read | |
pull-requests: read | |
id-token: write | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
detect-changes: | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.set-matrix.outputs.matrix }} | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Get changed Dockerfiles | |
id: changed-files | |
uses: tj-actions/changed-files@v40 | |
with: | |
files: | | |
**/Dockerfile | |
- name: Generate build matrix | |
id: set-matrix | |
run: | | |
# Get list of changed Dockerfiles | |
CHANGED_FILES="${{ steps.changed-files.outputs.all_changed_files }}" | |
# Order: node-base first, then clients, then protocols | |
MATRIX_DIRS=() | |
# Process each changed Dockerfile | |
while IFS= read -r file; do | |
if [ -z "$file" ]; then | |
continue | |
fi | |
# Get directory containing Dockerfile | |
DIR=$(dirname "$file") | |
# Store based on priority | |
if [[ "$DIR" == "node-base" ]]; then | |
# node-base goes first if changed | |
MATRIX_DIRS=("node-base" "${MATRIX_DIRS[@]}") | |
elif [[ "$DIR" == clients/* ]]; then | |
# Add client dirs after node-base | |
if [[ " ${MATRIX_DIRS[@]} " =~ " node-base " ]]; then | |
MATRIX_DIRS+=("$DIR") | |
else | |
MATRIX_DIRS=("$DIR" "${MATRIX_DIRS[@]}") | |
fi | |
else | |
# Add protocol dirs last | |
MATRIX_DIRS+=("$DIR") | |
fi | |
done <<< "$CHANGED_FILES" | |
# Convert to JSON array | |
if [ ${#MATRIX_DIRS[@]} -eq 0 ]; then | |
echo "matrix=[]" >> $GITHUB_OUTPUT | |
else | |
printf '%s\n' "${MATRIX_DIRS[@]}" | jq -R . | jq -s . > matrix.json | |
echo "matrix=$(cat matrix.json)" >> $GITHUB_OUTPUT | |
fi | |
build: | |
needs: detect-changes | |
if: ${{ needs.detect-changes.outputs.matrix != '[]' }} | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
image_path: ${{ fromJson(needs.detect-changes.outputs.matrix) }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Log in to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Generate version | |
id: version | |
run: | | |
if [[ "${{ matrix.image_path }}" == "node-base" ]]; then | |
IMAGE_NAME="node-base" | |
IMAGE_TAG="$(git rev-parse --short HEAD)" | |
elif [[ "${{ matrix.image_path }}" == clients/* ]]; then | |
CLIENT_NAME=$(basename ${{ matrix.image_path }}) | |
IMAGE_NAME="blockjoy-${CLIENT_NAME}" | |
if [[ -f "${{ matrix.image_path }}/Dockerfile" ]]; then | |
# Extract client version from Dockerfile (e.g., ERIGON_VERSION, RETH_VERSION) | |
CLIENT_VERSION=$(grep -E "ENV.*_VERSION=[[:space:]]*v?[0-9]+\.[0-9]+\.[0-9]+[-.a-zA-Z0-9]*" "${{ matrix.image_path }}/Dockerfile" | grep -oE "v?[0-9]+\.[0-9]+\.[0-9]+[-.a-zA-Z0-9]*") | |
if [[ ! -z "$CLIENT_VERSION" ]]; then | |
CLIENT_VERSION=${CLIENT_VERSION#v} | |
IMAGE_TAG="${CLIENT_VERSION}-$(git rev-parse --short HEAD)" | |
else | |
IMAGE_TAG="$(git rev-parse --short HEAD)" | |
fi | |
else | |
IMAGE_TAG="$(git rev-parse --short HEAD)" | |
fi | |
else | |
IMAGE_NAME=$(echo ${{ matrix.image_path }} | tr '/' '-') | |
IMAGE_TAG="$(git rev-parse --short HEAD)" | |
fi | |
echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT | |
echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Build and push images | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./${{ matrix.image_path }} | |
push: true | |
build-args: | | |
${{ matrix.image_path == 'node-base' && format('GRAFANA_LOKI_API_KEY={0} | |
GRAFANA_PROM_API_KEY={1}', secrets.GRAFANA_LOKI_API_KEY, secrets.GRAFANA_PROM_API_KEY) || '' }} | |
${{ (startsWith(matrix.image_path, 'clients/') || !startsWith(matrix.image_path, 'node-base/')) && format('GRAFANA_LOKI_BASICAUTH={0} | |
GRAFANA_PROM_BASICAUTH={1} | |
CLOUDFLARE_API_KEY={2}', secrets.GRAFANA_LOKI_BASICAUTH, secrets.GRAFANA_PROM_BASICAUTH, secrets.CLOUDFLARE_API_KEY) || '' }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
tags: ghcr.io/blockjoy/${{ steps.version.outputs.image_name }}:${{ steps.version.outputs.image_tag }} |