banner
NEWS LETTER

GithubAction

Scroll down

本文作者:丁辉

GithubAction

构建镜像

GITHUB设置 secrets

网址为:仓库地址/settings/secrets/actions

定时任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#定时任务触发构建
on:
schedule:
- cron: "0 0 * * *"

#通过接口触发构建
on:
repository_dispatch:
types:
- StartAction

#通过 push 代码触发构建
on:
push:
branches:
- master

# 当 push 到 master 分支,或者创建以 v 开头的 tag 时触发
on:
push:
branches:
- master
tags:
- v*

构建 Docker 镜像

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
59
60
61
62
63
64
65
66
name: Build-Images

#本地执行命令类
jobs:
run-docker-command:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Run Docker Command
run: |
docker run --name mysql \
-e MYSQL_ROOT_PASSWORD=${{ secrets.PASSWORD }} \
${{ secrets.IMAGES }}
---
# Docker构建镜像并 push 到仓库内
jobs:
Build-Images-One:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Build and push image
uses: docker/build-push-action@v3
with:
context: ./
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_STASH }}:${{ secrets.TAG }}

Build--Images-Two:
needs: Build-Images-One #等待 One 构建成功后开始执行
runs-on: ubuntu-latest
steps:
-
name: Check Out
uses: actions/checkout@v3
-
name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build and push
id: docker_build
uses: docker/build-push-action@v3
with:
context: ./demo/
file: ./demo/Dockerfile
push: true
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_STASH }}:${{ secrets.TAG }}

接口触发构建

1
curl -X POST https://api.github.com/repos/用户/仓库名/dispatches -H "Accept: application/vnd.github.everest-preview+json" -H "Authorization: token $YOUR_API_TOKEN" --data '{"event_type": "StartAction"}'

构建多架构镜像

官方Demo

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
name: ci

on:
push:
branches:
- "main"

jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
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: Build and push
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64,linux/arm64
#支持列表:https://github.com/docker-library/official-images#architectures-other-than-amd64
#platforms: |
#linux/arm64
#linux/amd64
#linux/arm/v5
#linux/arm/v7
#linux/386 #适用于 x86 32 位架构的 Docker 镜像
#linux/mips64le #适用于 MIPS 64 位架构的 Docker 镜像
#linux/ppc64le #适用于 IBM Power 架构的 Docker 镜像
#linux/s390x #适用于 IBM Z 架构的 Docker 镜像
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/nginx:latest

I'm so cute. Please give me money.

其他文章
cover
脚本安装Docker
  • 70/01/01
  • 00:00
  • Kubernetes-Docker
cover
CEPH对接Kubernetes-StorageClass
  • 70/01/01
  • 00:00
  • Kubernetes-存储
目录导航 置顶
  1. 1. GithubAction
    1. 1.1. 构建镜像
      1. 1.1.1. GITHUB设置 secrets
      2. 1.1.2. 定时任务
      3. 1.1.3. 构建 Docker 镜像
    2. 1.2. 接口触发构建
    3. 1.3. 构建多架构镜像
请输入关键词进行搜索