-
Notifications
You must be signed in to change notification settings - Fork 2
78 lines (66 loc) · 2.45 KB
/
Copy pathpublish-devcontainer.yml
File metadata and controls
78 lines (66 loc) · 2.45 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
name: Build and Push Dev Container Image
on:
push:
branches:
- main
paths:
- '.devcontainer/**' # 只有当 devcontainer 目录变化时才触发
# 新增: 允许手动触发,方便你强制重构
workflow_dispatch:
# 新增: 每周自动构建一次,确保能够获取 gitgit188/gomtm 的最新更新
# 因为如果你的基础镜像更新了,而你的 Dockerfile 没变,不加这个就不会触发构建
schedule:
- cron: '0 0 * * 1' # 每周一运行
# 新增: 并发控制
# 如果你连续提交了3次代码,这个配置会自动取消前2次正在运行的构建,只保留最新的
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
REGISTRY: ghcr.io
# 动态获取当前仓库名称,支持 Fork 后自动适配 (例如 user/dev)
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
# 必要的权限设置
permissions:
contents: read
packages: write # 允许推送到 GHCR
steps:
- name: Checkout repository
uses: actions/checkout@v4
# 1. 设置 Docker Buildx (这是使用缓存的高级特性的前提)
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# 2. 登录 GitHub Container Registry
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# 3. 提取元数据 (自动处理 tags 和 labels)
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest
type=sha,format=long
type=schedule,pattern={{date 'YYYYMMDD'}}
# 4. 构建并推送 (核心优化部分)
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .devcontainer
file: .devcontainer/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# 开启 GitHub Actions 缓存 (极大提升二次构建速度)
cache-from: type=gha
cache-to: type=gha,mode=max
# 确保构建时能拉取到最新的基础镜像
pull: true