Skip to content

Commit

Permalink
pref: update shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
cbluebird committed Oct 9, 2024
1 parent d464910 commit 93816ee
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 110 deletions.
53 changes: 36 additions & 17 deletions script/build_and_push_images.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,61 @@
#!/bin/bash

# Print the input parent directories and difference output
echo "PARENT_DIRS=$PARENT_DIRS"
echo "DIFF_OUTPUT=$DIFF_OUTPUT"

# Get tags from the input parameters
TAG=$1
CN_TAG=$2

# Read the differences and parent directories into arrays
IFS=',' read -r -a DIFF_OUTPUT_ARRAY <<< "$DIFF_OUTPUT"
IFS=',' read -r -a PARENT_DIRS_ARRAY <<< "$PARENT_DIRS"

# Print the contents of the arrays for debugging
echo "DIFF_OUTPUT array: ${DIFF_OUTPUT_ARRAY[@]}"
echo "PARENT_DIRS array: ${PARENT_DIRS_ARRAY[@]}"

# Function to build and push Docker images
build_and_push_image() {
local dockerfile_path=$1
local image_name=$2

docker buildx build --push \
--file "$dockerfile_path" \
--platform linux/amd64 \
--tag "ghcr.io/$USERNAME/devbox/$image_name" \
.
}

# Iterate over Dockerfile paths
for i in "${!DIFF_OUTPUT_ARRAY[@]}"; do
DOCKERFILE_PATH=${DIFF_OUTPUT_ARRAY[$i]}
PARENT_DIR=${PARENT_DIRS_ARRAY[$i]}

# Extract parent path for further use
parent_path="${DOCKERFILE_PATH%/*}"
parent_path=$(dirname "$parent_path")

IFS='/' read -ra ADDR <<< $DOCKERFILE_PATH
PARENT_DIR=${PARENT_DIRS_ARRAY[$i]}
# Create the English image name
IFS='/' read -ra ADDR <<< "$DOCKERFILE_PATH"
EN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$TAG"

# Build and push the English Docker image
build_and_push_image "$DOCKERFILE_PATH" "$EN_IMAGE_NAME"

docker buildx build --push \
--file $DOCKERFILE_PATH \
--platform linux/amd64\
--tag "ghcr.io/$USERNAME/devbox/$EN_IMAGE_NAME" \
.

# Check for the Chinese Dockerfile update script and handle accordingly
if [ -f "$parent_path/update_cn_dockerfile.sh" ]; then
bash "$parent_path/update_cn_dockerfile.sh" $DOCKERFILE_PATH
CN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$CN_TAG"
docker buildx build --push \
--file $DOCKERFILE_PATH"tmp" \
--platform linux/amd64\
--tag "ghcr.io/$USERNAME/devbox/$CN_IMAGE_NAME" \
.
bash "$parent_path/update_cn_dockerfile.sh" "$DOCKERFILE_PATH"
CN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$CN_TAG"

# Use a temporary Dockerfile for the Chinese image
TEMP_DOCKERFILE="${DOCKERFILE_PATH}tmp"

# Build and push the Chinese Docker image
build_and_push_image "$TEMP_DOCKERFILE" "$CN_IMAGE_NAME"

rm $DOCKERFILE_PATH"tmp"
# Remove the temporary Dockerfile
rm "$TEMP_DOCKERFILE"
fi
done
done
158 changes: 70 additions & 88 deletions script/generate_runtime_yaml.sh
Original file line number Diff line number Diff line change
@@ -1,71 +1,56 @@
#!/bin/bash

echo "PARENT_DIRS=$PARENT_DIRS"
echo "DIFF_OUTPUT=$DIFF_OUTPUT"

# Define tags from input parameters
TAG=$1
CN_TAG=$2

# Read the Dockerfile differences and parent directories into arrays
IFS=',' read -r -a DIFF_OUTPUT_ARRAY <<< "$DIFF_OUTPUT"
IFS=',' read -r -a PARENT_DIRS_ARRAY <<< "$PARENT_DIRS"

declare -A NAME_MAP
while IFS='=' read -r key value; do
NAME_MAP["$key"]="$value"
done < "configs/name.txt"

declare -A PORT_MAP
while IFS='=' read -r key value; do
PORT_MAP["$key"]="$value"
done < "configs/port.txt"

for i in "${!DIFF_OUTPUT_ARRAY[@]}"; do
DOCKERFILE_PATH=${DIFF_OUTPUT_ARRAY[$i]}
IFS='/' read -ra ADDR <<< $DOCKERFILE_PATH

PARENT_DIR=${PARENT_DIRS_ARRAY[$i]}
# Load name and port mappings into associative arrays
declare -A NAME_MAP PORT_MAP

if [ "${NAME_MAP[${ADDR[1]}]}" == "none" ]; then
exit 1
fi
# Function to load mappings from a config file into an associative array
load_mappings() {
local file=$1
local -n map=$2
while IFS='=' read -r key value; do
map["$key"]="$value"
done < "$file"
}

YAML_PATH="${DOCKERFILE_PATH%/*}"
parent_path=$(dirname "$YAML_PATH")
# Load the name and port mappings
load_mappings "configs/name.txt" NAME_MAP
load_mappings "configs/port.txt" PORT_MAP

EN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$TAG"
if [ -f "$parent_path/update_cn_dockerfile.sh" ]; then
CN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$CN_TAG"
else
CN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$TAG"
fi
# Create YAML output directory structure
mkdir -p yaml/en
mkdir -p yaml/cn

mkdir -p "yaml/en/${YAML_PATH}"
mkdir -p "yaml/cn/${YAML_PATH}"
en_output_file="yaml/en/${YAML_PATH}/$PARENT_DIR.yaml"
cn_output_file="yaml/cn/${YAML_PATH}/$PARENT_DIR.yaml"
if [ ! -f "$en_output_file" ]; then
touch "$en_output_file"
fi
if [ ! -f "$cn_output_file" ]; then
touch "$cn_output_file"
fi
# Function to generate YAML configuration and write it to a file
generate_yaml() {
local output_file=$1
local image_name=$2
local parent_dir=$3
local addr=("$@") # remaining arguments are passed as an array

cat << EOF > "$en_output_file"
cat << EOF > "$output_file"
apiVersion: devbox.sealos.io/v1alpha1
kind: Runtime
metadata:
name: ${ADDR[1]}-${PARENT_DIR//./-}
name: ${addr[1]}-${parent_dir//./-}
namespace: devbox-system
spec:
classRef: ${ADDR[1]}
classRef: ${addr[1]}
config:
image: ghcr.io/$DOCKER_USERNAME/devbox/$EN_IMAGE_NAME
image: ghcr.io/$DOCKER_USERNAME/devbox/$image_name
ports:
- containerPort: 22
name: devbox-ssh-port
protocol: TCP
appPorts:
- port: ${PORT_MAP[${ADDR[1]}]}
- port: ${PORT_MAP[${addr[1]}]}
name: devbox-app-port
protocol: TCP
user: sealos
Expand All @@ -75,55 +60,52 @@ spec:
- -c
releaseArgs:
- /home/sealos/project/entrypoint.sh
description: ${ADDR[1]} $PARENT_DIR
version: "$PARENT_DIR"
description: ${addr[1]} $parent_dir
version: "$parent_dir"
---
apiVersion: devbox.sealos.io/v1alpha1
kind: RuntimeClass
metadata:
name: ${ADDR[1]}
name: ${addr[1]}
spec:
title: "${NAME_MAP[${ADDR[1]}]}"
kind: ${ADDR[0]}
description: ${ADDR[1]}
title: "${NAME_MAP[${addr[1]}]}"
kind: ${addr[0]}
description: ${addr[1]}
EOF
}

cat << EOF > "$cn_output_file"
apiVersion: devbox.sealos.io/v1alpha1
kind: Runtime
metadata:
name: ${ADDR[1]}-${PARENT_DIR//./-}
namespace: devbox-system
spec:
classRef: ${ADDR[1]}
config:
image: ghcr.io/$DOCKER_USERNAME/devbox/$CN_IMAGE_NAME
ports:
- containerPort: 22
name: devbox-ssh-port
protocol: TCP
appPorts:
- port: ${PORT_MAP[${ADDR[1]}]}
name: devbox-app-port
protocol: TCP
user: sealos
workingDir: /home/sealos/project
releaseCommand:
- /bin/bash
- -c
releaseArgs:
- /home/sealos/project/entrypoint.sh
description: ${ADDR[1]} $PARENT_DIR
version: "$PARENT_DIR"
---
apiVersion: devbox.sealos.io/v1alpha1
kind: RuntimeClass
metadata:
name: ${ADDR[1]}
spec:
title: "${NAME_MAP[${ADDR[1]}]}"
kind: ${ADDR[0]}
description: ${ADDR[1]}
EOF
# Iterate over the changed Dockerfiles
for i in "${!DIFF_OUTPUT_ARRAY[@]}"; do
DOCKERFILE_PATH=${DIFF_OUTPUT_ARRAY[$i]}
IFS='/' read -ra ADDR <<< "$DOCKERFILE_PATH"
PARENT_DIR=${PARENT_DIRS_ARRAY[$i]}

# Check if the name is mapped to "none"
if [[ "${NAME_MAP[${ADDR[1]}]}" == "none" ]]; then
exit 1
fi

# Define paths for YAML files and the image names
YAML_PATH="${DOCKERFILE_PATH%/*}"
EN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$TAG"
CN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$CN_TAG"

# Check if update_cn_dockerfile.sh exists to decide CN image name
if [ -f "$(dirname "$DOCKERFILE_PATH")/update_cn_dockerfile.sh" ]; then
CN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$CN_TAG"
else
CN_IMAGE_NAME="${ADDR[1]}-$PARENT_DIR:$TAG"
fi

# Define output files
en_output_file="yaml/en/${YAML_PATH}/$PARENT_DIR.yaml"
cn_output_file="yaml/cn/${YAML_PATH}/$PARENT_DIR.yaml"

# Create the output files if they don’t exist
: > "$en_output_file"
: > "$cn_output_file"

done
# Generate and write the English and Chinese YAML configurations
generate_yaml "$en_output_file" "$EN_IMAGE_NAME" "$PARENT_DIR" "${ADDR[@]}"
generate_yaml "$cn_output_file" "$CN_IMAGE_NAME" "$PARENT_DIR" "${ADDR[@]}"
done
27 changes: 22 additions & 5 deletions script/get_changed_files.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
#!/bin/bash

DIFF_OUTPUT=$(git diff --name-only "$1" "$2"|grep Dockerfile)
# Check the number of input parameters
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <commit1> <commit2>"
exit 1
fi

# Get the list of Dockerfile differences between the two commits
DIFF_OUTPUT=$(git diff --name-only "$1" "$2" | grep Dockerfile)

# Initialize arrays to store parent directories and file paths
PARENT_DIRS=()
FILE_PATHS=()

# Iterate over the changed files
for FILE_PATH in $DIFF_OUTPUT; do
# Check if the file exists
if [[ ! -f "$FILE_PATH" ]]; then
echo "File $FILE_PATH does not exist, skipping."
echo "File '$FILE_PATH' does not exist, skipping."
continue
fi
PARENT_DIR=$(dirname "$FILE_PATH" | awk -F'/' '{print $(NF)}')

# Get the name of the parent directory of the file
PARENT_DIR=$(basename "$(dirname "$FILE_PATH")")
PARENT_DIRS+=("$PARENT_DIR")
FILE_PATHS+=("$FILE_PATH")
done

# Convert arrays to comma-separated strings
PARENT_DIRS_STRING=$(IFS=,; echo "${PARENT_DIRS[*]}")
DIFF_OUTPUT_STRING=$(IFS=,; echo "${FILE_PATHS[*]}")

# Print results to the console
echo "PARENT_DIRS=$PARENT_DIRS_STRING"
echo "DIFF_OUTPUT=$DIFF_OUTPUT_STRING"

echo "PARENT_DIRS=$PARENT_DIRS_STRING" >> $GITHUB_ENV
echo "DIFF_OUTPUT=$DIFF_OUTPUT_STRING" >> $GITHUB_ENV
# Write results to environment variables
echo "PARENT_DIRS=$PARENT_DIRS_STRING" >> "$GITHUB_ENV"
echo "DIFF_OUTPUT=$DIFF_OUTPUT_STRING" >> "$GITHUB_ENV"

0 comments on commit 93816ee

Please sign in to comment.