add kubectl to dockerfile #9
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: Dynamic Service Docker Builds | |
on: | |
push: | |
branches: | |
- main | |
paths-ignore: | |
- "deployment/**" | |
- "gef-portal-scraper/**" | |
- "**.md" | |
pull_request: | |
branches: | |
- main | |
paths-ignore: | |
- "deployment/**" | |
- "gef-portal-scraper/**" | |
- "**.md" | |
jobs: | |
detect_changes: | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.set-matrix.outputs.matrix }} | |
steps: | |
- name: Check out repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Get changed files | |
id: changed-files | |
uses: tj-actions/changed-files@v40 | |
- name: Display all changed files | |
run: | | |
echo "π Changed files in this push/PR:" | |
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do | |
echo " β $file" | |
done | |
- name: Set up build matrix | |
id: set-matrix | |
run: | | |
# Initialize variables | |
MATRIX="[" | |
SEPARATOR="" | |
# List of directories to exclude (space-separated) | |
EXCLUDED_DIRS="deployment resources gef-portal-scraper" | |
echo "π Changed files in this push/PR:" | |
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do | |
echo " β $file" | |
done | |
# Find all Dockerfiles in service directories | |
for dockerfile in $(find . -name "Dockerfile" -not -path "*/\.*"); do | |
# Get service directory (parent of Dockerfile) | |
service_dir=$(dirname "$dockerfile") | |
service_name=$(basename "$service_dir") | |
echo "π¦ Checking service: $service_name" | |
echo " β’ Directory: $service_dir" | |
# Skip if directory is in excluded list | |
skip=false | |
for excluded in $EXCLUDED_DIRS; do | |
if [[ "$service_dir" == *"$excluded"* ]]; then | |
skip=true | |
echo "βοΈ Skipping excluded directory: $service_dir" | |
break | |
fi | |
done | |
[ "$skip" = true ] && continue | |
# Check for changes in service directory | |
CHANGED=false | |
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do | |
# Remove leading ./ from paths for consistent comparison | |
clean_file=$(echo "$file" | sed 's|^\./||') | |
clean_dir=$(echo "$service_dir" | sed 's|^\./||') | |
echo " β’ Comparing changed file: $clean_file" | |
echo " β’ With service dir: $clean_dir" | |
if [[ "$clean_file" == "$clean_dir"* ]]; then | |
CHANGED=true | |
echo "β¨ Found changes in service: $service_name" | |
echo " Changed file: $clean_file" | |
break | |
fi | |
done | |
# If changes detected, add to build matrix | |
if [ "$CHANGED" = true ]; then | |
echo "π¨ Adding $service_name to build matrix" | |
# Remove leading ./ from context path | |
context_path=$(echo "$service_dir" | sed 's|^\./||') | |
MATRIX="${MATRIX}${SEPARATOR}{\"service\": \"${service_name}\", \"context\": \"${context_path}\"}" | |
SEPARATOR="," | |
else | |
echo "βοΈ No changes detected for $service_name - skipping" | |
fi | |
done | |
MATRIX="${MATRIX}]" | |
echo "π Final build matrix:" | |
echo "$MATRIX" | jq '.' | |
if [ "$MATRIX" = "[]" ]; then | |
echo "β οΈ No services to build" | |
echo "matrix=[]" >> $GITHUB_OUTPUT | |
else | |
echo "π Services ready for build" | |
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT | |
fi | |
build_and_push: | |
needs: detect_changes | |
if: ${{ needs.detect_changes.outputs.matrix != '[]' }} | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
strategy: | |
matrix: | |
include: ${{ fromJson(needs.detect_changes.outputs.matrix) }} | |
steps: | |
- name: Check out repository | |
uses: actions/checkout@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Log in to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Define lowercase repository owner | |
id: repo | |
run: | | |
echo "REPO_OWNER_LC=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV | |
- name: Display build information | |
run: | | |
echo "ποΈ Building service: ${{ matrix.service }}" | |
echo " β’ Context: ${{ matrix.context }}" | |
echo " β’ Image name: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.service }}:latest" | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v4 | |
with: | |
context: ./${{ matrix.context }} | |
file: ./${{ matrix.context }}/Dockerfile | |
push: true | |
tags: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.service }}:latest | |
- name: Build status | |
run: | | |
echo "β Successfully built and pushed: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.service }}:latest" |