Skip to content

Commit

Permalink
chore: update build scripts & makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
BrooksYang committed May 18, 2024
1 parent 2b84044 commit aabccdb
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 33 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ VERSION_PACKAGE=github.com/bingo-project/component-base/version
# make sure include common.mk at the first include line
include scripts/make-rules/common.mk
include scripts/make-rules/golang.mk
include scripts/make-rules/image.mk
include scripts/make-rules/tools.mk
include scripts/make-rules/generate.mk
include scripts/make-rules/swagger.mk
Expand Down Expand Up @@ -67,6 +68,10 @@ deps: ## 安装依赖,例如:生成需要的代码、安装需要的工具
build: tidy protoc
@$(MAKE) go.build

.PHONY: image
image: ## Build docker images for host arch.
@$(MAKE) image.build

## --------------------------------------
## Cleanup
## --------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion scripts/docker/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ build() {
cd build/docker || exit
cp .env.example .env
docker-compose build
docker save "${images[@]}" | gzip >bingo-images.tar.gz
docker save "${images[@]}" | gzip >"$app_name"-images.tar.gz

cd - || exit
mkdir -p _output
Expand Down
41 changes: 17 additions & 24 deletions scripts/docker/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@

# Source
ROOT_PATH=$(dirname "${BASH_SOURCE[0]}")/../..
source "${ROOT_PATH}/scripts/lib/ssh-agent.sh"
source "${ROOT_PATH}/scripts/lib/env.sh"
source "${ROOT_PATH}/scripts/docker/env.sh"

# Params
tag=$1
remote_user=$2
remote_host=$3
remote_key=$4
remote_user=$1
remote_host=$2
tag=$3

# Validate params
validate_params() {
# Default params
if [ -z "$1" ]; then
tag="$version"
fi
if [ -z "$2" ]; then
remote_user=$SSH_USER
fi
if [ -z "$3" ]; then
if [ -z "$2" ]; then
remote_host=$SSH_HOST
fi
if [ -z "$4" ]; then
remote_key=$SSH_KEY
if [ -z "$3" ]; then
tag="$version"
fi

if [ -z "$remote_user" ]; then
Expand All @@ -35,40 +30,38 @@ validate_params() {
echo "validation failed: remote_host is empty."
exit 1
fi

# if [ -z "$remote_key" ]; then
# echo "validation failed: remote_key is empty."
# exit 1
# fi
}

package() {
cd build/docker || exit

# config file
mkdir config
cp -a ../../configs/*.example.yaml config
mkdir -p config _output
cp -a ../../configs/*.yaml config

# docker-compose
tar -czvpf "$app_name"-docker.tar.gz * .env.example

rm -r config
cd - || exit
mv build/docker/*.tar.gz _output
mv build/docker/*.tar.gz _output/

ls -lh _output/
}

scp_to_remote() {
scp _output/*.tar.gz "$remote_user"@"$remote_host":/tmp/
echo "start scp to remote..."
scp -o StrictHostKeyChecking=no _output/*.tar.gz "$remote_user"@"$remote_host":/tmp/
# rm _output/*.tar.gz
}

deploy_to_remote() {
ssh "$remote_user"@"$remote_host" 'bash -s' "$app_name" "$tag" <./scripts/docker/run.sh
echo "start deploy to remote..."
ssh -o StrictHostKeyChecking=no "$remote_user"@"$remote_host" 'bash -s' "$app_name" "$tag" <./scripts/docker/run.sh
}

# Run
validate_params "$1" "$2" "$3" "$4"
start_ssh_agent
validate_params "$1" "$2" "$3"
package
scp_to_remote
deploy_to_remote
25 changes: 18 additions & 7 deletions scripts/docker/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ validate_params() {

install_docker() {
if ! command -v docker &>/dev/null; then
apt update && apt install -y -qq docker.io docker-compose
apt update && apt install -y -qq docker.io
fi

if ! command -v docker-compose &>/dev/null; then
curl -SL https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-linux-x86_64 \
-o /usr/local/bin/docker-compose &&
chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
fi
}

Expand Down Expand Up @@ -47,13 +53,18 @@ run() {
sed -i "s/APP_VERSION=.*/APP_VERSION=${tag}/g" .env
fi

# Load and tag latest
loaded=$(docker load </tmp/"${app_name}"-images.tar.gz)
for image_with_version in $(echo "$loaded" | awk -F ': ' '{print $2}'); do
image=${image_with_version%:*}
docker tag "$image_with_version" "$image":latest
done
# Load images
docker load </tmp/"${app_name}"-images.tar.gz

# tag latest
# echo "tag latest"
# loaded=$(docker load </tmp/"${app_name}"-images.tar.gz)
# for image_with_version in $(echo "$loaded" | awk -F ': ' '{print $2}'); do
# image=${image_with_version%:*}
# docker tag "$image_with_version" "$image":latest
# done

echo "docker-compose up -d"
docker-compose up -d

rm /tmp/"${app_name}"*.tar.gz
Expand Down
8 changes: 7 additions & 1 deletion scripts/install/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ install_git() {
# Docker
install_docker() {
if ! command -v docker &>/dev/null; then
apt update && apt install -y -qq docker.io docker-compose
apt update && apt install -y -qq docker.io
fi

if ! command -v docker-compose &>/dev/null; then
curl -SL https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-linux-x86_64 \
-o /usr/local/bin/docker-compose && \
chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
fi
}

Expand Down
33 changes: 33 additions & 0 deletions scripts/make-rules/image.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# ==============================================================================
# Makefile helper functions for docker image
#

DOCKER := docker

# Docker version
TAG := $(VERSION:v%=%)

# Image build related variables.
REGISTRY_PREFIX ?= bingo

# Determine image files by looking into cmd/*
IMAGES_DIR ?= $(wildcard ${ROOT_DIR}/cmd/*)
# Determine images names by stripping out the dir names
IMAGES ?= $(filter-out tools,$(foreach image,${IMAGES_DIR},$(notdir ${image})))

ifeq (${IMAGES},)
$(error Could not determine IMAGES, set ONEX_ROOT or run in source dir)
endif

.PHONY: image.build
image.build:
$(ROOT_DIR)/scripts/docker/build.sh -a amd64

.PHONY: image.push
image.push: $(addprefix image.push., $(IMAGES))

.PHONY: image.push.%
image.push.%: image.build.% ## Build and push specified docker image.
$(eval IMAGE := $*)
@echo "===========> Pushing image $(IMAGE) $(TAG) to $(REGISTRY_PREFIX)"
$(DOCKER) push $(REGISTRY_PREFIX)/$(IMAGE):$(TAG)

0 comments on commit aabccdb

Please sign in to comment.