创建项目:
git clone https://github.com/venbill/demoimage.git
.github/workflows/docker-image.yml 为githubworkflow配置文件
Dockerfile为镜像构建文件
index.html为拷贝到镜像中的文件
docker-image.yml解释
name: Docker Image CI
on:
push:
tags:
- 'v*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set version
id: version
run: echo ::set-output name=release_version::${GITHUB_REF#refs/*/}
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
platforms: |
linux/amd64
push: true
build-args: |
APP_NAME=${{ github.event.repository.name }}
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:latest
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:${{ steps.version.outputs.release_version }}
on.push.branches: [ master ]
#master分支push触发构建
on.push.tags:- 'v*'
#以v开头的tag触发构建
runs-on: ubuntu-latest
构建镜像所在系统
actions/checkout@v2
拉代码
QEMU
qemu是一个和vmware类似的虚拟化工具
Docker Buildx
构建工具
Login to DockerHub
登录dockerhub
Build and push
构建镜像,并且push到中央仓库
platforms: linux/amd64
构建镜像对应的平台,受到from镜像限制
QEMU支持环境包括:
同时需要考虑Dockerfile中脚本对各个平台来源镜像命令的兼容性。可能部分命令在arm系统镜像不能正常执行。
配置dockerhub AccessToken和github secrets
idea 自动将项目提交到github,并提交代码
在github找到这个项目,配置secrets actions
新增docker-image.yml中引用的变量
DOCKERHUB_USERNAME dockerhub用户名
DOCKERHUB_TOKEN 上面在dockerhub上创建的token
新建分支v1.0并push到github,自动触发 workflow actions
等执行完成后查看dockerhub上的镜像
完成镜像制作并提交到dockerhub,速度非常快,而且屏蔽了环境问题。同时解决了网络问题,以及外网访问的问题。