Skip to content

Commit b32a0e8

Browse files
authored
ci: use matrix to build images. (#11)
1 parent 8260c27 commit b32a0e8

File tree

9 files changed

+166
-137
lines changed

9 files changed

+166
-137
lines changed

.github/workflows/docker-image.yml

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,74 @@ on:
44
workflow_dispatch:
55
inputs:
66
tag:
7-
description: 'Tag for the Docker image'
7+
description: 'tag for the Docker image'
88
required: true
99
default: 'latest'
1010
push:
1111
branches:
1212
- 'main'
13-
paths:
14-
- '**/Dockerfile'
15-
1613
jobs:
14+
define-matrix:
15+
runs-on: ubuntu-latest
16+
outputs:
17+
tag: ${{ steps.set_tag.outputs.tag }}
18+
tag_cn: ${{ steps.set_tag.outputs.tag_cn }}
19+
build_targets: ${{ steps.get_build_matrix.outputs.build_targets }}
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
with:
24+
fetch-depth: 0
25+
- name: Set up tag
26+
id: set_tag
27+
run: |
28+
if [ -n "${{ inputs.tag }}" ]; then
29+
tag=${{ inputs.tag }}
30+
else
31+
tag=${{ github.sha }}::6
32+
fi
33+
tag_cn=$tag-cn
34+
echo "tag=$tag" >> $GITHUB_OUTPUT
35+
echo "tag_cn=$tag_cn" >> $GITHUB_OUTPUT
36+
- name: Get build matrix
37+
id: get_build_matrix
38+
run: |
39+
if [ -n "${{ inputs.tag }}" ]; then
40+
build_targets=$(bash script/get_all_dockerfile.sh)
41+
else
42+
build_targets=$(bash script/get_changed_dockerfile.sh ${{ github.event.before }} ${{ github.sha }})
43+
fi
44+
echo "build_targets=$build_targets" >> $GITHUB_OUTPUT
1745
build:
1846
runs-on: ubuntu-latest
19-
47+
needs: define-matrix
48+
strategy:
49+
matrix:
50+
build_target: ${{ fromJson(needs.define-matrix.outputs.build_targets) }}
2051
steps:
2152
- name: Checkout code
2253
uses: actions/checkout@v4
2354
with:
2455
fetch-depth: 0
25-
2656
- name: Set up QEMU
2757
uses: docker/setup-qemu-action@v3
28-
29-
- name: Set up tag
30-
id: set_tag
31-
run: |
32-
if [ -n "$INPUT_TAG" ]; then
33-
TAG=$INPUT_TAG
34-
else
35-
TAG=${COMMIT_ID::6}
36-
fi
37-
CN_TAG=$INPUT_TAG-cn
38-
39-
echo "TAG=$TAG" >> $GITHUB_ENV
40-
echo "CN_TAG=$CN_TAG" >> $GITHUB_ENV
41-
echo "TAG=$TAG" >> $GITHUB_OUTPUT
42-
echo "CN_TAG=$CN_TAG" >> $GITHUB_OUTPUT
43-
44-
- name: Get changed files
45-
id: getfile
46-
run: |
47-
bash script/get_changed_files.sh ${{ github.event.before }} ${{ github.sha }}
48-
shell: bash
49-
50-
- name: Echo output
51-
run: |
52-
echo "Changed files: ${{ env.DIFF_OUTPUT }}"
53-
echo "Parent directories: ${{ env.PARENT_DIRS }}"
54-
5558
- name: Set up Docker Buildx
5659
uses: docker/setup-buildx-action@v3
57-
5860
- name: Login to ghcr.io
5961
uses: docker/login-action@v3
6062
with:
6163
registry: ghcr.io
6264
username: ${{ github.repository_owner }}
6365
password: ${{ secrets.GH_PAT }}
64-
6566
- name: Build and push image
6667
run: |
67-
bash script/build_and_push_images.sh ${{ steps.set_tag.outputs.TAG }} ${{ steps.set_tag.outputs.CN_TAG }}
68-
env:
69-
USERNAME: ${{ github.repository_owner }}
70-
COMMIT_ID: ${{ github.sha }}
71-
shell: bash
72-
73-
- uses: actions/checkout@v3
74-
- name: generate runtime yaml
75-
run: |
76-
bash script/generate_runtime_yaml.sh ${{ steps.set_tag.outputs.TAG }} ${{ steps.set_tag.outputs.CN_TAG }}
77-
bash script/generate_json.sh ${{ steps.set_tag.outputs.TAG }} ${{ steps.set_tag.outputs.CN_TAG }}
78-
env:
79-
DOCKER_USERNAME: ${{ github.repository_owner }}
80-
COMMIT_ID: ${{ github.sha }}
81-
shell: bash
82-
83-
- uses: stefanzweifel/git-auto-commit-action@v5
84-
with:
85-
commit_message: auto generate runtime yaml
68+
echo "build_target=$build_target"
69+
echo "tag=${{ needs.define-matrix.outputs.tag }}"
70+
echo "tag_cn=${{ needs.define-matrix.outputs.tag_cn }}"
71+
image_name=$(bash script/get_image_name.sh ${{ github.repository_owner }} $build_target ${{ needs.define-matrix.outputs.tag }})
72+
image_name_cn=$(bash script/get_image_name.sh ${{ github.repository_owner }} $build_target ${{ needs.define-matrix.outputs.tag_cn }})
73+
echo "image_name=$image_name" >> $GITHUB_OUTPUT
74+
echo "image_name_cn=$image_name_cn" >> $GITHUB_OUTPUT
75+
is_cn=0 bash script/build_and_push_images.sh $build_target $image_name $is_cn
76+
is_cn=1 bash script/build_and_push_images.sh $build_target $image_name_cn $is_cn
77+
# TODO: generate runtime yaml and json

Language/c/gcc-12.2.0/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM ghcr.io/labring-actions/devbox/debian-ssh-12.6:547a61
22

33
RUN cd /home/devbox/project && \
4-
rm -rf ./*
4+
rm -rf ./*
55

66
COPY /Language/c/project /home/devbox/project
77

@@ -12,5 +12,5 @@ RUN apt-get update && \
1212
chown -R devbox:devbox /home/devbox/project && \
1313
chmod -R u+rw /home/devbox/project && \
1414
chmod -R +x /home/devbox/project/entrypoint.sh
15-
16-
RUN mkdir /root/.devbox
15+
16+
RUN mkdir /root/.devbox

OS/debian-ssh/12.6/Dockerfiletmp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
FROM ghcr.io/labring-actions/devbox/debian:ce4733
2+
3+
COPY /script/startup.sh /usr/start/startup.sh
4+
5+
RUN chmod +x /usr/start/startup.sh && \
6+
apt-get update && \
7+
apt-get install -y \
8+
dumb-init \
9+
wget \
10+
openssh-client \
11+
openssh-server && \
12+
apt-get clean && \
13+
rm -rf /var/lib/apt/lists/* && \
14+
mkdir -p /run/sshd && \
15+
chmod 755 /run/sshd && \
16+
echo 'AllowTcpForwarding yes' >> /etc/ssh/sshd_config && \
17+
echo 'GatewayPorts yes' >> /etc/ssh/sshd_config && \
18+
echo 'X11Forwarding yes' >> /etc/ssh/sshd_config && \
19+
echo 'Port 22' >> /etc/ssh/sshd_config && \
20+
echo 'AuthorizedKeysFile /usr/start/.ssh/authorized_keys' >> /etc/ssh/sshd_config && \
21+
useradd -m -s /bin/bash devbox && \
22+
usermod -aG sudo devbox && \
23+
echo 'devbox ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers && \
24+
rm -rf /tmp/* && \
25+
mkdir -p /home/devbox/.ssh && \
26+
echo "devbox:devbox" | sudo chpasswd && \
27+
chown -R devbox:devbox /home/devbox/.ssh && \
28+
chmod -R 770 /home/devbox/.ssh
29+
30+
USER devbox
31+
COPY /OS/debian-ssh/project /home/devbox/project
32+
RUN sudo chown -R devbox:devbox /home/devbox/project && \
33+
sudo chmod -R 777 /home/devbox/project
34+
35+
USER root
36+
37+
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
38+
CMD ["sudo", "-E", "/usr/start/startup.sh"]
39+
40+
WORKDIR /home/devbox/project
41+
EXPOSE 22
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
./hello.sh
1+
#!/bin/bash
2+
3+
./hello.sh

script/build_and_push_images.sh

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,24 @@
11
#!/bin/bash
22

3-
# Print the input parent directories and difference output
4-
echo "PARENT_DIRS=$PARENT_DIRS"
5-
echo "DIFF_OUTPUT=$DIFF_OUTPUT"
6-
7-
# Get tags from the input parameters
8-
TAG=$1
9-
CN_TAG=$2
10-
11-
# Read the differences and parent directories into arrays
12-
IFS=',' read -r -a DIFF_OUTPUT_ARRAY <<< "$DIFF_OUTPUT"
13-
IFS=',' read -r -a PARENT_DIRS_ARRAY <<< "$PARENT_DIRS"
14-
15-
# Print the contents of the arrays for debugging
16-
echo "DIFF_OUTPUT array: ${DIFF_OUTPUT_ARRAY[@]}"
17-
echo "PARENT_DIRS array: ${PARENT_DIRS_ARRAY[@]}"
18-
19-
# Function to build and push Docker images
20-
build_and_push_image() {
21-
local dockerfile_path=$1
22-
local image_name=$2
3+
build_target=$1
4+
image_name=$2
5+
is_cn=$3
236

7+
function build_and_push_image() {
248
docker buildx build --push \
25-
--file "$dockerfile_path" \
9+
--file "$build_target" \
2610
--platform linux/amd64 \
27-
--tag "ghcr.io/$USERNAME/devbox/$image_name" \
11+
--tag "$image_name" \
2812
.
2913
}
3014

31-
# Iterate over Dockerfile paths
32-
for i in "${!DIFF_OUTPUT_ARRAY[@]}"; do
33-
DOCKERFILE_PATH=${DIFF_OUTPUT_ARRAY[$i]}
34-
PARENT_DIR=${PARENT_DIRS_ARRAY[$i]}
35-
36-
# Extract parent path for further use
37-
parent_path="${DOCKERFILE_PATH%/*}"
38-
parent_path=$(dirname "$parent_path")
39-
40-
# Create the English image name
41-
IFS='/' read -ra ADDR <<< "$DOCKERFILE_PATH"
42-
EN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$TAG"
43-
44-
# Build and push the English Docker image
45-
build_and_push_image "$DOCKERFILE_PATH" "$EN_IMAGE_NAME"
46-
47-
# Check for the Chinese Dockerfile update script and handle accordingly
48-
if [ -f "$parent_path/update_cn_dockerfile.sh" ]; then
49-
bash "$parent_path/update_cn_dockerfile.sh" "$DOCKERFILE_PATH"
50-
CN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$CN_TAG"
15+
function execute_cn_patch() {
16+
script_dir=$(dirname "$(dirname "$build_target")")
17+
bash "$script_dir/update_cn_dockerfile.sh" "$build_target"
18+
}
5119

52-
# Use a temporary Dockerfile for the Chinese image
53-
TEMP_DOCKERFILE="${DOCKERFILE_PATH}tmp"
54-
55-
# Build and push the Chinese Docker image
56-
build_and_push_image "$TEMP_DOCKERFILE" "$CN_IMAGE_NAME"
20+
if [ "$is_cn" -eq 1 ]; then
21+
execute_cn_patch
22+
fi
5723

58-
# Remove the temporary Dockerfile
59-
rm "$TEMP_DOCKERFILE"
60-
fi
61-
done
24+
build_and_push_image

script/get_all_dockerfile.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
DOCKERFILES=$(find . -name "Dockerfile" | sed 's|^\./||')
3+
ARRAY_OUTPUT="["
4+
FIRST=true
5+
6+
for DOCKERFILE in $DOCKERFILES; do
7+
if [ "$FIRST" = true ]; then
8+
ARRAY_OUTPUT+="\"$DOCKERFILE\""
9+
FIRST=false
10+
else
11+
ARRAY_OUTPUT+=",\"$DOCKERFILE\""
12+
fi
13+
done
14+
15+
ARRAY_OUTPUT+="]"
16+
echo "$ARRAY_OUTPUT"

script/get_changed_dockerfile.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
if [ "$#" -ne 2 ]; then
4+
echo "Usage: $0 <commit1> <commit2>"
5+
exit 1
6+
fi
7+
8+
DIFF_OUTPUT=$(git diff --name-only "$1" "$2"|grep Dockerfile)
9+
10+
ARRAY_OUTPUT="["
11+
FIRST=true
12+
13+
for FILE_PATH in $DIFF_OUTPUT; do
14+
if [[ ! -f "$FILE_PATH" ]]; then
15+
echo "File $FILE_PATH does not exist, skipping."
16+
continue
17+
fi
18+
19+
if [ "$FIRST" = true ]; then
20+
ARRAY_OUTPUT+="\"$FILE_PATH\""
21+
FIRST=false
22+
else
23+
ARRAY_OUTPUT+=",\"$FILE_PATH\""
24+
fi
25+
done
26+
27+
ARRAY_OUTPUT+="]"
28+
echo "$ARRAY_OUTPUT"

script/get_changed_files.sh

Lines changed: 0 additions & 28 deletions
This file was deleted.

script/get_image_name.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
if [ "$#" -ne 3 ]; then
4+
echo "Usage: $0 <username> <build_target> <tag>"
5+
exit 1
6+
fi
7+
8+
USERNAME=$1
9+
BUILD_TARGET=$2
10+
TAG=$3
11+
12+
IFS='/' read -ra ADDR <<< "$BUILD_TARGET"
13+
IMAGE_NAME="${ADDR[1]}-${ADDR[2]}"
14+
15+
echo "ghcr.io/$USERNAME/devbox/$IMAGE_NAME:$TAG"

0 commit comments

Comments
 (0)