Skip to content

Commit d9a8929

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

5 files changed

Lines changed: 132 additions & 2 deletions

File tree

.github/workflows/cd-publish.yaml

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

.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/BUILD_CONTAINER_VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.1

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 git ca-certificates curl gnupg wget sudo apt-utils tar qemu-system-x86 && \
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)