|
| 1 | +name: Build and Push Application Image |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + push: |
| 6 | + branches: ["main"] |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + packages: write |
| 12 | + |
| 13 | +defaults: |
| 14 | + run: |
| 15 | + shell: bash |
| 16 | + |
| 17 | +env: |
| 18 | + REGISTRY: ghcr.io |
| 19 | + IMAGE_NAME: ${{ github.repository }}/my-cuda-app |
| 20 | + |
| 21 | +jobs: |
| 22 | + create_local_images: |
| 23 | + strategy: |
| 24 | + fail-fast: false |
| 25 | + matrix: |
| 26 | + arch: ["amd64", "arm64"] |
| 27 | + runs-on: ${{ matrix.arch == 'arm64' && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }} |
| 28 | + steps: |
| 29 | + |
| 30 | + - name: Free Disk Space (Ubuntu) |
| 31 | + uses: jlumbroso/free-disk-space@main |
| 32 | + with: |
| 33 | + # this might remove tools that are actually needed, |
| 34 | + # if set to "true" but frees about 6 GB |
| 35 | + tool-cache: true |
| 36 | + |
| 37 | + # all of these default to true, but feel free to set to |
| 38 | + # "false" if necessary for your workflow |
| 39 | + android: true |
| 40 | + dotnet: true |
| 41 | + haskell: true |
| 42 | + large-packages: true |
| 43 | + docker-images: true |
| 44 | + swap-storage: true |
| 45 | + |
| 46 | + - name: Checkout |
| 47 | + uses: actions/checkout@v4 |
| 48 | + |
| 49 | + - name: Set up Docker Buildx |
| 50 | + uses: docker/setup-buildx-action@v3 |
| 51 | + with: |
| 52 | + driver-opts: | |
| 53 | + image=moby/buildkit:v0.12.0 |
| 54 | + network=host |
| 55 | +
|
| 56 | + - name: Install Pack CLI |
| 57 | + uses: buildpacks/github-actions/setup-pack@v5.9.4 |
| 58 | + |
| 59 | + - name: Build CUDA Build Image (local only) |
| 60 | + run: | |
| 61 | + docker build -t cuda-build-run:latest -f Dockerfile \ |
| 62 | + --platform linux/${{ matrix.arch }} . |
| 63 | +
|
| 64 | + - name: Create Builder Image (local only) |
| 65 | + run: | |
| 66 | + pack builder create cuda-builder:latest \ |
| 67 | + --config builder.toml \ |
| 68 | + --pull-policy if-not-present |
| 69 | +
|
| 70 | + - name: Export Docker images from the Docker daemon |
| 71 | + run: | |
| 72 | + docker save cuda-builder:latest cuda-build-run:latest | \ |
| 73 | + zstd -T0 --long=31 -o images.tar.zst |
| 74 | +
|
| 75 | + - name: Save Docker images to the cache |
| 76 | + uses: actions/cache/save@v4 |
| 77 | + with: |
| 78 | + key: ${{ github.run_id }}-cuda-images-${{ matrix.arch }} |
| 79 | + path: images.tar.zst |
| 80 | + |
| 81 | + build_application_image: |
| 82 | + runs-on: ${{ matrix.arch == 'arm64' && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }} |
| 83 | + needs: create_local_images |
| 84 | + strategy: |
| 85 | + fail-fast: false |
| 86 | + matrix: |
| 87 | + arch: ["amd64", "arm64"] |
| 88 | + steps: |
| 89 | + - name: Free Disk Space (Ubuntu) |
| 90 | + uses: jlumbroso/free-disk-space@main |
| 91 | + with: |
| 92 | + # this might remove tools that are actually needed, |
| 93 | + # if set to "true" but frees about 6 GB |
| 94 | + tool-cache: true |
| 95 | + |
| 96 | + # all of these default to true, but feel free to set to |
| 97 | + # "false" if necessary for your workflow |
| 98 | + android: true |
| 99 | + dotnet: true |
| 100 | + haskell: true |
| 101 | + large-packages: true |
| 102 | + docker-images: true |
| 103 | + swap-storage: true |
| 104 | + |
| 105 | + - name: Maximize Disk Space (Aggressive) |
| 106 | + run: | |
| 107 | + echo "Available space before cleanup:" |
| 108 | + df -h |
| 109 | +
|
| 110 | + # Remove unnecessary packages and files |
| 111 | + sudo apt-get remove -y '^aspnetcore-.*' '^dotnet-.*' '^llvm-.*' '^mongodb-.*' '^mysql-.*' '^postgresql-.*' |
| 112 | + sudo apt-get autoremove -y |
| 113 | + sudo apt-get autoclean |
| 114 | +
|
| 115 | + # Remove large directories |
| 116 | + sudo rm -rf /usr/share/dotnet |
| 117 | + sudo rm -rf /usr/local/lib/android |
| 118 | + sudo rm -rf /opt/ghc |
| 119 | + sudo rm -rf /opt/hostedtoolcache/CodeQL |
| 120 | + sudo rm -rf /usr/local/share/boost |
| 121 | + sudo rm -rf /usr/local/graalvm/ |
| 122 | + sudo rm -rf /usr/local/share/chromium |
| 123 | + sudo rm -rf /usr/local/lib/node_modules |
| 124 | +
|
| 125 | + # Clean Docker completely |
| 126 | + docker system prune -af --volumes |
| 127 | +
|
| 128 | + echo "Available space after cleanup:" |
| 129 | + df -h |
| 130 | +
|
| 131 | + - name: Checkout |
| 132 | + uses: actions/checkout@v4 |
| 133 | + |
| 134 | + - name: Install Pack CLI |
| 135 | + uses: buildpacks/github-actions/setup-pack@v5.9.4 |
| 136 | + |
| 137 | + - name: Configure Docker for Large Images |
| 138 | + run: | |
| 139 | + sudo systemctl stop docker |
| 140 | + echo '{"storage-driver": "overlay2", "max-concurrent-downloads": 1, "max-concurrent-uploads": 1, "log-driver": "none"}' | sudo tee /etc/docker/daemon.json |
| 141 | + sudo systemctl start docker |
| 142 | + docker system prune -af --volumes |
| 143 | +
|
| 144 | + - name: Restore Docker images from the cache |
| 145 | + uses: actions/cache/restore@v4 |
| 146 | + with: |
| 147 | + fail-on-cache-miss: true |
| 148 | + key: ${{ github.run_id }}-cuda-images-${{ matrix.arch }} |
| 149 | + path: images.tar.zst |
| 150 | + env: |
| 151 | + SEGMENT_DOWNLOAD_TIMEOUT_MINS: 1 |
| 152 | + |
| 153 | + - name: Load Docker images into the Docker daemon |
| 154 | + run: | |
| 155 | + zstd -dc --long=31 images.tar.zst | docker load |
| 156 | + rm -rf images.tar.zst |
| 157 | +
|
| 158 | + - name: Build Application Image |
| 159 | + run: | |
| 160 | + IMAGE_NAME_LOWER=$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]') |
| 161 | + pack build ${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${{ github.sha }}_linux-${{ matrix.arch }} \ |
| 162 | + --builder cuda-builder:latest \ |
| 163 | + --trust-builder \ |
| 164 | + --pull-policy never \ |
| 165 | + --platform linux/${{ matrix.arch }} |
| 166 | +
|
| 167 | + - name: Export Application Image |
| 168 | + run: | |
| 169 | + IMAGE_NAME_LOWER=$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]') |
| 170 | + docker save ${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${{ github.sha }}_linux-${{ matrix.arch }} | \ |
| 171 | + zstd -T0 --long=31 -o app-image.tar.zst |
| 172 | +
|
| 173 | + - name: Save Application Image to cache |
| 174 | + uses: actions/cache/save@v4 |
| 175 | + with: |
| 176 | + key: ${{ github.run_id }}-app-image-${{ matrix.arch }} |
| 177 | + path: app-image.tar.zst |
| 178 | + |
| 179 | + push_application_image: |
| 180 | + runs-on: ubuntu-24.04 |
| 181 | + if: success() && github.ref == 'refs/heads/main' |
| 182 | + needs: build_application_image |
| 183 | + strategy: |
| 184 | + fail-fast: false |
| 185 | + matrix: |
| 186 | + arch: ["amd64", "arm64"] |
| 187 | + steps: |
| 188 | + - name: Free Disk Space (Ubuntu) |
| 189 | + uses: jlumbroso/free-disk-space@main |
| 190 | + with: |
| 191 | + # this might remove tools that are actually needed, |
| 192 | + # if set to "true" but frees about 6 GB |
| 193 | + tool-cache: true |
| 194 | + |
| 195 | + # all of these default to true, but feel free to set to |
| 196 | + # "false" if necessary for your workflow |
| 197 | + android: true |
| 198 | + dotnet: true |
| 199 | + haskell: true |
| 200 | + large-packages: true |
| 201 | + docker-images: true |
| 202 | + swap-storage: true |
| 203 | + |
| 204 | + - name: Log in to Container Registry |
| 205 | + uses: docker/login-action@v3 |
| 206 | + with: |
| 207 | + registry: ${{ env.REGISTRY }} |
| 208 | + username: ${{ github.actor }} |
| 209 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 210 | + |
| 211 | + - name: Restore Application Image from cache |
| 212 | + uses: actions/cache/restore@v4 |
| 213 | + with: |
| 214 | + fail-on-cache-miss: true |
| 215 | + key: ${{ github.run_id }}-app-image-${{ matrix.arch }} |
| 216 | + path: app-image.tar.zst |
| 217 | + |
| 218 | + - name: Load and Push Application Image |
| 219 | + run: | |
| 220 | + zstd -dc --long=31 app-image.tar.zst | docker load |
| 221 | + IMAGE_NAME_LOWER=$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]') |
| 222 | + docker push ${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${{ github.sha }}_linux-${{ matrix.arch }} |
| 223 | +
|
| 224 | + create_manifest: |
| 225 | + runs-on: ubuntu-24.04 |
| 226 | + needs: push_application_image |
| 227 | + if: success() && github.ref == 'refs/heads/main' |
| 228 | + steps: |
| 229 | + - name: Log in to Container Registry |
| 230 | + uses: docker/login-action@v3 |
| 231 | + with: |
| 232 | + registry: ${{ env.REGISTRY }} |
| 233 | + username: ${{ github.actor }} |
| 234 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 235 | + |
| 236 | + - name: Create and push manifest lists |
| 237 | + run: | |
| 238 | + # Create manifest for commit SHA |
| 239 | + IMAGE_NAME_LOWER=$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]') |
| 240 | + docker manifest create ${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${{ github.sha }} \ |
| 241 | + ${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${{ github.sha }}_linux-amd64 \ |
| 242 | + ${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${{ github.sha }}_linux-arm64 |
| 243 | + docker manifest push ${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${{ github.sha }} |
| 244 | +
|
| 245 | + # Create manifest for latest tag |
| 246 | + docker manifest create ${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:latest \ |
| 247 | + ${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${{ github.sha }}_linux-amd64 \ |
| 248 | + ${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${{ github.sha }}_linux-arm64 |
| 249 | + docker manifest push ${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:latest |
| 250 | +
|
0 commit comments