Merge pull request #196 from zenml-io/add-sandbox-dockerfile-generato… #1
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: Build and Push Project Codespace Images | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths-ignore: | |
| - "_assets/**" | |
| - ".github/**" | |
| - ".gitignore" | |
| - ".gitmodules" | |
| - ".typos.toml" | |
| - "CODE-OF-CONDUCT.md" | |
| - "CONTRIBUTING.md" | |
| - "scripts/**" | |
| - "LICENSE" | |
| - "pyproject.toml" | |
| - "README.md" | |
| workflow_dispatch: | |
| inputs: | |
| project: | |
| description: "Project to build (leave empty to detect from changed files)" | |
| required: false | |
| default: "" | |
| jobs: | |
| detect-changes: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| matrix: ${{ steps.set-matrix.outputs.matrix }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 2 | |
| - name: Detect changed projects | |
| id: set-matrix | |
| run: | | |
| # If this was a manual dispatch _and_ they provided a project, just use that | |
| if [[ "${{ github.event_name }}" == "workflow_dispatch" && -n "${{ github.event.inputs.project }}" ]]; then | |
| PROJECTS="[\"${{ github.event.inputs.project }}\"]" | |
| else | |
| # Otherwise auto-diff HEAD^ → HEAD for any changed top-level dirs | |
| CHANGED_FILES=$(git diff --name-only HEAD^ HEAD) | |
| CHANGED_DIRS=$(echo "$CHANGED_FILES" \ | |
| | awk -F/ '{print $1}' \ | |
| | sort -u \ | |
| | grep -v '^$') | |
| ALL_PROJECT_DIRS=$(find . -maxdepth 1 -type d \ | |
| -not -path '*/\.*' \ | |
| -not -path '.' \ | |
| | sed 's|^\./||' \ | |
| | grep -v '^_') | |
| PROJECTS="[" | |
| sep="" | |
| for d in $CHANGED_DIRS; do | |
| if echo "$ALL_PROJECT_DIRS" | grep -qx "$d"; then | |
| PROJECTS+="${sep}\"$d\"" | |
| sep="," | |
| fi | |
| done | |
| PROJECTS+="]" | |
| fi | |
| echo "matrix=$PROJECTS" >> $GITHUB_OUTPUT | |
| echo "Projects to build: $PROJECTS" | |
| check-dockerfile: | |
| needs: detect-changes | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| project: ${{ fromJson(needs.detect-changes.outputs.matrix) }} | |
| outputs: | |
| dockerfile_exists: ${{ steps.check-dockerfile.outputs.dockerfile_exists }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 | |
| - name: Check for Dockerfile.codespace | |
| id: check-dockerfile | |
| run: | | |
| if [ -f "${{ matrix.project }}/Dockerfile.codespace" ]; then | |
| echo "dockerfile_exists=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "dockerfile_exists=false" >> $GITHUB_OUTPUT | |
| fi | |
| generate-dockerfile: | |
| needs: [detect-changes, check-dockerfile] | |
| if: needs.check-dockerfile.outputs.dockerfile_exists == 'false' | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| project: ${{ fromJson(needs.detect-changes.outputs.matrix) }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: "3.10" | |
| - name: Generate Dockerfile.codespace | |
| id: generate-dockerfile | |
| run: | | |
| python scripts/generate_codespace_dockerfile.py "${{ matrix.project }}" | |
| echo "Generated Dockerfile.codespace for ${{ matrix.project }}" | |
| - name: Create Pull Request for new Dockerfile | |
| uses: peter-evans/create-pull-request@v5 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| commit-message: "Auto-generate Dockerfile.codespace for ${{ matrix.project }}" | |
| title: "Auto-generate Dockerfile.codespace for ${{ matrix.project }}" | |
| body: | | |
| This PR adds a generated Dockerfile.codespace for the ${{ matrix.project }} project. | |
| Please review the changes and merge if they look good. | |
| Once merged, the Docker image will be built and pushed automatically. | |
| branch: "auto-dockerfile-${{ matrix.project }}" | |
| base: main | |
| labels: | | |
| automated-pr | |
| dockerfile | |
| codespace | |
| build-and-push: | |
| needs: [detect-changes, check-dockerfile] | |
| if: needs.check-dockerfile.outputs.dockerfile_exists == 'true' | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| project: ${{ fromJson(needs.detect-changes.outputs.matrix) }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 | |
| # Generate timestamp for image tag | |
| - name: Generate timestamp | |
| id: timestamp | |
| run: echo "timestamp=$(date -u +'%Y%m%d%H%M%S')" >> $GITHUB_OUTPUT | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v2 | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
| - name: Build and push | |
| uses: docker/build-push-action@v4 | |
| with: | |
| context: . | |
| file: ${{ matrix.project }}/Dockerfile.codespace | |
| push: true | |
| tags: zenmldocker/projects-${{ matrix.project }}:${{ steps.timestamp.outputs.timestamp }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max |