|
| 1 | +name: Build and Push Project Sandbox Images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths-ignore: |
| 8 | + - "_assets/**" |
| 9 | + - ".github/**" |
| 10 | + - ".gitignore" |
| 11 | + - ".gitmodules" |
| 12 | + - ".typos.toml" |
| 13 | + - "CODE-OF-CONDUCT.md" |
| 14 | + - "CONTRIBUTING.md" |
| 15 | + - "scripts/**" |
| 16 | + - "LICENSE" |
| 17 | + - "pyproject.toml" |
| 18 | + - "README.md" |
| 19 | + |
| 20 | + workflow_dispatch: |
| 21 | + inputs: |
| 22 | + project: |
| 23 | + description: "Project to build (leave empty to detect from changed files)" |
| 24 | + required: false |
| 25 | + default: "" |
| 26 | + |
| 27 | +jobs: |
| 28 | + detect-changes: |
| 29 | + runs-on: ubuntu-latest |
| 30 | + outputs: |
| 31 | + matrix: ${{ steps.set-matrix.outputs.matrix }} |
| 32 | + steps: |
| 33 | + - name: Checkout code |
| 34 | + uses: actions/checkout@v3 |
| 35 | + with: |
| 36 | + fetch-depth: 2 |
| 37 | + |
| 38 | + - name: Detect changed projects |
| 39 | + id: set-matrix |
| 40 | + run: | |
| 41 | + if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then |
| 42 | + if [ -n "${{ github.event.inputs.project }}" ]; then |
| 43 | + PROJECTS="[\"${{ github.event.inputs.project }}\"]" |
| 44 | + else |
| 45 | + # Auto-detect changed files (same logic as push events) |
| 46 | + CHANGED_FILES=$(git diff --name-only HEAD^ HEAD) |
| 47 | + CHANGED_DIRS=$(echo "$CHANGED_FILES" | grep -o "^[^/]*" | sort -u | grep -v "^$") |
| 48 | + ALL_PROJECT_DIRS=$(find . -maxdepth 1 -type d -not -path "*/\.*" -not -path "." | sed 's|^\./||' | grep -v "^_") |
| 49 | + PROJECTS="[" |
| 50 | + COMMA="" |
| 51 | + for DIR in $CHANGED_DIRS; do |
| 52 | + if echo "$ALL_PROJECT_DIRS" | grep -q "^$DIR$"; then |
| 53 | + PROJECTS="${PROJECTS}${COMMA}\"${DIR}\"" |
| 54 | + COMMA="," |
| 55 | + fi |
| 56 | + done |
| 57 | + PROJECTS="${PROJECTS}]" |
| 58 | + fi |
| 59 | + else |
| 60 | + CHANGED_FILES=$(git diff --name-only HEAD^ HEAD) |
| 61 | + CHANGED_DIRS=$(echo "$CHANGED_FILES" | grep -o "^[^/]*" | sort -u | grep -v "^$") |
| 62 | + ALL_PROJECT_DIRS=$(find . -maxdepth 1 -type d -not -path "*/\.*" -not -path "." | sed 's|^\./||' | grep -v "^_") |
| 63 | + PROJECTS="[" |
| 64 | + COMMA="" |
| 65 | + for DIR in $CHANGED_DIRS; do |
| 66 | + if echo "$ALL_PROJECT_DIRS" | grep -q "^$DIR$"; then |
| 67 | + PROJECTS="${PROJECTS}${COMMA}\"${DIR}\"" |
| 68 | + COMMA="," |
| 69 | + fi |
| 70 | + done |
| 71 | + PROJECTS="${PROJECTS}]" |
| 72 | + fi |
| 73 | + echo "matrix=$PROJECTS" >> $GITHUB_OUTPUT |
| 74 | + echo "Projects to build: $PROJECTS" |
| 75 | +
|
| 76 | + build-and-push: |
| 77 | + needs: detect-changes |
| 78 | + runs-on: ubuntu-latest |
| 79 | + strategy: |
| 80 | + matrix: |
| 81 | + project: ${{ fromJson(needs.detect-changes.outputs.matrix) }} |
| 82 | + steps: |
| 83 | + - name: Checkout code |
| 84 | + uses: actions/checkout@v3 |
| 85 | + with: |
| 86 | + fetch-depth: 0 |
| 87 | + |
| 88 | + - name: Check for Dockerfile.sandbox |
| 89 | + id: check-dockerfile |
| 90 | + run: | |
| 91 | + if [ -f "${{ matrix.project }}/Dockerfile.sandbox" ]; then |
| 92 | + echo "dockerfile_exists=true" >> $GITHUB_OUTPUT |
| 93 | + else |
| 94 | + echo "dockerfile_exists=false" >> $GITHUB_OUTPUT |
| 95 | + echo "No Dockerfile.sandbox found in ${{ matrix.project }}, will generate one." |
| 96 | + fi |
| 97 | +
|
| 98 | + - name: Set up Python |
| 99 | + if: steps.check-dockerfile.outputs.dockerfile_exists == 'false' |
| 100 | + uses: actions/setup-python@v4 |
| 101 | + with: |
| 102 | + python-version: "3.10" |
| 103 | + |
| 104 | + - name: Generate Dockerfile.sandbox if needed |
| 105 | + if: steps.check-dockerfile.outputs.dockerfile_exists == 'false' |
| 106 | + id: generate-dockerfile |
| 107 | + run: | |
| 108 | + python generate_sandbox_dockerfile.py "${{ matrix.project }}" |
| 109 | + echo "Generated Dockerfile.sandbox for ${{ matrix.project }}" |
| 110 | +
|
| 111 | + - name: Create Pull Request for new Dockerfile |
| 112 | + if: steps.check-dockerfile.outputs.dockerfile_exists == 'false' |
| 113 | + uses: peter-evans/create-pull-request@v5 |
| 114 | + with: |
| 115 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 116 | + commit-message: "Auto-generate Dockerfile.sandbox for ${{ matrix.project }}" |
| 117 | + title: "Auto-generate Dockerfile.sandbox for ${{ matrix.project }}" |
| 118 | + body: | |
| 119 | + This PR adds a generated Dockerfile.sandbox for the ${{ matrix.project }} project. |
| 120 | +
|
| 121 | + Please review the changes and merge if they look good. |
| 122 | +
|
| 123 | + Once merged, the Docker image will be built and pushed automatically. |
| 124 | + branch: "auto-dockerfile-${{ matrix.project }}" |
| 125 | + base: main |
| 126 | + labels: | |
| 127 | + automated-pr |
| 128 | + dockerfile |
| 129 | + sandbox |
| 130 | +
|
| 131 | + # Only build and push if Dockerfile already exists |
| 132 | + - name: Set up Docker Buildx |
| 133 | + if: steps.check-dockerfile.outputs.dockerfile_exists == 'true' |
| 134 | + uses: docker/setup-buildx-action@v2 |
| 135 | + |
| 136 | + - name: Login to DockerHub |
| 137 | + if: steps.check-dockerfile.outputs.dockerfile_exists == 'true' |
| 138 | + uses: docker/login-action@v2 |
| 139 | + with: |
| 140 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 141 | + password: ${{ secrets.DOCKERHUB_PASSWORD }} |
| 142 | + |
| 143 | + - name: Build and push |
| 144 | + if: steps.check-dockerfile.outputs.dockerfile_exists == 'true' |
| 145 | + uses: docker/build-push-action@v4 |
| 146 | + with: |
| 147 | + context: . |
| 148 | + file: ${{ matrix.project }}/Dockerfile.sandbox |
| 149 | + push: true |
| 150 | + tags: | |
| 151 | + zenmldocker/projects-${{ matrix.project }}:latest |
| 152 | + cache-from: type=gha |
| 153 | + cache-to: type=gha,mode=max |
0 commit comments