二丫讲梵 二丫讲梵
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)

二丫讲梵

行者常至,为者常成
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)
  • 最佳实践

  • 迎刃而解

  • Nginx

  • Php

  • Zabbix

  • AWS

  • Prometheus

  • Grafana

  • Loki

  • CentOS

  • Supervisord

  • Systemd

  • Docker

  • Docker-Compose

  • Rancher

  • Ansible

  • OpenLdap

  • GitLab

  • GitHub

    • GitHub中开源项目维护流程手册
    • 分享我的开源项目Thank-Mirror
    • 2024年重磅开源项目 awesome-ops 已收录项目500个
    • 分享我的开源项目Cloud_Dns_Exporter,妈妈再也不担心我忘换证书了
    • 一个仅需三步配置就能生成免费个人博客的开源模板vdoing-template
    • 如何将个人的GitHub主页配置的优雅好看
    • 利用GitHub Actions自动为README添加TOC目录
    • 利用GitHub Actions自动将项目贡献者列表添加到README中
    • 利用GitHub Actions自动优雅地为项目构建Releases
    • 利用GitHub Actions自动获取博客rss文章
    • 利用GitHub Actions自动构建项目的docker镜像并发布到DockerHub
      • 前言
      • 配置
      • 补充
      • 发布到 ghcr
    • 利用GitHub Actions自动生成GitHub的Fans
    • 利用GitHub Actions自动生成个人star列表并归类
    • 利用GitHub Actions自动对仓库内图片进行无损压缩
    • 利用GitHub Actions自动检测项目中的问题链接
    • 利用GitHub Actions自动构建go项目的二进制到release
    • 利用github-slug-action暴漏Github Action上下文中的关键变量
    • 利用Github Action自动检测项目中 yaml 或 json 的语法
    • 记录最近在Github Action配置实践中的几个新的收获
    • VMR一个开源的通用SDK版本管理器
  • Etcd

  • Consul

  • RabbitMQ

  • Kafka

  • Mysql

  • MongoDB

  • OpenVPN

  • Kvm

  • VMware

  • 配置文件详解

  • Other

  • 运维观止
  • GitHub
二丫讲梵
2022-07-23
目录

利用GitHub Actions自动构建项目的docker镜像并发布到DockerHub

文章发布较早,内容可能过时,阅读注意甄别。

# 前言

做一个开源项目,尽量提供给受众以简单易用的快速上手体验,也是项目能够立刻把人抓住的一个关键。现在如果想让用户快速体验项目,除了提供 demo 环境之外,还有一个方案,那就是提供一个完备的 docker-compose,让人能够直接一键拉起。

注意:是 docker-compose,而非 k8s 的 yml,尽管生产环境直接用 docker-compose 的很少,但是作为中间阶段,快速部署一个项目体验,而又不需要过多基础环境配置的场景来说,优势还是很大的。

于是,项目应该配套提供好对应的镜像,而由于现在 Mac 新 CPU 架构越来越多,因此提供的镜像最好又是能够支持多 CPU 架构运行的。

本文就来讲一下,如何借助 Github Actions 自动构建兼容多 CPU 架构的 docker 镜像并发布到 DockerHub。

# 配置

所用 Actions: build-push-action (opens new window) 多 CPU 架构镜像构建的流程文档:利用 buildx 构建支持多 CPU 架构平台的 docker 镜像 (opens new window) ,此内容提供基础知识参考,后边构建不需要了解过多。

使用配置其实非常简单,基本上阅读完官方介绍文档就可以上手使用了,这里说一两个需要注意的地方。

首先添加 Actions 配置文件,e.g. .github/workflows/docker-image.yml:

# This is a basic workflow to help you get started with Actions
name: build docker image
# Controls when the action will run.
on:
  push:
    branches:
      - main
  # Allows you to run this workflow manually from the Actions tab
  # 可以手动触发
  workflow_dispatch:
    inputs:
      logLevel:
        description: "Log level"
        required: true
        default: "warning"
      tags:
        description: "Test scenario tags"

jobs:
  buildx:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Get current date
        id: date
        run: echo "::set-output name=today::$(date +'%Y-%m-%d_%H-%M')"

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1

      - name: Available platforms
        run: echo ${{ steps.buildx.outputs.platforms }}

      - 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: .
          file: ./Dockerfile
          # 所需要的体系结构,可以在 Available platforms 步骤中获取所有的可用架构
          platforms: linux/amd64,linux/arm64/v8
          # 镜像推送时间
          push: ${{ github.event_name != 'pull_request' }}
          # 给清单打上多个标签
          tags: |
            eryajf/go-ldap-admin-server:${{ steps.date.outputs.today }}
            eryajf/go-ldap-admin-server:latest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

很多配置见名知意,对照官方文档也都能找到答案,这里就不多赘述。

这里对几个关键的配置项做一下单独说明:

  • DOCKERHUB_TOKEN 的配置这里就不赘述了,在项目的 setting 中进行配置,已经多次讲过,这里留下此 token 创建的地址:https://hub.docker.com/settings/security (opens new window)
  • file:指定在项目仓库中的 Dockerfile 文件位置。
  • platforms:指定构建镜像所需要兼容支持的平台架构,通常 amd,arm 就够了。
  • tags:将要构建的镜像标签,此处我定义的是,每次构建时,提交一个该镜像时间戳的标签,再覆盖一下 latest 的标签,这样提供给 docker-compose 就直接用 latest 标签,可以保障每个新用户体验拉起的时候都是最新的镜像。

最后构建的镜像效果如下:

image_20220723_105957

这里也可以看到推上去的镜像都是兼容两个 CPU 架构平台的。

# 补充

以上给出的配置示例,是当项目 main 分支有代码提交之后触发,有时候你可能会觉得这样每次打包会比较频繁,希望把打镜像的操作与 tag 对齐,那么可以通过如下配置进行:

name: build_docker

on:
  push:
    branches: [main]
  release:
    types: [created] # 表示在创建新的 Release 时触发

jobs:
  build_docker:
    name: Build docker
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push
        id: docker_build
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./Dockerfile
          push: true
          platforms: linux/amd64,linux/arm64
          tags: |
            ${{ secrets.DOCKERHUB_USERNAME }}/${{ GITHUB_REPOSITORY_NAME_PART }}:${{ github.ref_name }}
            ${{ secrets.DOCKERHUB_USERNAME }}/${{ GITHUB_REPOSITORY_NAME_PART }}:latest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

此处添加了触发时机,重要的在最后,我们使用 ${{ github.ref_name }} 来拿到 github 当次触发的来源,如果是分支 push,则这个值为 main,如果是创建了一个 v0.0.1 的 tag,则这个值为 v0.0.1。

# 发布到 ghcr

ghcr.io 是 GitHub 官方的容器仓库,如果你想将镜像发布到 ghcr,则可通过如下内容进行发布:

name: build_docker

on:
  push:
    branches: [main]
  release:
    types: [created, published] # 表示在创建新的 Release 时触发

jobs:
  build_docker:
    name: Build docker
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Inject slug/short variables
        uses: rlespinasse/github-slug-action@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      # Create Docker Image in Github
      - name: Login to the GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build And Push
        uses: docker/build-push-action@v3
        with:
          context: .
          push: true
          platforms: linux/amd64,linux/arm64
          tags: |
            ghcr.io/${{ secrets.DOCKERHUB_USERNAME }}/${{ env.GITHUB_REPOSITORY_NAME_PART }}:${{ env.GITHUB_REF_NAME }}
            ghcr.io/${{ secrets.DOCKERHUB_USERNAME }}/${{ env.GITHUB_REPOSITORY_NAME_PART }}:latest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
微信 支付宝
上次更新: 2024/09/26, 21:41:44
利用GitHub Actions自动获取博客rss文章
利用GitHub Actions自动生成GitHub的Fans

← 利用GitHub Actions自动获取博客rss文章 利用GitHub Actions自动生成GitHub的Fans→

最近更新
01
从赵心童世锦赛夺冠聊聊我的斯诺克情缘
05-16
02
学习周刊-总第211期-2025年第20周
05-15
03
记录二五年五一之短暂回归家庭
05-09
更多文章>
Theme by Vdoing | Copyright © 2017-2025 | 点击查看十年之约 | 浙ICP备18057030号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式