diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 76b857233..1f217c047 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -56,3 +56,5 @@ jobs: DEFAULT_CRON: "0 30 0 * * * *" DEFAULT_REGION: "ap-guangzhou" DEFAULT_FUNCTION_NAME: "NeteaseCloudMusicTasks" + + SHA: ${{ github.sha }} diff --git a/.github/workflows/upload.yml b/.github/workflows/upload.yml index 7d7bc86be..dc169a9c0 100644 --- a/.github/workflows/upload.yml +++ b/.github/workflows/upload.yml @@ -28,6 +28,8 @@ jobs: uses: actions/checkout@v2 - name: Create config file run: mv config.example.json config.json + - name: Write sha to config file + run: sed -i "s/commitId/${{ github.sha }}/g" config.json - name: Setting environment variable run: | ver=$(cat config.json | grep version | grep -o "[\.0-9]*") diff --git a/config.example.json b/config.example.json index 53652c684..ec37b18bd 100644 --- a/config.example.json +++ b/config.example.json @@ -1,6 +1,7 @@ { /* 开源地址: https://github.com/chen310/NeteaseCloudMusicTasks */ "version": "2.20", + "sha": "commitId", /* 多账号的配置详见 https://github.com/chen310/NeteaseCloudMusicTasks#%E5%A4%9A%E8%B4%A6%E5%8F%B7 */ "users": [ { diff --git a/index.py b/index.py index 5c69f1038..369a46e78 100644 --- a/index.py +++ b/index.py @@ -54,6 +54,7 @@ def start(event={}, context={}): config = json5.load(f) print('Version:', config['version']) + print('Commit ID:', config['sha']) # 公共配置 setting = config['setting'] diff --git a/ql_update.py b/ql_update.py index 65814495a..1821f452c 100644 --- a/ql_update.py +++ b/ql_update.py @@ -1,5 +1,19 @@ import os + +def writeSha(path, file): + cur = os.getcwd() + os.chdir(path) + result = os.popen('git rev-parse HEAD').read().strip() + os.chdir(cur) + + if len(result) == 40 and ' ' not in result: + os.system('sed -i "s/commitId/{}/g" {}'.format(result, file)) + print('已写入 commit id') + else: + print('commit id 获取失败') + + if __name__ == "__main__": github_url = 'https://github.com/chen310/NeteaseCloudMusicTasks.git' @@ -21,12 +35,15 @@ os.system('cp -f {} {}'.format(scripts_config, old_config)) print('复制配置示例文件...') os.system('cp -f {} {}'.format(repo_config, example_config)) + writeSha(os.path.dirname(repo_config), example_config) print('更新配置文件...') os.system('python3 {}scripts/chen310_NeteaseCloudMusicTasks/updateconfig.py {} {} {}'.format( - data_path, repo_config, scripts_config, scripts_config)) + data_path, example_config, scripts_config, scripts_config)) print('更新完成') else: - print('复制配置文件') - os.system('cp {} {}'.format(repo_config, scripts_config)) print('复制配置示例文件...') os.system('cp -f {} {}'.format(repo_config, example_config)) + writeSha(os.path.dirname(repo_config), example_config) + print('复制配置文件...') + os.system('cp {} {}'.format(example_config, scripts_config)) + diff --git a/serverless/deploy.sh b/serverless/deploy.sh index 9a1ce4a78..4e33a523a 100644 --- a/serverless/deploy.sh +++ b/serverless/deploy.sh @@ -22,6 +22,7 @@ else config_file="config.json" old_config_file="config.old.json" example_config_file="config.example.json" + sed -i "s/commitId/$SHA/g" $example_config_file # 复制模板文件为配置文件 cp $example_config_file $config_file if [ -e "code/config.json" ]; then diff --git a/updateconfig.py b/updateconfig.py index 482030404..909cb3b5b 100644 --- a/updateconfig.py +++ b/updateconfig.py @@ -1,5 +1,6 @@ import json5 import sys +import os from utils import jsonDumps from utils import updateConfig @@ -61,6 +62,16 @@ def processSetting(setting, template): def before(src, dst): + if 'sha' in src: + del src['sha'] + + if 'sha' in dst and dst['sha'] == 'commitId': + result = os.popen('git rev-parse HEAD').read().strip() + if len(result) == 40 and ' ' not in result: + dst['sha'] = result + else: + del dst['sha'] + for user in src['users']: if 'md5' in user: del user['md5']