Skip to content

Commit f59d51a

Browse files
committed
feat(cnb-image): add to cnb-image
Signed-off-by: aLinChe <1129332011@qq.com>
1 parent 67275ad commit f59d51a

4 files changed

Lines changed: 121 additions & 2 deletions

File tree

.github/workflows/cd-publish.yaml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: Build and Publish to CNB
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
workflow_dispatch: # 允许手动触发
7+
8+
env:
9+
# 设置镜像名和构建产物的名称
10+
IMAGE_NAME: docker.cnb.cool/alinche/linux-minimal
11+
ARTIFACT_NAME: myminilinux-build-artifacts
12+
13+
jobs:
14+
build:
15+
name: Build Linux Minimal
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Install Dependencies
22+
run: |
23+
sudo apt-get update
24+
sudo apt-get install -y build-essential g++ qemu-utils parted dosfstools e2fsprogs udev kpartx grub2-common
25+
26+
- name: Build Project
27+
run: |
28+
echo "开始构建..."
29+
make build
30+
make make_diskimage
31+
32+
mkdir -p build bin/vmlinuz
33+
tar -czf "${{ env.ARTIFACT_NAME }}.tar.gz" build bin/vmlinuz/vmlinuz-x86_64
34+
ls -lh "${{ env.ARTIFACT_NAME }}.tar.gz"
35+
36+
- name: Upload Build Artifacts
37+
uses: actions/upload-artifact@v4
38+
with:
39+
name: ${{ env.ARTIFACT_NAME }}
40+
path: "${{ env.ARTIFACT_NAME }}.tar.gz"
41+
retention-days: 1
42+
43+
publish:
44+
name: Publish Docker Image
45+
needs: build # 必须等待 build 任务成功
46+
runs-on: ubuntu-latest
47+
steps:
48+
- name: Checkout code
49+
uses: actions/checkout@v4
50+
# 这里 checkout 主要是为了获取 Dockerfile
51+
52+
- name: Download Build Artifacts
53+
uses: actions/download-artifact@v4
54+
with:
55+
name: ${{ env.ARTIFACT_NAME }}
56+
# 下载到当前目录,这样 Docker build 就能找到了
57+
path: .
58+
59+
- name: Set up Docker Buildx
60+
uses: docker/setup-buildx-action@v2
61+
62+
- name: Login to CNB Registry
63+
uses: docker/login-action@v2
64+
with:
65+
registry: docker.cnb.cool
66+
username: cnb
67+
password: ${{ secrets.CNB_TOKEN }}
68+
69+
- name: Generate Tag
70+
id: meta
71+
run: |
72+
# 生成类似 :20251218-66ccff 的标签
73+
SHA_SHORT=$(git rev-parse --short HEAD)
74+
DATE_TAG=$(date +%Y%m%d)
75+
echo "tag=${DATE_TAG}-${SHA_SHORT}" >> $GITHUB_OUTPUT
76+
77+
- name: Build and Push Docker Image
78+
uses: docker/build-push-action@v4
79+
with:
80+
context: .
81+
# 指定你的 Dockerfile 路径
82+
file: Dockerfile
83+
platforms: linux/amd64
84+
push: true
85+
build-args: |
86+
TAR_FILE=${{ env.ARTIFACT_NAME }}.tar.gz
87+
tags: |
88+
${{ env.IMAGE_NAME }}:latest
89+
${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.tag }}

.github/workflows/sync-to-cnb.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ on:
44
branches: [ master ] # 指定在推送到master分支时触发
55

66
jobs:
7-
sync:
7+
sync-code:
88
runs-on: ubuntu-latest
99
steps:
1010
- name: Checkout code
1111
uses: actions/checkout@v3
12+
with:
13+
fetch-depth: 0
1214

1315
- name: Configure Git and Push to CNB
1416
run: |

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*.app
3333

3434
.*
35-
!/.github/**
35+
!/.github
3636
/build
3737
mnt
3838
rootfs/*

tools/Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM ubuntu:jammy
2+
3+
ARG TAR_FILE=myminilinux-build-artifacts
4+
5+
ENV TZ=Asia/Shanghai
6+
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
7+
8+
# 设置工作目录
9+
WORKDIR /app
10+
11+
# 将本地的脚本复制到工作目录
12+
COPY *.sh ./
13+
14+
# 将构建产物复制进镜像 (这个文件名要和 workflow 里解压/下载的文件名一致)
15+
COPY ${TAR_FILE} /app/
16+
17+
# 设置sudo免密码
18+
RUN apt update && \
19+
apt install --no-install-recommends -y ca-certificates curl gnupg wget sudo apt-utils tar && \
20+
git config --global --add safe.directory '*' && \
21+
sudo apt autoremove -q -y && \
22+
sudo apt clean -q -y
23+
24+
# 解压内核跟磁盘镜像
25+
RUN tar -xvf ${TAR_FILE} && rm ${TAR_FILE}
26+
27+
# 设置容器启动时的默认命令
28+
CMD ["/bin/bash"]

0 commit comments

Comments
 (0)