Docker Build & Push #36
This file contains hidden or 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 & Push | |
| on: | |
| workflow_dispatch: | |
| schedule: | |
| - cron: '20 2 * * 0' | |
| env: | |
| DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USERNAME }} | |
| DOCKERHUB_REPO_SUFFIX: "dev" | |
| jobs: | |
| discover: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| matrix: ${{ steps.set-matrix.outputs.matrix }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - id: set-matrix | |
| run: | | |
| # Find all Dockerfiles in non-hidden directories | |
| find . -type f \( -name "*.Dockerfile" -o -name "Dockerfile" \) \ | |
| | grep -v '/\.[^/]*/' > dockerfiles.txt | |
| # Generate platform matrix | |
| echo "[" > matrix.json | |
| while IFS= read -r file; do | |
| dir=$(dirname "$file") | |
| platforms="linux/amd64" | |
| # Check for .platform file | |
| if [ -f "$dir/.platform" ]; then | |
| platforms=$(grep -vE '^(#|$)' "$dir/.platform" | tr '\n' ',' | sed 's/,*$//') | |
| [ -z "$platforms" ] && platforms="linux/amd64" | |
| fi | |
| # Generate JSON entry | |
| jq -n \ | |
| --arg file "$file" \ | |
| --arg platforms "$platforms" \ | |
| '{file: $file, platforms: $platforms}' >> matrix.json | |
| echo "," >> matrix.json | |
| done < dockerfiles.txt | |
| # Fix JSON format | |
| sed -i '$d' matrix.json # Remove trailing comma | |
| echo "]" >> matrix.json | |
| jq -c 'map(select(.file != null))' matrix.json > matrix-clean.json | |
| echo "matrix={\"include\":$(cat matrix-clean.json)}" >> $GITHUB_OUTPUT | |
| build-push: | |
| needs: discover | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: ${{ fromJson(needs.discover.outputs.matrix) }} | |
| fail-fast: false | |
| max-parallel: 4 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Generate image metadata | |
| id: tags | |
| run: | | |
| file="${{ matrix.file }}" | |
| dir_path=$(dirname "$file") | |
| folder=$(basename "$dir_path") | |
| sanitized_folder=$(echo "$folder" | tr '[:upper:]' '[:lower:]' | tr -cd '[:alnum:]-_') | |
| repo_name="${sanitized_folder}-${DOCKERHUB_REPO_SUFFIX}" | |
| tag=$(basename "$file" | sed 's/\.[^.]*$//; s/Dockerfile/latest/; s/[^a-zA-Z0-9_.-]/-/g') | |
| echo "dir_path=$dir_path" >> $GITHUB_OUTPUT | |
| echo "image_name=$DOCKERHUB_USER/$repo_name" >> $GITHUB_OUTPUT | |
| echo "tag=$tag" >> $GITHUB_OUTPUT | |
| - uses: docker/setup-buildx-action@v3 | |
| with: | |
| platforms: ${{ matrix.platforms }} | |
| - uses: docker/login-action@v3 | |
| with: | |
| username: ${{ env.DOCKERHUB_USER }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - uses: docker/build-push-action@v6 | |
| with: | |
| context: ${{ steps.tags.outputs.dir_path }} | |
| file: ${{ matrix.file }} | |
| platforms: ${{ matrix.platforms }} | |
| tags: | | |
| ${{ steps.tags.outputs.image_name }}:${{ steps.tags.outputs.tag }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| push: true |