|
| 1 | +name: Build and Push Docker Images with Nix |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + pull_request: |
| 7 | + branches: [ main ] |
| 8 | + schedule: |
| 9 | + - cron: '0 2 * * 1' # Weekly on Monday at 2 AM |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + push_images: |
| 13 | + description: 'Push images to registry' |
| 14 | + type: boolean |
| 15 | + default: true |
| 16 | + |
| 17 | +env: |
| 18 | + REGISTRY: ghcr.io |
| 19 | + IMAGE_NAME: reaslab/docker-python-uv |
| 20 | + |
| 21 | +jobs: |
| 22 | + build: |
| 23 | + runs-on: fedora-latest |
| 24 | + permissions: |
| 25 | + contents: read |
| 26 | + packages: write |
| 27 | + id-token: write |
| 28 | + attestations: write |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Checkout |
| 32 | + uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - name: Set up Nix |
| 35 | + uses: cachix/install-nix-action@v22 |
| 36 | + with: |
| 37 | + nix_path: nixpkgs=channel:nixos-unstable |
| 38 | + |
| 39 | + - name: Install Nix dependencies |
| 40 | + run: | |
| 41 | + nix-env -iA nixpkgs.dockerTools |
| 42 | + nix-env -iA nixpkgs.gnutar |
| 43 | + nix-env -iA nixpkgs.gzip |
| 44 | +
|
| 45 | + - name: Build Docker image with Nix |
| 46 | + run: | |
| 47 | + nix-build docker.nix --option sandbox false |
| 48 | +
|
| 49 | + - name: Set up Docker Buildx |
| 50 | + uses: docker/setup-buildx-action@v3 |
| 51 | + |
| 52 | + - name: Log in to Container Registry |
| 53 | + if: github.event.inputs.push_images != 'false' |
| 54 | + uses: docker/login-action@v3 |
| 55 | + with: |
| 56 | + registry: ${{ env.REGISTRY }} |
| 57 | + username: ${{ github.actor }} |
| 58 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 59 | + |
| 60 | + - name: Load Docker image |
| 61 | + run: | |
| 62 | + docker load < result |
| 63 | +
|
| 64 | + - name: Extract metadata |
| 65 | + id: meta |
| 66 | + if: github.event.inputs.push_images != 'false' |
| 67 | + uses: docker/metadata-action@v5 |
| 68 | + with: |
| 69 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 70 | + tags: | |
| 71 | + type=raw,value=secure-latest |
| 72 | +
|
| 73 | + - name: Tag Docker image |
| 74 | + if: github.event.inputs.push_images != 'false' |
| 75 | + run: | |
| 76 | + # Get the image ID from the loaded image |
| 77 | + IMAGE_ID=$(docker images --format "{{.ID}}" | head -1) |
| 78 | + echo "Image ID: $IMAGE_ID" |
| 79 | + |
| 80 | + # Tag with all metadata tags |
| 81 | + for tag in ${{ steps.meta.outputs.tags }}; do |
| 82 | + echo "Tagging with: $tag" |
| 83 | + docker tag $IMAGE_ID $tag |
| 84 | + done |
| 85 | +
|
| 86 | + - name: Push Docker image |
| 87 | + if: github.event.inputs.push_images != 'false' |
| 88 | + run: | |
| 89 | + for tag in ${{ steps.meta.outputs.tags }}; do |
| 90 | + echo "Pushing: $tag" |
| 91 | + docker push $tag |
| 92 | + done |
| 93 | +
|
| 94 | + - name: Run security scan |
| 95 | + if: github.event.inputs.push_images != 'false' |
| 96 | + uses: aquasecurity/trivy-action@master |
| 97 | + with: |
| 98 | + image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:secure-latest |
| 99 | + format: 'sarif' |
| 100 | + output: 'trivy-results.sarif' |
| 101 | + |
| 102 | + - name: Upload Trivy scan results |
| 103 | + if: github.event.inputs.push_images != 'false' |
| 104 | + uses: github/codeql-action/upload-sarif@v2 |
| 105 | + with: |
| 106 | + sarif_file: 'trivy-results.sarif' |
| 107 | + |
| 108 | + test: |
| 109 | + runs-on: fedora-latest |
| 110 | + needs: build |
| 111 | + if: github.event.inputs.push_images != 'false' |
| 112 | + |
| 113 | + steps: |
| 114 | + - name: Test Python version |
| 115 | + run: | |
| 116 | + docker run --rm ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:secure-latest python --version |
| 117 | +
|
| 118 | + - name: Test UV installation |
| 119 | + run: | |
| 120 | + docker run --rm ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:secure-latest uv --version |
| 121 | +
|
| 122 | + - name: Test security restrictions |
| 123 | + run: | |
| 124 | + # Test that dangerous modules are restricted |
| 125 | + docker run --rm ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:secure-latest python -c " |
| 126 | + try: |
| 127 | + import os |
| 128 | + print('ERROR: os module should be restricted') |
| 129 | + exit(1) |
| 130 | + except ImportError: |
| 131 | + print('OK: os module is properly restricted') |
| 132 | + " |
| 133 | +
|
| 134 | + - name: Test Gurobi availability |
| 135 | + run: | |
| 136 | + # Test that Gurobi is available (without license) |
| 137 | + docker run --rm ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:secure-latest python -c " |
| 138 | + try: |
| 139 | + import gurobipy |
| 140 | + print('OK: Gurobi Python package is available') |
| 141 | + except ImportError as e: |
| 142 | + print(f'ERROR: Gurobi not available: {e}') |
| 143 | + exit(1) |
| 144 | + " |
| 145 | +
|
| 146 | + - name: Test scientific packages |
| 147 | + run: | |
| 148 | + # Test that scientific packages are available |
| 149 | + docker run --rm ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:secure-latest python -c " |
| 150 | + import numpy |
| 151 | + import scipy |
| 152 | + import pandas |
| 153 | + import matplotlib |
| 154 | + import sklearn |
| 155 | + print('OK: All scientific packages are available') |
| 156 | + print(f'NumPy version: {numpy.__version__}') |
| 157 | + print(f'SciPy version: {scipy.__version__}') |
| 158 | + print(f'Pandas version: {pandas.__version__}') |
| 159 | + " |
| 160 | +
|
| 161 | + cleanup: |
| 162 | + runs-on: fedora-latest |
| 163 | + needs: [build, test] |
| 164 | + if: always() && github.event.inputs.push_images != 'false' |
| 165 | + |
| 166 | + permissions: |
| 167 | + packages: write |
| 168 | + |
| 169 | + steps: |
| 170 | + - name: Clean up old images |
| 171 | + uses: dataaxiom/ghcr-cleanup-action@v1 |
| 172 | + with: |
| 173 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 174 | + package-name: ${{ env.IMAGE_NAME }} |
| 175 | + keep-versions: 10 |
| 176 | + |
| 177 | + generate-summary: |
| 178 | + runs-on: fedora-latest |
| 179 | + needs: [build, test] |
| 180 | + if: always() |
| 181 | + |
| 182 | + steps: |
| 183 | + - name: Generate summary |
| 184 | + run: | |
| 185 | + echo "## 🐳 Docker Image Build Summary" >> $GITHUB_STEP_SUMMARY |
| 186 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 187 | + echo "**Image:** \`${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}\`" >> $GITHUB_STEP_SUMMARY |
| 188 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 189 | + echo "**Tags:**" >> $GITHUB_STEP_SUMMARY |
| 190 | + echo "- \`secure-latest\`" >> $GITHUB_STEP_SUMMARY |
| 191 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 192 | + echo "**Features:**" >> $GITHUB_STEP_SUMMARY |
| 193 | + echo "- ✅ Secure Python 3.12 environment" >> $GITHUB_STEP_SUMMARY |
| 194 | + echo "- ✅ UV package manager" >> $GITHUB_STEP_SUMMARY |
| 195 | + echo "- ✅ Gurobi optimization solver" >> $GITHUB_STEP_SUMMARY |
| 196 | + echo "- ✅ Scientific computing packages" >> $GITHUB_STEP_SUMMARY |
| 197 | + echo "- ✅ Non-root user execution" >> $GITHUB_STEP_SUMMARY |
| 198 | + echo "- ✅ Resource limits and security restrictions" >> $GITHUB_STEP_SUMMARY |
| 199 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 200 | + echo "**Registry:** [GitHub Container Registry](https://github.com/orgs/reaslab/packages)" >> $GITHUB_STEP_SUMMARY |
0 commit comments