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

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • 网站状态 (opens new window)
    • json2go (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
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancker
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 分类
  • 标签
  • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • 网站状态 (opens new window)
    • json2go (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

  • Prometheus

  • Grafana

  • CentOS

  • Supervisord

  • Systemd

  • Docker

  • Docker-Compose

  • Rancher

  • Ansible

  • OpenLdap

  • GitLab

  • GitHub

    • GitHub中开源项目维护流程手册
    • 分享我的开源项目Thank-Mirror
    • 一个仅需三步配置就能生成免费个人博客的开源模板vdoing-template
    • 如何将个人的GitHub主页配置的优雅好看
    • 利用GitHub Actions自动为README添加TOC目录
    • 利用GitHub Actions自动将项目贡献者列表添加到README中
    • 利用GitHub Actions自动优雅地为项目构建Releases
    • 利用GitHub Actions自动获取博客rss文章
    • 利用GitHub Actions自动构建项目的docker镜像并发布到DockerHub
      • 前言
      • 配置
    • 利用GitHub Actions自动生成GitHub的Fans
    • 利用GitHub Actions自动生成个人star列表并归类
    • 利用GitHub Actions自动对仓库内图片进行无损压缩
    • 利用GitHub Actions自动检测项目中的问题链接
    • 利用GitHub Actions自动构建go项目的二进制到release
  • 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架构平台的。

微信 支付宝
上次更新: 2022/08/19, 09:49:20

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

最近更新
01
学习周刊-总第91期-2023年第04周
01-27
02
学习周刊-总第90期-2023年第03周
01-20
03
学习周刊-总第89期-2023年第02周
01-13
更多文章>
Theme by Vdoing | Copyright © 2017-2023 | 点击查看十年之约 | 浙ICP备18057030号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式