docker-build.yaml 16 KB

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