背景
因docker全面被墙,pull/build变得极为困难,考虑使用GithubActions帮助我们解决这个问题。
实现
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/[email protected]
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/[email protected]
with:
image: "repo:latest"
retention_days: "1"
成果
直接Fork成品仓库打开Actions权限即可完成构建。
azureology/DockerPullerBuilder: Painless pull and build image using Github Actions