use specific image digest when pulling base image #8
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: | |
| - uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 2 | |
| - name: Detect changed projects | |
| id: set-matrix | |
| run: | | |
| if [[ "${{ github.event_name }}" == "workflow_dispatch" && -n "${{ github.event.inputs.project }}" ]]; then | |
| PROJECTS="[\"${{ github.event.inputs.project }}\"]" | |
| else | |
| CHANGED_FILES=$(git diff --name-only HEAD^ HEAD) | |
| CHANGED_DIRS=$(echo "$CHANGED_FILES" \ | |
| | grep -o "^[^/]*" \ | |
| | sort -u \ | |
| | grep -v "^$") | |
| ALL_PROJECT_DIRS=$(find . -maxdepth 1 -type d \ | |
| -not -path "*/\.*" \ | |
| -not -path "." \ | |
| | sed 's|^\./||' \ | |
| | grep -vE "^(scripts|_).*$") | |
| 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" | |
| build-or-generate: | |
| needs: detect-changes | |
| runs-on: ubuntu-latest | |
| if: ${{ fromJson(needs.detect-changes.outputs.matrix)[0] != null }} | |
| strategy: | |
| matrix: | |
| project: ${{ fromJson(needs.detect-changes.outputs.matrix) }} | |
| steps: | |
| - uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 | |
| - name: Check for Dockerfile.codespace | |
| id: check | |
| run: | | |
| if [ -f "${{ matrix.project }}/Dockerfile.codespace" ]; then | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Debug Docker image | |
| if: steps.check.outputs.exists == 'true' | |
| run: | | |
| docker pull zenmldocker/zenml-sandbox:latest | |
| docker image inspect zenmldocker/zenml-sandbox:latest || echo "Failed to inspect image" | |
| docker manifest inspect zenmldocker/zenml-sandbox:latest || echo "Failed to inspect manifest" | |
| # ── Generate & submit PR if missing ───────────────── | |
| - name: Generate Dockerfile.codespace | |
| if: steps.check.outputs.exists == 'false' | |
| run: python ./scripts/generate_codespace_dockerfile.py "${{ matrix.project }}" | |
| - name: Create PR for generated Dockerfile | |
| if: steps.check.outputs.exists == 'false' | |
| 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 ${{ matrix.project }}. | |
| branch: "auto-dockerfile-${{ matrix.project }}" | |
| base: main | |
| labels: automated-pr,dockerfile,codespace | |
| # ── Build & push if present ─────────────────── | |
| - name: Generate image tag timestamp | |
| if: steps.check.outputs.exists == 'true' | |
| id: timestamp | |
| run: echo "timestamp=$(date -u +'%Y%m%d%H%M%S')" >> $GITHUB_OUTPUT | |
| - name: Set up Docker Buildx | |
| if: steps.check.outputs.exists == 'true' | |
| uses: docker/setup-buildx-action@v2 | |
| with: | |
| platforms: linux/amd64 | |
| - name: Login to DockerHub | |
| if: steps.check.outputs.exists == 'true' | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
| - name: Build and push | |
| if: steps.check.outputs.exists == 'true' | |
| uses: docker/build-push-action@v4 | |
| with: | |
| context: . | |
| file: ${{ matrix.project }}/Dockerfile.codespace | |
| push: true | |
| platforms: linux/amd64 | |
| provenance: false # prevents failure due to 'no match for platform in manifest': https://github.com/docker/build-push-action/issues/820 | |
| tags: zenmldocker/projects-${{ matrix.project }}:${{ steps.timestamp.outputs.timestamp }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max |