Feat/first nib images #8
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: write | |
pull-requests: read | |
id-token: write | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
detect-changes: | |
runs-on: ubuntu-latest | |
outputs: | |
base_matrix: ${{ steps.set-matrix.outputs.base_matrix }} | |
clients_matrix: ${{ steps.set-matrix.outputs.clients_matrix }} | |
protocols_matrix: ${{ steps.set-matrix.outputs.protocols_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 matrices | |
id: set-matrix | |
run: | | |
# Get list of changed Dockerfiles and convert to newline-separated | |
CHANGED_FILES=$(echo "${{ steps.changed-files.outputs.all_changed_files }}" | tr ' ' '\n') | |
# Initialize empty arrays for each group | |
BASE_MATRIX='{"include":[]}' | |
CLIENTS_MATRIX='{"include":[]}' | |
PROTOCOLS_MATRIX='{"include":[]}' | |
# Process each changed Dockerfile | |
while IFS= read -r file; do | |
if [ -z "$file" ]; then | |
continue | |
fi | |
# Get directory containing Dockerfile | |
DIR=$(dirname "$file") | |
# Add to appropriate matrix | |
if [[ "$DIR" == "node-base" ]]; then | |
BASE_MATRIX=$(echo $BASE_MATRIX | jq --arg path "$DIR" '.include += [{"image_path": $path}]') | |
elif [[ "$DIR" == clients/* ]]; then | |
CLIENTS_MATRIX=$(echo $CLIENTS_MATRIX | jq --arg path "$DIR" '.include += [{"image_path": $path}]') | |
else | |
PROTOCOLS_MATRIX=$(echo $PROTOCOLS_MATRIX | jq --arg path "$DIR" '.include += [{"image_path": $path}]') | |
fi | |
done <<< "$CHANGED_FILES" | |
echo "base_matrix=$BASE_MATRIX" >> $GITHUB_OUTPUT | |
echo "clients_matrix=$CLIENTS_MATRIX" >> $GITHUB_OUTPUT | |
echo "protocols_matrix=$PROTOCOLS_MATRIX" >> $GITHUB_OUTPUT | |
build-base: | |
needs: detect-changes | |
if: ${{ fromJson(needs.detect-changes.outputs.base_matrix).include[0] }} | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: ${{ fromJson(needs.detect-changes.outputs.base_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: | | |
SHA=$(git rev-parse --short HEAD) | |
echo "image_name=node-base" >> $GITHUB_OUTPUT | |
echo "image_tag=${SHA}" >> $GITHUB_OUTPUT | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Build and push base image | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./${{ matrix.image_path }} | |
push: true | |
build-args: | | |
GRAFANA_LOKI_API_KEY=${{ secrets.GRAFANA_LOKI_API_KEY }} | |
GRAFANA_PROM_API_KEY=${{ secrets.GRAFANA_PROM_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 }} | |
build-clients: | |
needs: [detect-changes, build-base] | |
if: | | |
always() && | |
needs.build-base.result != 'failure' && | |
fromJson(needs.detect-changes.outputs.clients_matrix).include[0] | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: ${{ fromJson(needs.detect-changes.outputs.clients_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: | | |
SHA=$(git rev-parse --short HEAD) | |
CLIENT_NAME=$(basename ${{ matrix.image_path }}) | |
IMAGE_NAME="blockjoy-${CLIENT_NAME}" | |
if [[ -f "${{ matrix.image_path }}/Dockerfile" ]]; then | |
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}-${SHA}" | |
else | |
IMAGE_TAG="${SHA}" | |
fi | |
else | |
IMAGE_TAG="${SHA}" | |
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 client images | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./${{ matrix.image_path }} | |
push: true | |
build-args: | | |
GRAFANA_LOKI_BASICAUTH=${{ secrets.GRAFANA_LOKI_BASICAUTH }} | |
GRAFANA_PROM_BASICAUTH=${{ secrets.GRAFANA_PROM_BASICAUTH }} | |
CLOUDFLARE_API_KEY=${{ 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 }} | |
build-protocols: | |
needs: [detect-changes, build-clients] | |
if: | | |
always() && | |
needs.build-clients.result != 'failure' && | |
fromJson(needs.detect-changes.outputs.protocols_matrix).include[0] | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: ${{ fromJson(needs.detect-changes.outputs.protocols_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: | | |
SHA=$(git rev-parse --short HEAD) | |
IMAGE_NAME=$(basename $(dirname ${{ matrix.image_path }}))"-"$(basename ${{ matrix.image_path }}) | |
echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT | |
echo "image_tag=${SHA}" >> $GITHUB_OUTPUT | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Build and push protocol images | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./${{ matrix.image_path }} | |
push: true | |
build-args: | | |
GRAFANA_LOKI_BASICAUTH=${{ secrets.GRAFANA_LOKI_BASICAUTH }} | |
GRAFANA_PROM_BASICAUTH=${{ secrets.GRAFANA_PROM_BASICAUTH }} | |
CLOUDFLARE_API_KEY=${{ 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 }} |