|
| 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 | + - "generate_sandbox_dockerfile.py" |
| 16 | + - "generate_zenml_project.py" |
| 17 | + - "LICENSE" |
| 18 | + - "pyproject.toml" |
| 19 | + - "README.md" |
| 20 | + |
| 21 | + workflow_dispatch: |
| 22 | + inputs: |
| 23 | + project: |
| 24 | + description: "Project to build (leave empty to detect from changed files)" |
| 25 | + required: false |
| 26 | + default: "" |
| 27 | + |
| 28 | +jobs: |
| 29 | + detect-changes: |
| 30 | + runs-on: ubuntu-latest |
| 31 | + outputs: |
| 32 | + matrix: ${{ steps.set-matrix.outputs.matrix }} |
| 33 | + steps: |
| 34 | + - name: Checkout code |
| 35 | + uses: actions/checkout@v3 |
| 36 | + with: |
| 37 | + fetch-depth: 2 |
| 38 | + |
| 39 | + - name: Detect changed projects |
| 40 | + id: set-matrix |
| 41 | + run: | |
| 42 | + if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then |
| 43 | + if [ -n "${{ github.event.inputs.project }}" ]; then |
| 44 | + # Use the manually specified project |
| 45 | + PROJECTS="[\"${{ github.event.inputs.project }}\"]" |
| 46 | + else |
| 47 | + # Auto-detect changed files (same logic as push events) |
| 48 | + CHANGED_FILES=$(git diff --name-only HEAD^ HEAD) |
| 49 | + CHANGED_DIRS=$(echo "$CHANGED_FILES" | grep -o "^[^/]*" | sort -u | grep -v "^$") |
| 50 | + PROJECTS_WITH_DOCKERFILE=$(find . -maxdepth 2 -name "Dockerfile.sandbox" -not -path "*/\.*" | xargs dirname | xargs basename -a) |
| 51 | + PROJECTS="[" |
| 52 | + COMMA="" |
| 53 | + for DIR in $CHANGED_DIRS; do |
| 54 | + if echo "$PROJECTS_WITH_DOCKERFILE" | grep -q "^$DIR$"; then |
| 55 | + PROJECTS="${PROJECTS}${COMMA}\"${DIR}\"" |
| 56 | + COMMA="," |
| 57 | + fi |
| 58 | + done |
| 59 | + PROJECTS="${PROJECTS}]" |
| 60 | + fi |
| 61 | + else |
| 62 | + # Original push event logic |
| 63 | + CHANGED_FILES=$(git diff --name-only HEAD^ HEAD) |
| 64 | + CHANGED_DIRS=$(echo "$CHANGED_FILES" | grep -o "^[^/]*" | sort -u | grep -v "^$") |
| 65 | + PROJECTS_WITH_DOCKERFILE=$(find . -maxdepth 2 -name "Dockerfile.sandbox" -not -path "*/\.*" | xargs dirname | xargs basename -a) |
| 66 | + PROJECTS="[" |
| 67 | + COMMA="" |
| 68 | + for DIR in $CHANGED_DIRS; do |
| 69 | + if echo "$PROJECTS_WITH_DOCKERFILE" | grep -q "^$DIR$"; then |
| 70 | + PROJECTS="${PROJECTS}${COMMA}\"${DIR}\"" |
| 71 | + COMMA="," |
| 72 | + fi |
| 73 | + done |
| 74 | + PROJECTS="${PROJECTS}]" |
| 75 | + fi |
| 76 | + echo "matrix=$PROJECTS" >> $GITHUB_OUTPUT |
| 77 | + echo "Projects to build: $PROJECTS" |
| 78 | +
|
| 79 | + build-and-push: |
| 80 | + needs: detect-changes |
| 81 | + runs-on: ubuntu-latest |
| 82 | + strategy: |
| 83 | + matrix: |
| 84 | + project: ${{ fromJson(needs.detect-changes.outputs.matrix) }} |
| 85 | + steps: |
| 86 | + - name: Checkout code |
| 87 | + uses: actions/checkout@v3 |
| 88 | + |
| 89 | + - name: Check for Dockerfile.sandbox |
| 90 | + id: check-dockerfile |
| 91 | + run: | |
| 92 | + if [ -f "${{ matrix.project }}/Dockerfile.sandbox" ]; then |
| 93 | + echo "dockerfile_exists=true" >> $GITHUB_OUTPUT |
| 94 | + else |
| 95 | + echo "dockerfile_exists=false" >> $GITHUB_OUTPUT |
| 96 | + echo "No Dockerfile.sandbox found in ${{ matrix.project }}, skipping." |
| 97 | + fi |
| 98 | +
|
| 99 | + - name: Set up Docker Buildx |
| 100 | + if: steps.check-dockerfile.outputs.dockerfile_exists == 'true' |
| 101 | + uses: docker/setup-buildx-action@v2 |
| 102 | + |
| 103 | + - name: Login to DockerHub |
| 104 | + if: steps.check-dockerfile.outputs.dockerfile_exists == 'true' |
| 105 | + uses: docker/login-action@v2 |
| 106 | + with: |
| 107 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 108 | + password: ${{ secrets.DOCKERHUB_PASSWORD}} |
| 109 | + |
| 110 | + - name: Build and push |
| 111 | + if: steps.check-dockerfile.outputs.dockerfile_exists == 'true' |
| 112 | + uses: docker/build-push-action@v4 |
| 113 | + with: |
| 114 | + context: . |
| 115 | + file: ${{ matrix.project }}/Dockerfile.sandbox |
| 116 | + push: true |
| 117 | + tags: | |
| 118 | + safoinext/zenml-sandbox-${{ matrix.project }}:latest |
| 119 | + cache-from: type=gha |
| 120 | + cache-to: type=gha,mode=max |
0 commit comments