建置 · Smoke 測試 (Build + Smoke) #27
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: 建置 · Smoke 測試 (Build + Smoke) | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| image: | |
| description: | | |
| Image name (directory name under images/). | |
| Use "all" to build every image. | |
| required: true | |
| default: 'py-devkit-image-base' | |
| env: | |
| REGISTRY: ghcr.io | |
| ORG: shumingyang-opencode | |
| jobs: | |
| discover: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| matrix: ${{ steps.discover.outputs.matrix }} | |
| steps: | |
| - uses: actions/checkout@v7 | |
| - id: discover | |
| run: | | |
| if [ "all" = "${{ inputs.image }}" ]; then | |
| FIRST=true | |
| JSON='{"image":[' | |
| for dir in images/*/; do | |
| name=$(basename "$dir") | |
| [ -f "images/$name/Dockerfile" ] || continue | |
| if [ "$FIRST" = true ]; then | |
| JSON="$JSON\"$name\"" | |
| FIRST=false | |
| else | |
| JSON="$JSON,\"$name\"" | |
| fi | |
| done | |
| JSON="$JSON]}" | |
| echo "matrix=$JSON" >> "$GITHUB_OUTPUT" | |
| else | |
| if [ ! -d "images/${{ inputs.image }}" ]; then | |
| echo "Error: images/${{ inputs.image }} not found" | |
| exit 1 | |
| fi | |
| echo "matrix={\"image\":[\"${{ inputs.image }}\"]}" >> "$GITHUB_OUTPUT" | |
| fi | |
| build: | |
| needs: discover | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| strategy: | |
| matrix: ${{ fromJSON(needs.discover.outputs.matrix) }} | |
| steps: | |
| - uses: actions/checkout@v7 | |
| - name: Login to GHCR | |
| uses: docker/login-action@v4 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract version from Dockerfile | |
| id: version | |
| run: | | |
| IMG=${{ matrix.image }} | |
| VER=$(grep -oP 'org\.opencontainers\.image\.version="\K[^"]+' images/$IMG/Dockerfile || echo "0.0.0") | |
| echo "version=$VER" >> "$GITHUB_OUTPUT" | |
| echo "image=$IMG" >> "$GITHUB_OUTPUT" | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v4 | |
| - name: Build and push | |
| uses: docker/build-push-action@v7 | |
| with: | |
| context: ./images/${{ steps.version.outputs.image }} | |
| file: ./images/${{ steps.version.outputs.image }}/Dockerfile | |
| push: true | |
| tags: | | |
| ${{ env.REGISTRY }}/${{ env.ORG }}/${{ steps.version.outputs.image }}:${{ steps.version.outputs.version }} | |
| ${{ env.REGISTRY }}/${{ env.ORG }}/${{ steps.version.outputs.image }}:latest | |
| labels: | | |
| org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} | |
| org.opencontainers.image.revision=${{ github.sha }} | |
| test: | |
| needs: [build, discover] | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: read | |
| strategy: | |
| matrix: ${{ fromJSON(needs.discover.outputs.matrix) }} | |
| steps: | |
| - uses: actions/checkout@v7 | |
| - name: Login to GHCR | |
| uses: docker/login-action@v4 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract image info | |
| id: info | |
| run: | | |
| IMG=${{ matrix.image }} | |
| VER=$(grep -oP 'org\.opencontainers\.image\.version="\K[^"]+' images/$IMG/Dockerfile || echo "0.0.0") | |
| echo "tag=${{ env.REGISTRY }}/${{ env.ORG }}/$IMG:$VER" >> "$GITHUB_OUTPUT" | |
| - name: Run smoke tests | |
| run: | | |
| docker pull ${{ steps.info.outputs.tag }} | |
| docker run --rm -i ${{ steps.info.outputs.tag }} bash < images/${{ matrix.image }}/test-smoke.sh |