docker-build.yaml 17 KB

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