docker-build.yaml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. name: Create and publish Docker images with specific build args
  2. on:
  3. workflow_dispatch:
  4. push:
  5. branches:
  6. - main
  7. - dev
  8. tags:
  9. - v*
  10. env:
  11. REGISTRY: ghcr.io
  12. IMAGE_NAME: ${{ github.repository }}
  13. FULL_IMAGE_NAME: ghcr.io/${{ github.repository }}
  14. jobs:
  15. build-main-image:
  16. runs-on: ubuntu-latest
  17. permissions:
  18. contents: read
  19. packages: write
  20. strategy:
  21. fail-fast: false
  22. matrix:
  23. platform:
  24. - linux/amd64
  25. - linux/arm64
  26. steps:
  27. - name: Prepare
  28. run: |
  29. platform=${{ matrix.platform }}
  30. echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
  31. - name: Checkout repository
  32. uses: actions/checkout@v4
  33. - name: Set up QEMU
  34. uses: docker/setup-qemu-action@v3
  35. - name: Set up Docker Buildx
  36. uses: docker/setup-buildx-action@v3
  37. - name: Log in to the Container registry
  38. uses: docker/login-action@v3
  39. with:
  40. registry: ${{ env.REGISTRY }}
  41. username: ${{ github.actor }}
  42. password: ${{ secrets.GITHUB_TOKEN }}
  43. - name: Extract metadata for Docker images (default latest tag)
  44. id: meta
  45. uses: docker/metadata-action@v5
  46. with:
  47. images: ${{ env.FULL_IMAGE_NAME }}
  48. tags: |
  49. type=ref,event=branch
  50. type=ref,event=tag
  51. type=sha,prefix=git-
  52. type=semver,pattern={{version}}
  53. type=semver,pattern={{major}}.{{minor}}
  54. flavor: |
  55. latest=${{ github.ref == 'refs/heads/main' }}
  56. - name: Extract metadata for Docker cache
  57. id: cache-meta
  58. uses: docker/metadata-action@v5
  59. with:
  60. images: ${{ env.FULL_IMAGE_NAME }}
  61. tags: |
  62. type=ref,event=branch
  63. ${{ github.ref_type == 'tag' && 'type=raw,value=main' || '' }}
  64. flavor: |
  65. prefix=cache-${{ matrix.platform }}-
  66. latest=false
  67. - name: Build Docker image (latest)
  68. uses: docker/build-push-action@v5
  69. id: build
  70. with:
  71. context: .
  72. push: true
  73. platforms: ${{ matrix.platform }}
  74. labels: ${{ steps.meta.outputs.labels }}
  75. outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
  76. cache-from: type=registry,ref=${{ steps.cache-meta.outputs.tags }}
  77. cache-to: type=registry,ref=${{ steps.cache-meta.outputs.tags }},mode=max
  78. build-args: |
  79. BUILD_HASH=${{ github.sha }}
  80. - name: Export digest
  81. run: |
  82. mkdir -p /tmp/digests
  83. digest="${{ steps.build.outputs.digest }}"
  84. touch "/tmp/digests/${digest#sha256:}"
  85. - name: Upload digest
  86. uses: actions/upload-artifact@v4
  87. with:
  88. name: digests-main-${{ env.PLATFORM_PAIR }}
  89. path: /tmp/digests/*
  90. if-no-files-found: error
  91. retention-days: 1
  92. build-cuda-image:
  93. runs-on: ubuntu-latest
  94. permissions:
  95. contents: read
  96. packages: write
  97. strategy:
  98. fail-fast: false
  99. matrix:
  100. platform:
  101. - linux/amd64
  102. - linux/arm64
  103. steps:
  104. - name: Prepare
  105. run: |
  106. platform=${{ matrix.platform }}
  107. echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
  108. - name: Checkout repository
  109. uses: actions/checkout@v4
  110. - name: Set up QEMU
  111. uses: docker/setup-qemu-action@v3
  112. - name: Set up Docker Buildx
  113. uses: docker/setup-buildx-action@v3
  114. - name: Log in to the Container registry
  115. uses: docker/login-action@v3
  116. with:
  117. registry: ${{ env.REGISTRY }}
  118. username: ${{ github.actor }}
  119. password: ${{ secrets.GITHUB_TOKEN }}
  120. - name: Extract metadata for Docker images (cuda tag)
  121. id: meta
  122. uses: docker/metadata-action@v5
  123. with:
  124. images: ${{ env.FULL_IMAGE_NAME }}
  125. tags: |
  126. type=ref,event=branch
  127. type=ref,event=tag
  128. type=sha,prefix=git-
  129. type=semver,pattern={{version}}
  130. type=semver,pattern={{major}}.{{minor}}
  131. type=raw,enable=${{ github.ref == 'refs/heads/main' }},prefix=,suffix=,value=cuda
  132. flavor: |
  133. latest=${{ github.ref == 'refs/heads/main' }}
  134. suffix=-cuda,onlatest=true
  135. - name: Extract metadata for Docker cache
  136. id: cache-meta
  137. uses: docker/metadata-action@v5
  138. with:
  139. images: ${{ env.FULL_IMAGE_NAME }}
  140. tags: |
  141. type=ref,event=branch
  142. ${{ github.ref_type == 'tag' && 'type=raw,value=main' || '' }}
  143. flavor: |
  144. prefix=cache-cuda-${{ matrix.platform }}-
  145. latest=false
  146. - name: Build Docker image (cuda)
  147. uses: docker/build-push-action@v5
  148. id: build
  149. with:
  150. context: .
  151. push: true
  152. platforms: ${{ matrix.platform }}
  153. labels: ${{ steps.meta.outputs.labels }}
  154. outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
  155. cache-from: type=registry,ref=${{ steps.cache-meta.outputs.tags }}
  156. cache-to: type=registry,ref=${{ steps.cache-meta.outputs.tags }},mode=max
  157. build-args: |
  158. BUILD_HASH=${{ github.sha }}
  159. USE_CUDA=true
  160. - name: Export digest
  161. run: |
  162. mkdir -p /tmp/digests
  163. digest="${{ steps.build.outputs.digest }}"
  164. touch "/tmp/digests/${digest#sha256:}"
  165. - name: Upload digest
  166. uses: actions/upload-artifact@v4
  167. with:
  168. name: digests-cuda-${{ env.PLATFORM_PAIR }}
  169. path: /tmp/digests/*
  170. if-no-files-found: error
  171. retention-days: 1
  172. build-ollama-image:
  173. runs-on: ubuntu-latest
  174. permissions:
  175. contents: read
  176. packages: write
  177. strategy:
  178. fail-fast: false
  179. matrix:
  180. platform:
  181. - linux/amd64
  182. - linux/arm64
  183. steps:
  184. - name: Prepare
  185. run: |
  186. platform=${{ matrix.platform }}
  187. echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
  188. - name: Checkout repository
  189. uses: actions/checkout@v4
  190. - name: Set up QEMU
  191. uses: docker/setup-qemu-action@v3
  192. - name: Set up Docker Buildx
  193. uses: docker/setup-buildx-action@v3
  194. - name: Log in to the Container registry
  195. uses: docker/login-action@v3
  196. with:
  197. registry: ${{ env.REGISTRY }}
  198. username: ${{ github.actor }}
  199. password: ${{ secrets.GITHUB_TOKEN }}
  200. - name: Extract metadata for Docker images (ollama tag)
  201. id: meta
  202. uses: docker/metadata-action@v5
  203. with:
  204. images: ${{ env.FULL_IMAGE_NAME }}
  205. tags: |
  206. type=ref,event=branch
  207. type=ref,event=tag
  208. type=sha,prefix=git-
  209. type=semver,pattern={{version}}
  210. type=semver,pattern={{major}}.{{minor}}
  211. type=raw,enable=${{ github.ref == 'refs/heads/main' }},prefix=,suffix=,value=ollama
  212. flavor: |
  213. latest=${{ github.ref == 'refs/heads/main' }}
  214. suffix=-ollama,onlatest=true
  215. - name: Extract metadata for Docker cache
  216. id: cache-meta
  217. uses: docker/metadata-action@v5
  218. with:
  219. images: ${{ env.FULL_IMAGE_NAME }}
  220. tags: |
  221. type=ref,event=branch
  222. ${{ github.ref_type == 'tag' && 'type=raw,value=main' || '' }}
  223. flavor: |
  224. prefix=cache-ollama-${{ matrix.platform }}-
  225. latest=false
  226. - name: Build Docker image (ollama)
  227. uses: docker/build-push-action@v5
  228. id: build
  229. with:
  230. context: .
  231. push: true
  232. platforms: ${{ matrix.platform }}
  233. labels: ${{ steps.meta.outputs.labels }}
  234. outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
  235. cache-from: type=registry,ref=${{ steps.cache-meta.outputs.tags }}
  236. cache-to: type=registry,ref=${{ steps.cache-meta.outputs.tags }},mode=max
  237. build-args: |
  238. BUILD_HASH=${{ github.sha }}
  239. USE_OLLAMA=true
  240. - name: Export digest
  241. run: |
  242. mkdir -p /tmp/digests
  243. digest="${{ steps.build.outputs.digest }}"
  244. touch "/tmp/digests/${digest#sha256:}"
  245. - name: Upload digest
  246. uses: actions/upload-artifact@v4
  247. with:
  248. name: digests-ollama-${{ env.PLATFORM_PAIR }}
  249. path: /tmp/digests/*
  250. if-no-files-found: error
  251. retention-days: 1
  252. merge-main-images:
  253. runs-on: ubuntu-latest
  254. needs: [ build-main-image ]
  255. steps:
  256. - name: Download digests
  257. uses: actions/download-artifact@v4
  258. with:
  259. pattern: digests-main-*
  260. path: /tmp/digests
  261. merge-multiple: true
  262. - name: Set up Docker Buildx
  263. uses: docker/setup-buildx-action@v3
  264. - name: Log in to the Container registry
  265. uses: docker/login-action@v3
  266. with:
  267. registry: ${{ env.REGISTRY }}
  268. username: ${{ github.actor }}
  269. password: ${{ secrets.GITHUB_TOKEN }}
  270. - name: Extract metadata for Docker images (default latest tag)
  271. id: meta
  272. uses: docker/metadata-action@v5
  273. with:
  274. images: ${{ env.FULL_IMAGE_NAME }}
  275. tags: |
  276. type=ref,event=branch
  277. type=ref,event=tag
  278. type=sha,prefix=git-
  279. type=semver,pattern={{version}}
  280. type=semver,pattern={{major}}.{{minor}}
  281. flavor: |
  282. latest=${{ github.ref == 'refs/heads/main' }}
  283. - name: Create manifest list and push
  284. working-directory: /tmp/digests
  285. run: |
  286. docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
  287. $(printf '${{ env.FULL_IMAGE_NAME }}@sha256:%s ' *)
  288. - name: Inspect image
  289. run: |
  290. docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:${{ steps.meta.outputs.version }}
  291. merge-cuda-images:
  292. runs-on: ubuntu-latest
  293. needs: [ build-cuda-image ]
  294. steps:
  295. - name: Download digests
  296. uses: actions/download-artifact@v4
  297. with:
  298. pattern: digests-cuda-*
  299. path: /tmp/digests
  300. merge-multiple: true
  301. - name: Set up Docker Buildx
  302. uses: docker/setup-buildx-action@v3
  303. - name: Log in to the Container registry
  304. uses: docker/login-action@v3
  305. with:
  306. registry: ${{ env.REGISTRY }}
  307. username: ${{ github.actor }}
  308. password: ${{ secrets.GITHUB_TOKEN }}
  309. - name: Extract metadata for Docker images (default latest tag)
  310. id: meta
  311. uses: docker/metadata-action@v5
  312. with:
  313. images: ${{ env.FULL_IMAGE_NAME }}
  314. tags: |
  315. type=ref,event=branch
  316. type=ref,event=tag
  317. type=sha,prefix=git-
  318. type=semver,pattern={{version}}
  319. type=semver,pattern={{major}}.{{minor}}
  320. type=raw,enable=${{ github.ref == 'refs/heads/main' }},prefix=,suffix=,value=cuda
  321. flavor: |
  322. latest=${{ github.ref == 'refs/heads/main' }}
  323. suffix=-cuda,onlatest=true
  324. - name: Create manifest list and push
  325. working-directory: /tmp/digests
  326. run: |
  327. docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
  328. $(printf '${{ env.FULL_IMAGE_NAME }}@sha256:%s ' *)
  329. - name: Inspect image
  330. run: |
  331. docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:${{ steps.meta.outputs.version }}
  332. merge-ollama-images:
  333. runs-on: ubuntu-latest
  334. needs: [ build-ollama-image ]
  335. steps:
  336. - name: Download digests
  337. uses: actions/download-artifact@v4
  338. with:
  339. pattern: digests-ollama-*
  340. path: /tmp/digests
  341. merge-multiple: true
  342. - name: Set up Docker Buildx
  343. uses: docker/setup-buildx-action@v3
  344. - name: Log in to the Container registry
  345. uses: docker/login-action@v3
  346. with:
  347. registry: ${{ env.REGISTRY }}
  348. username: ${{ github.actor }}
  349. password: ${{ secrets.GITHUB_TOKEN }}
  350. - name: Extract metadata for Docker images (default ollama tag)
  351. id: meta
  352. uses: docker/metadata-action@v5
  353. with:
  354. images: ${{ env.FULL_IMAGE_NAME }}
  355. tags: |
  356. type=ref,event=branch
  357. type=ref,event=tag
  358. type=sha,prefix=git-
  359. type=semver,pattern={{version}}
  360. type=semver,pattern={{major}}.{{minor}}
  361. type=raw,enable=${{ github.ref == 'refs/heads/main' }},prefix=,suffix=,value=ollama
  362. flavor: |
  363. latest=${{ github.ref == 'refs/heads/main' }}
  364. suffix=-ollama,onlatest=true
  365. - name: Create manifest list and push
  366. working-directory: /tmp/digests
  367. run: |
  368. docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
  369. $(printf '${{ env.FULL_IMAGE_NAME }}@sha256:%s ' *)
  370. - name: Inspect image
  371. run: |
  372. docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:${{ steps.meta.outputs.version }}