利用GithubActions构建和拉取docker镜像
背景#
因docker全面被墙,pull/build变得极为困难,考虑使用GithubActions帮助我们解决这个问题。
之前写过使用Github Actions构建Docker image局限性较大,这次增加通用性。
实现#
Pull#
核心思想是调用docker pull
再将image打包至artifacts进行取回。
额外增加了架构选择和释放空间选项,较大的image建议勾选防止run out of space
name: Pull and Save Docker Image
on:
workflow_dispatch:
inputs:
free_space:
description: 'Free disk space before pulling'
type: boolean
default: false
image:
description: Docker image name
required: true
default: 'alpine:latest'
arch:
type: choice
description: Select OS/ARCH
options:
- linux/amd64
- linux/arm64
- linux/arm/v7
jobs:
pull_and_upload:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Free Disk Space (Ubuntu)
if: ${{ github.event.inputs.free_space == 'true' }}
uses: jlumbroso/free-disk-space@main
with:
# this might remove tools that are actually needed,
# if set to "true" but frees about 6 GB
tool-cache: false
# all of these default to true, but feel free to set to
# "false" if necessary for your workflow
android: true
dotnet: true
haskell: true
large-packages: true
docker-images: true
swap-storage: true
- name: Pull Docker Images
run: |
docker pull "${{ github.event.inputs.image }}" --platform "${{ github.event.inputs.arch }}"
- name: Upload image
uses: ishworkh/container-image-artifact-upload@v2.0.0
with:
image: "${{ github.event.inputs.image }}"
retention_days: "1"
Build#
需要先clone需要构建的repo用于提供Dockerfile及依赖,调用docker build
同样使用artifacts取回。
name: Build Docker Image By Git Repo
on:
workflow_dispatch:
inputs:
free_space:
description: 'Free disk space before building'
type: boolean
default: false
repo_url:
description: 'HTTP(S) git repo url with Dockerfile'
required: true
default: 'https://github.com/foxglove/nuscenes2mcap.git' # Default repo with Dockerfile
jobs:
build_and_upload:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Free Disk Space (Ubuntu)
if: ${{ github.event.inputs.free_space == 'true' }}
uses: jlumbroso/free-disk-space@main
with:
# this might remove tools that are actually needed,
# if set to "true" but frees about 6 GB
tool-cache: false
# all of these default to true, but feel free to set to
# "false" if necessary for your workflow
android: true
dotnet: true
haskell: true
large-packages: true
docker-images: true
swap-storage: true
- name: Clone git repo and build docker image
run: |
repo_url="${{ github.event.inputs.repo_url }}"
git clone ${repo_url} repo
cd repo
docker build . -t repo:latest
- name: Upload image
uses: ishworkh/docker-image-artifact-upload@v2.0.1
with:
image: "repo:latest"
retention_days: "1"
成果#
直接Fork成品仓库打开Actions权限即可完成构建。
azureology/DockerPullerBuilder: Painless pull and build image using Github Actions
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架