调整dockerfile及构建仓库地址

This commit is contained in:
Akira
2021-01-16 21:39:54 +08:00
parent 1c8598bd1e
commit 533b6adde6
5 changed files with 95 additions and 235 deletions

34
.github/workflows/docker_build.yml vendored Normal file
View File

@@ -0,0 +1,34 @@
name: 构建JD Scripts镜像
on:
#防止fork乱用action设置只能手动触发构建
workflow_dispatch:
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@master
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: 构建并推送到Dockerhub镜像仓库
uses: docker/build-push-action@v2
with:
context: .
file: ./jd_scripts/Dockerfile
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64
push: true
tags: |
lxk0301/jd_scripts:latest

View File

@@ -1,32 +1,28 @@
FROM alpine
LABEL AUTHOR="Akira <e.akimoto.akira@gmail.com>" \
VERSION=0.1.2 \
UPDATE_CONTENT="更新内容较多,重新阅读仓库[Readme](https://github.com/LXK9301/jd_scripts/tree/master/docker),更新镜像并更新配置后使用。"
LABEL AUTHOR="none" \
VERSION=0.1.3
ENV DEFAULT_LIST_FILE=crontab_list.sh \
CUSTOM_LIST_MERGE_TYPE=append \
REPO_URL=https://gitee.com/lxk0301/jd_scripts.git
RUN set -ex \
&& apk update && apk upgrade\
&& apk add --no-cache tzdata moreutils git nodejs npm curl jq\
&& apk add --no-cache tzdata git nodejs moreutils npm curl jq \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
RUN git clone https://gitee.com/lxk0301/jd_scripts.git /scripts \
RUN git clone ${REPO_URL} /scripts \
&& cd /scripts \
&& git checkout master \
&& mkdir logs \
&& npm install \
&& cd /tmp \
&& npm install request
&& npm install
ENV BUILD_VERSION=0.1.2 \
DEFAULT_LIST_FILE=crontab_list.sh \
CUSTOM_LIST_MERGE_TYPE=append
# github action 构建
COPY ./docker/docker_entrypoint.sh /usr/local/bin
# 本地构建
# COPY ./docker_entrypoint.sh /usr/local/bin
RUN chmod +x /usr/local/bin/docker_entrypoint.sh
RUN cp /scripts/docker/docker_entrypoint.sh /usr/local/bin \
&& chmod +x /usr/local/bin/docker_entrypoint.sh
WORKDIR /scripts
ENTRYPOINT ["docker_entrypoint.sh"]
CMD [ "crond" ]

View File

@@ -1,5 +1,5 @@
#必须要的默认定时任务请勿删除
52 */1 * * * sh /scripts/docker/default_task.sh >> /scripts/logs/default_task.log 2>&1
52 */1 * * * docker_entrypoint.sh >> /scripts/logs/default_task.log 2>&1
# 每3天的23:50分清理一次日志
50 23 */3 * * rm -rf /scripts/logs/*.log

View File

@@ -1,122 +1,62 @@
#!/bin/sh
set -e
echo "定时任务更新代码git 拉取最新代码,并安装更新依赖..."
git -C /scripts pull
npm install --prefix /scripts
######################################获取docker构建文件里面的自定义信息方法-start#####################################################
function getDockerImageLabel() {
repo=akyakya/jd_scripts
imageTag=latest
token=$(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${repo}:pull" | jq -r '.token')
digest=$(curl -s -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $token" "https://registry-1.docker.io/v2/${repo}/manifests/${imageTag}" | jq .config.digest -r)
labels=$(curl -s -L -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $token" "https://registry-1.docker.io/v2/${repo}/blobs/$digest" | jq .config.Labels)
echo $labels
}
######################################获取docker构建文件里面的自定义信息方法-end#####################################################
######################################对比版本版本号大小方法-start###################################################################
function version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"
}
######################################对比版本版本号大小方法-end###################################################################
#######################################通知用户更新镜像-start#####################################################################
echo "检查docker镜像更新更新..."
if type jq >/dev/null 2>&1; then
echo "获取dockerhub仓库镜像labels信息..."
labels=$(getDockerImageLabel)
export NOTIFY_CONTENT=$(echo $labels | jq -r .UPDATE_CONTENT)
version=$(echo $labels | jq -r .VERSION)
else
# 第一版通知逻辑无法包含在上面判断里面,镜像构建好直接开启通知
echo "当前容版版本过旧,发送镜像更新通知"
export NOTIFY_CONTENT="更新内容较多重新阅读仓库Readme(https://github.com/LXK9301/jd_scripts/tree/master/docker),更新镜像并更新配置后使用。"
cd /scripts/docker
node notify_docker_user.js
fi
#通知通知用户更新镜像
if [ ! $BUILD_VERSION ]; then
if [ $version ]; then
echo "当前容器版本为空dockerhub仓库版本为$version,发送通知"
cd /scripts/docker
node notify_docker_user.js
fi
else
if version_gt $version $BUILD_VERSION; then
echo "当前容器版本为$BUILD_VERSIONdockerhub仓库版本为$version,发送通知"
cd /scripts/docker
node notify_docker_user.js
fi
fi
#######################################通知用户更新镜像-end#####################################################################
##兼容旧镜像的环境变量
if [ !$DEFAULT_LIST_FILE ]; then
defaultListFile="/scripts/docker/crontab_list.sh"
else
echo "定义定时任务合并处理用到的文件路径..."
defaultListFile="/scripts/docker/$DEFAULT_LIST_FILE"
fi
echo "默认文件定时任务文件路径为 ${defaultListFile}"
if [ $CUSTOM_LIST_FILE ]; then
customListFile="/scripts/docker/$CUSTOM_LIST_FILE"
mergedListFile="/scripts/docker/merged_list_file.sh"
if type ts >/dev/null 2>&1; then
echo '系统已安装moreutils工具包默认定时任务增加ts 输出'
##复制一个新文件来追加|ts防止git pull的时候冲突
cp $defaultListFile /scripts/docker/default_list.sh
defaultListFile="/scripts/docker/default_list.sh"
sed -i 's/>>/|ts >>/g' $defaultListFile
echo "自定义定时任务文件路径为 ${customListFile}"
fi
mergedListFile="/scripts/docker/merged_list_file.sh"
echo "合并后定时任务文件路径为 ${mergedListFile}"
echo "定时任务合并加载最新定时任务列表..."
#判断 自定义文件是否存在 是否存在
echo "第3步将默认定时任务列表添加到并后定时任务文件..."
cat $defaultListFile >$mergedListFile
echo "第2步判断是否存在自定义任务任务列表并追加..."
if [ $CUSTOM_LIST_FILE ]; then
echo "您配置了自定义任务文件:$CUSTOM_LIST_FILE,自定义任务类型为:$CUSTOM_LIST_MERGE_TYPE..."
if [ -f "$customListFile" ]; then
if [ $CUSTOM_LIST_MERGE_TYPE == "append" ]; then
echo "合并默认定时任务文件:$DEFAULT_LIST_FILE 和 自定义定时任务文件:$CUSTOM_LIST_FILE"
cat $defaultListFile >$mergedListFile
echo -e "" >>$mergedListFile
cat $customListFile >>$mergedListFile
elif [ $CUSTOM_LIST_MERGE_TYPE == "overwrite" ]; then
cat $customListFile >$mergedListFile
echo "$CUSTOM_LIST_FILE but file ..."
echo "配置了自定义任务文件:$CUSTOM_LIST_FILE,自定义任务类型为:$CUSTOM_LIST_MERGE_TYPE..."
touch "$customListFile"
cat $customListFile >$mergedListFile
else
echo "配置配置了错误的自定义定时任务类型:$CUSTOM_LIST_MERGE_TYPE自定义任务类型为只能为append或者overwrite..."
cat $defaultListFile >$mergedListFile
fi
else
echo "配置的自定义任务文件:$CUSTOM_LIST_FILE未找到,使用默认配置$DEFAULT_LIST_FILE..."
cat $defaultListFile >$mergedListFile
fi
else
echo "当前使用的为默认定时任务文件 $DEFAULT_LIST_FILE ..."
cat $defaultListFile >$mergedListFile
echo "当前使用默认定时任务文件 $DEFAULT_LIST_FILE ..."
fi
# 判断最后要加载的定时任务是否包含默认定时任务,不包含的话就加进去
echo "第3步判断是否配置了默认脚本更新任务..."
if [ $(grep -c "default_task.sh" $mergedListFile) -eq '0' ]; then
echo "合并后的定时任务文件,未包含必须的默认定时任务,增加默认定时任务..."
echo -e >>$mergedListFile
echo "52 */1 * * * sh /scripts/docker/default_task.sh |ts >> /scripts/logs/default_task.log 2>&1" >>$mergedListFile
echo "52 */1 * * * docker_entrypoint.sh >> /scripts/logs/default_task.log 2>&1" >>$mergedListFile
else
sed -i "/default_task.sh/d" $mergedListFile
echo "#脚本追加默认定时任务" >>$mergedListFile
echo "52 */1 * * * docker_entrypoint.sh >> /scripts/logs/default_task.log 2>&1" >>$mergedListFile
fi
echo "第5步判断是否配置了随即延迟参数..."
if [ $RANDOM_DELAY_MAX ]; then
if [ $RANDOM_DELAY_MAX -ge 1 ]; then
echo "已设置随机延迟为 $RANDOM_DELAY_MAX , 设置延迟任务中..."
sed -i "/\(jd_bean_sign.js\|jd_blueCoin.js\|jd_joy_reward.js\|jd_joy_steal.js\|jd_joy_feedPets.js\|jd_car_exchange.js\)/!s/node/sleep \$((RANDOM % \$RANDOM_DELAY_MAX)); node/g" $mergedListFile
fi
else
echo "未配置随即延迟对应的环境变量,故不设置延迟任务"
echo "未配置随即延迟对应的环境变量,故不设置延迟任务..."
fi
##增加自定义shell脚本
echo "第6步判断是否配置自定义shell执行脚本..."
if [ 0"$CUSTOM_SHELL_FILE" = "0" ]; then
echo "未配置自定shell脚本文件跳过执行。"
else
@@ -124,6 +64,7 @@ else
echo "自定义shell脚本为远程脚本开始下在自定义远程脚本。"
wget -O /scripts/docker/shell_script_mod.sh $CUSTOM_SHELL_FILE
echo "下载完成,开始执行..."
echo "#远程自定义shell脚本追加定时任务" >>$mergedListFile
sh -x /scripts/docker/shell_script_mod.sh
echo "自定义远程shell脚本下载并执行结束。"
else
@@ -131,13 +72,21 @@ else
echo "自定义shell脚本为docker挂载脚本文件但是指定挂载文件不存在跳过执行。"
else
echo "docker挂载的自定shell脚本开始执行..."
echo "#docker挂载自定义shell脚本追加定时任务" >>$mergedListFile
sh -x $CUSTOM_SHELL_FILE
echo "docker挂载的自定shell脚本执行结束。"
fi
fi
fi
echo "加载最新的定时任务文件..."
echo "第7步增加 |ts 任务日志输出时间戳..."
sed -i "/\( ts\| |ts\|| ts\)/!s/>>/\|ts >>/g" $mergedListFile
echo "第8步执行proc_file.sh脚本任务..."
sh -x /scripts/docker/shell_script_mod.sh
echo "第9步加载最新的定时任务文件..."
crontab $mergedListFile
sh -x /scripts/docker/proc_file.sh
echo "第10步将仓库的docker_entrypoint.sh脚本更新至系统/usr/local/bin/docker_entrypoint.sh内..."
cat /scripts/docker_entrypoint.sh >/usr/local/bin/docker_entrypoint.sh

View File

@@ -1,142 +1,23 @@
#!/bin/sh
set -e
export LANG="zh_CN.UTF-8"
if [ $1 ]; then
echo "Currently does not support specifying startup parameters"
echo "Please delete the last command attached to $(docker run) or the configured $(command:) parameter in $(docker-compose.yml)"
echo "暂时不支持指定启动参数,请删除 docker run时最后附带的命令 或者 docker-compose.yml中的配置的command:指令 "
fi
echo "##############################################################################"
echo "Container start , Pull the latest code..."
echo "容器启动git 拉取最新代码..."
echo "设定远程仓库地址..."
cd /scripts
git remote set-url origin $REPO_URL
git reset --hard
echo "git pull拉取最新代码..."
git -C /scripts pull
echo "npm install 安装最新依赖"
npm install --prefix /scripts
echo "##############################################################################"
######################################获取docker构建文件里面的自定义信息方法-start#####################################################
function getDockerImageLabel() {
repo=akyakya/jd_scripts
imageTag=latest
token=$(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${repo}:pull" | jq -r '.token')
digest=$(curl -s -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $token" "https://registry-1.docker.io/v2/${repo}/manifests/${imageTag}" | jq .config.digest -r)
labels=$(curl -s -L -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $token" "https://registry-1.docker.io/v2/${repo}/blobs/$digest" | jq .config.Labels)
echo $labels
}
######################################获取docker构建文件里面的自定义信息方法-end#####################################################
######################################对比版本版本号大小方法-start###################################################################
function version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"
}
######################################对比版本版本号大小方法-end###################################################################
#######################################通知用户更新镜像-start#####################################################################
echo "check docker images update..."
echo "检查docker镜像更新更新..."
if type jq >/dev/null 2>&1; then
echo "get dockerhub repo images labels..."
echo "获取dockerhub仓库镜像labels信息..."
labels=$(getDockerImageLabel)
export NOTIFY_CONTENT=$(echo $labels | jq -r .UPDATE_CONTENT)
version=$(echo $labels | jq -r .VERSION)
else
# 第一版通知逻辑无法包含在上面判断里面,镜像构建好直接开启通知
echo "Current container version is too old, send update notification"
echo "当前版本过旧,发送镜像更新通知"
export NOTIFY_CONTENT="更新内容较多重新阅读仓库Readme(https://github.com/LXK9301/jd_scripts/tree/master/docker),更新镜像并更新配置后使用。"
cd /scripts/docker
node notify_docker_user.js
fi
#通知通知用户更新镜像
if [ ! $BUILD_VERSION ]; then
if [ $version ]; then
echo "Current container version is empty, dockerhub lastet $version, send update notification"
echo "当前容器版本为空dockerhub仓库版本为$version,发送更新通知"
cd /scripts/docker
node notify_docker_user.js
fi
else
if version_gt $version $BUILD_VERSION; then
echo "Current container version $BUILD_VERSION, dockerhub lastet version $version, send update notification"
echo "当前容器版本为$BUILD_VERSIONdockerhub仓库版本为$version,发送通知"
cd /scripts/docker
node notify_docker_user.js
fi
fi
#######################################通知用户更新镜像-end#####################################################################
##兼容旧镜像的环境变量
if [ !$DEFAULT_LIST_FILE ]; then
defaultListFile="/scripts/docker/crontab_list.sh"
else
defaultListFile="/scripts/docker/$DEFAULT_LIST_FILE"
fi
customListFile="/scripts/docker/$CUSTOM_LIST_FILE"
mergedListFile="/scripts/docker/merged_list_file.sh"
if type ts >/dev/null 2>&1; then
echo 'moreutils tools installed, default task append |ts output'
echo '系统已安装moreutils工具包默认定时任务增加ts 输出'
##复制一个新文件来追加|ts防止git pull的时候冲突
cp $defaultListFile /scripts/docker/default_list.sh
defaultListFile="/scripts/docker/default_list.sh"
sed -i 's/>>/|ts >>/g' $defaultListFile
fi
#判断 自定义文件是否存在 是否存在
if [ $CUSTOM_LIST_FILE ]; then
echo "You have configured a custom list file: $CUSTOM_LIST_FILE, custom list merge type: $CUSTOM_LIST_MERGE_TYPE..."
echo "您配置了自定义任务文件:$CUSTOM_LIST_FILE,自定义任务类型为:$CUSTOM_LIST_MERGE_TYPE..."
if [ -f "$customListFile" ]; then
if [ $CUSTOM_LIST_MERGE_TYPE == "append" ]; then
echo "merge default list file: $DEFAULT_LIST_FILE and custom list file: $CUSTOM_LIST_FILE"
echo "合并默认定时任务文件:$DEFAULT_LIST_FILE 和 自定义定时任务文件:$CUSTOM_LIST_FILE"
cat $defaultListFile >$mergedListFile
echo -e "" >>$mergedListFile
cat $customListFile >>$mergedListFile
elif [ $CUSTOM_LIST_MERGE_TYPE == "overwrite" ]; then
cat $customListFile >$mergedListFile
echo "merge custom list file: $CUSTOM_LIST_FILE..."
echo "合并自定义任务文件:$CUSTOM_LIST_FILE"
touch "$customListFile"
else
echo "配置配置了错误的自定义定时任务类型:$CUSTOM_LIST_MERGE_TYPE自定义任务类型为只能为append或者overwrite..."
cat $defaultListFile >$mergedListFile
fi
else
echo "Not found custom list file: $CUSTOM_LIST_FILE ,use default list file: $DEFAULT_LIST_FILE"
echo "自定义任务文件:$CUSTOM_LIST_FILE 未找到,使用默认配置$DEFAULT_LIST_FILE..."
cat $defaultListFile >$mergedListFile
fi
else
echo "The currently used is the default crontab task file: $DEFAULT_LIST_FILE ..."
echo "当前使用的为默认定时任务文件 $DEFAULT_LIST_FILE ..."
cat $defaultListFile >$mergedListFile
fi
# 判断最后要加载的定时任务是否包含默认定时任务,不包含的话就加进去
if [ $(grep -c "default_task.sh" $mergedListFile) -eq '0' ]; then
echo "Merged crontab task filethe required default task is not included, append default task..."
echo "合并后的定时任务文件,未包含必须的默认定时任务,增加默认定时任务..."
echo -e >>$mergedListFile
echo "52 */1 * * * sh /scripts/docker/default_task.sh |ts >> /scripts/logs/default_task.log 2>&1" >>$mergedListFile
fi
if [ $RANDOM_DELAY_MAX -ge 1 ]; then
echo "已设置随机延迟为 $RANDOM_DELAY_MAX , 设置延迟任务中... "
sed -i "/\(jd_bean_sign.js\|jd_blueCoin.js\|jd_joy_reward.js\|jd_joy_steal.js\|jd_joy_feedPets.js\)/!s/node/sleep \$((RANDOM % \$RANDOM_DELAY_MAX)); node/g" $mergedListFile
fi
echo "Load the latest crontab task file..."
echo "加载最新的定时任务文件..."
crontab $mergedListFile
echo "------------------------------------------------执行定时任务任务shell脚本------------------------------------------------"
sh -x /scripts/docker/default_task.sh
echo "--------------------------------------------------默认定时任务执行完成---------------------------------------------------"
if [ $run_cmd == 'crond' ]; then
echo "Start crontab task main process..."
echo "启动crondtab定时任务主进程..."
crond -f
else
echo "默认定时任务执行结束。"
fi