Deploy Nop Artifacts #317
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy Nop Artifacts | |
on: | |
# push: | |
# branches: ['master'] | |
# 启用手工触发 | |
# https://dev.to/this-is-learning/manually-trigger-a-github-action-with-workflowdispatch-3mga | |
workflow_dispatch: | |
schedule: | |
# Note:此为 UTC 时间,最终的执行时间需加上时区 | |
- cron: '0 18 * * *' | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
# https://github.com/actions/checkout | |
- name: Checkout Nop source code | |
uses: actions/checkout@v4 | |
with: | |
repository: entropy-cloud/nop-entropy | |
path: code | |
- name: Checkout Nop extensions source code | |
uses: actions/checkout@v4 | |
with: | |
repository: entropy-cloud/nop-extensions | |
path: nop-extensions | |
# 克隆当前仓库到 dist 目录,以便于收集发布产物并提交至 dist 分支 | |
- name: Checkout Nop artifacts master repository | |
uses: actions/checkout@v4 | |
with: | |
repository: '${{ github.repository }}' | |
path: dist | |
# 用于临时存放构建产物 | |
- name: Checkout Nop artifacts dist repository | |
uses: actions/checkout@v4 | |
with: | |
repository: '${{ github.repository }}' | |
ref: dist | |
path: dist-temp | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
cache: maven | |
- name: Prepare environments | |
run: | | |
#################################################### | |
# 记录构建信息 | |
#################################################### | |
NOP_DIST_DIR="${GITHUB_WORKSPACE}/dist" | |
NOP_DIST_TEMP_DIR="${GITHUB_WORKSPACE}/dist-temp" | |
NOP_BUILD_LOG_FILE="${NOP_DIST_DIR}/USAGE.md" | |
NOP_BUILD_AT="$(TZ='Asia/Shanghai' date '+%Y-%m-%d %H:%M:%S %:z')" | |
NOP_BUILD_TIMESTAMP="$(date +%s)" | |
echo "NOP_DIST_DIR=${NOP_DIST_DIR}" >> ${GITHUB_ENV} | |
echo "NOP_DIST_TEMP_DIR=${NOP_DIST_TEMP_DIR}" >> ${GITHUB_ENV} | |
echo "NOP_BUILD_LOG_FILE=${NOP_BUILD_LOG_FILE}" >> ${GITHUB_ENV} | |
echo "NOP_BUILD_AT=${NOP_BUILD_AT}" >> ${GITHUB_ENV} | |
echo "NOP_BUILD_TIMESTAMP=${NOP_BUILD_TIMESTAMP}" >> ${GITHUB_ENV} | |
#################################################### | |
# 记录版本信息 | |
#################################################### | |
NOP_VERSION="$(cd ${GITHUB_WORKSPACE}/code && mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" | |
NOP_LATEST_COMMIT="$(cd ${GITHUB_WORKSPACE}/code && git log --format="%H" -n 1)" | |
echo "NOP_VERSION=${NOP_VERSION}" >> ${GITHUB_ENV} | |
echo "NOP_LATEST_COMMIT=${NOP_LATEST_COMMIT}" >> ${GITHUB_ENV} | |
NOP_EXTENSIONS_VERSION="$(cd ${GITHUB_WORKSPACE}/nop-extensions && mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" | |
NOP_EXTENSIONS_LATEST_COMMIT="$(cd ${GITHUB_WORKSPACE}/nop-extensions && git log --format="%H" -n 1)" | |
echo "NOP_EXTENSIONS_VERSION=${NOP_EXTENSIONS_VERSION}" >> ${GITHUB_ENV} | |
echo "NOP_EXTENSIONS_LATEST_COMMIT=${NOP_EXTENSIONS_LATEST_COMMIT}" >> ${GITHUB_ENV} | |
#################################################### | |
# 检查 dist 分支中记录 Nop 变更号的文件 | |
#################################################### | |
NOP_BUILD_LOG_URL="https://raw.githubusercontent.com/${{github.repository}}/dist/USAGE.md" | |
NOP_BUILD_LOG_CONTENT="$(curl ${NOP_BUILD_LOG_URL})" | |
NOP_IGNORE_BUILDING=false | |
NOP_EXTENSIONS_IGNORE_BUILDING=false | |
if [ -n "$(echo "${NOP_BUILD_LOG_CONTENT}" | grep ${NOP_LATEST_COMMIT})" ]; then | |
NOP_IGNORE_BUILDING=true | |
fi | |
if [ -n "$(echo "${NOP_BUILD_LOG_CONTENT}" | grep ${NOP_EXTENSIONS_LATEST_COMMIT})" ]; then | |
NOP_EXTENSIONS_IGNORE_BUILDING=true | |
fi | |
# 若 Nop 源码需更新,则其子项目也需一并更新 | |
if [[ "${NOP_IGNORE_BUILDING}" != "true" ]]; then | |
NOP_EXTENSIONS_IGNORE_BUILDING=false | |
fi | |
NOP_ARTIFACTS_NEED_UPDATE=false | |
if [[ "${NOP_IGNORE_BUILDING}" != "true" || "${NOP_EXTENSIONS_IGNORE_BUILDING}" != "true" ]]; then | |
NOP_ARTIFACTS_NEED_UPDATE=true | |
fi | |
# 若是没有最新的提交,则无需构建 | |
echo "NOP_ARTIFACTS_NEED_UPDATE=${NOP_ARTIFACTS_NEED_UPDATE}" >> ${GITHUB_ENV} | |
echo "NOP_IGNORE_BUILDING=${NOP_IGNORE_BUILDING}" >> ${GITHUB_ENV} | |
echo "NOP_EXTENSIONS_IGNORE_BUILDING=${NOP_EXTENSIONS_IGNORE_BUILDING}" >> ${GITHUB_ENV} | |
if [[ "${NOP_IGNORE_BUILDING}" = "true" ]]; then | |
echo "Ignore the Nop building for commit ${NOP_LATEST_COMMIT}" | |
else | |
echo "Continue the Nop building for commit ${NOP_LATEST_COMMIT}" | |
fi | |
if [[ "${NOP_EXTENSIONS_IGNORE_BUILDING}" = "true" ]]; then | |
echo "Ignore the Nop extensions building for commit ${NOP_EXTENSIONS_LATEST_COMMIT}" | |
else | |
echo "Continue the Nop extensions building for commit ${NOP_EXTENSIONS_LATEST_COMMIT}" | |
fi | |
#################################################### | |
# 收集不需要部署发布包的模块:以 mvn --projects '!webapp' 形式排除指定模块 | |
#################################################### | |
# 获取全部待构建模块 | |
NOP_MODULES=";$( \ | |
cd ${GITHUB_WORKSPACE}/code \ | |
&& mvn -B validate \ | |
| grep '/pom.xml' | awk '{print $NF}' | sed 's|/pom.xml$||g;' \ | |
| sort | uniq \ | |
| tr '\n' ';' \ | |
)" | |
# 查找部署被忽略的模块 | |
# https://blog.jdriven.com/2022/11/mastering-maven-exclude-modules-from-build/ | |
NOP_DEPLOY_SKIPPED_MODULES=( | |
$( \ | |
( cd ${GITHUB_WORKSPACE}/code \ | |
&& grep -rl --include=pom.xml \ | |
'<maven.deploy.skip>true</maven.deploy.skip>' . \ | |
| sed 's|^\./||g; s|/pom.xml$||g;' \ | |
\ | |
; find . -type d \( -name '*-demo' -o -name '*-demo2' \) \ | |
| sed '/\/src\//d; /\/target\//d; s|^\./||g;' \ | |
\ | |
; echo -e "nop-demo/nop-spring-demo-no-orm" | |
) \ | |
| sed '/nop-report\/nop-report-demo/d;' \ | |
| sort | uniq \ | |
) | |
) | |
# 从忽略列表中排除掉未被引入的模块 | |
NOP_IGNORED_MODULES=( ) | |
for m in "${NOP_DEPLOY_SKIPPED_MODULES[@]}"; do | |
if [[ "$(echo $NOP_MODULES | grep ";$m;")" != "" ]]; then | |
NOP_IGNORED_MODULES+=( $m ) | |
fi | |
done | |
echo "NOP_MODULES=${NOP_MODULES[@]}" | |
echo "NOP_DEPLOY_SKIPPED_MODULES=${NOP_DEPLOY_SKIPPED_MODULES[@]}" | |
echo "NOP_IGNORED_MODULES=${NOP_IGNORED_MODULES[@]}" | |
echo "NOP_IGNORED_MODULES=${NOP_IGNORED_MODULES[@]}" >> ${GITHUB_ENV} | |
- name: Build Nop packages | |
if: ${{ env.NOP_IGNORE_BUILDING != 'true' }} | |
working-directory: code | |
run: | | |
rm -rf ${{env.NOP_DIST_TEMP_DIR}}/io/github/entropy-cloud || echo "Ignore" | |
# 部署构建产物到本地指定目录 | |
# Note: *-codegen 项目不参与部署,但会参与构建 | |
# https://dzone.com/articles/using-github-as-maven-repository | |
# https://stackoverflow.com/questions/12435283/adding-a-maven-distribution-repository-on-the-command-line#answer-39439854 | |
mvn --batch-mode \ | |
-DaltDeploymentRepository=internal::default::file://${{env.NOP_DIST_TEMP_DIR}} \ | |
-DskipTests \ | |
-Dquarkus.package.type=uber-jar \ | |
--projects="$( \ | |
echo ${{env.NOP_IGNORED_MODULES}} \ | |
| sed 's/ /\n/g' \ | |
| sed '/-codegen$/d; s|^|!|g' \ | |
| tr '\n' ',' \ | |
)" \ | |
--file pom.xml \ | |
clean deploy | |
- name: Build Nop IDEA plugin | |
if: ${{ env.NOP_IGNORE_BUILDING != 'true' }} | |
working-directory: code/nop-idea-plugin | |
run: | | |
rm -f ${{env.NOP_DIST_TEMP_DIR}}/nop-idea-plugin-*.zip | |
bash ./gradlew buildPlugin | |
NOP_IDEA_PLUGIN_NAME="$(basename `ls build/distributions/nop-idea-plugin-*.zip`)" | |
cp build/distributions/${NOP_IDEA_PLUGIN_NAME} ${{env.NOP_DIST_TEMP_DIR}}/ | |
- name: Build Nop extensions | |
if: ${{ env.NOP_EXTENSIONS_IGNORE_BUILDING != 'true' }} | |
working-directory: nop-extensions | |
run: | | |
rm -rf ${{env.NOP_DIST_TEMP_DIR}}/io/github/entropy-cloud/extensions || echo "Ignore" | |
IGNORED_MODULES="$( \ | |
( grep -rl --include=pom.xml \ | |
'<maven.deploy.skip>true</maven.deploy.skip>' . \ | |
| sed 's|^\./||g; s|/pom.xml$||g;' \ | |
) \ | |
| sort | uniq \ | |
| tr '\n' ' ' \ | |
)" | |
mvn --batch-mode \ | |
-DaltDeploymentRepository=internal::default::file://${{env.NOP_DIST_TEMP_DIR}} \ | |
-DskipTests \ | |
--projects="$( \ | |
echo ${IGNORED_MODULES} \ | |
| sed 's/ /\n/g' \ | |
| sed 's|^|!|g' \ | |
| tr '\n' ',' \ | |
)" \ | |
--file pom.xml \ | |
clean deploy | |
- name: Update Nop building information | |
if: ${{ env.NOP_ARTIFACTS_NEED_UPDATE == 'true' }} | |
working-directory: dist | |
run: | | |
NOP_BUILD_START="${{env.NOP_BUILD_TIMESTAMP}}" | |
NOP_BUILD_END="$(date +%s)" | |
NOP_BUILD_PERIOD="$(echo "scale=2; ($NOP_BUILD_END - $NOP_BUILD_START) / 60" | bc)" | |
sed "s|%build_at%|${{env.NOP_BUILD_AT}}|g" -i ${{env.NOP_BUILD_LOG_FILE}} | |
sed "s|%build_period%|${NOP_BUILD_PERIOD}|g" -i ${{env.NOP_BUILD_LOG_FILE}} | |
sed "s|%nop_version%|${{env.NOP_VERSION}}|g" -i ${{env.NOP_BUILD_LOG_FILE}} | |
sed "s|%nop_latest_commit%|${{env.NOP_LATEST_COMMIT}}|g" -i ${{env.NOP_BUILD_LOG_FILE}} | |
sed "s|%nop_extensions_version%|${{env.NOP_EXTENSIONS_VERSION}}|g" -i ${{env.NOP_BUILD_LOG_FILE}} | |
sed "s|%nop_extensions_latest_commit%|${{env.NOP_EXTENSIONS_LATEST_COMMIT}}|g" -i ${{env.NOP_BUILD_LOG_FILE}} | |
NOP_IDEA_PLUGIN_NAME="$(basename `ls "${{env.NOP_DIST_TEMP_DIR}}"/nop-idea-plugin-*.zip`)" | |
sed "s|%idea_plugin_name%|${NOP_IDEA_PLUGIN_NAME}|g" -i ${{env.NOP_BUILD_LOG_FILE}} | |
NOP_CLI_JAR_PATH="$(cd ${{env.NOP_DIST_TEMP_DIR}} && find io/github/entropy-cloud/nop-cli -name 'nop-cli-*.jar')" | |
NOP_CLI_JAR_NAME="$(basename ${NOP_CLI_JAR_PATH})" | |
sed "s|%cli_jar_path%|${NOP_CLI_JAR_PATH}|g" -i ${{env.NOP_BUILD_LOG_FILE}} | |
sed "s|%cli_jar_name%|${NOP_CLI_JAR_NAME}|g" -i ${{env.NOP_BUILD_LOG_FILE}} | |
# 动态注入未部署的模块 | |
IGNORED_MODULES="$(echo ${{env.NOP_IGNORED_MODULES}} | sed 's/ /::::/g')" | |
sed "s|%nop_ignored_modules%|${IGNORED_MODULES}|g" -i ${{env.NOP_BUILD_LOG_FILE}} | |
sed 's/::::/\n/g' -i ${{env.NOP_BUILD_LOG_FILE}} | |
echo ${{env.NOP_IGNORED_MODULES}} \ | |
| sed 's/ /\n/g' \ | |
| sed 's|^|io/github/entropy-cloud/|g; s|$|/|g' \ | |
>> ${{env.NOP_DIST_DIR}}/.gitignore | |
- name: Commit Nop artifacts | |
if: ${{ env.NOP_ARTIFACTS_NEED_UPDATE == 'true' }} | |
working-directory: dist | |
# Note:推送至当前仓库,需在仓库配置中启用 Action 的写权限 | |
run: | | |
git config user.name "$(git log --format='%an' HEAD^!)" | |
git config user.email "$(git log --format='%ae' HEAD^!)" | |
mv "${{env.NOP_DIST_TEMP_DIR}}"/{io,nop-*} . | |
git checkout -B dist | |
git add . | |
git commit -m "Update artifacts" | |
git push origin dist -f |