From d93bbf48f6cfac556f8ebf07d4f19126bcde9ea8 Mon Sep 17 00:00:00 2001 From: Meng Sen Date: Thu, 28 Nov 2024 16:48:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=8F=91=20=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E5=BA=94=E7=94=A8=E5=95=86=E5=BA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Meng Sen --- .github/workflows/process-apps.yml | 47 ++++++++++++++++ process-apps.py | 88 ++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 .github/workflows/process-apps.yml create mode 100644 process-apps.py diff --git a/.github/workflows/process-apps.yml b/.github/workflows/process-apps.yml new file mode 100644 index 000000000..34769cf6c --- /dev/null +++ b/.github/workflows/process-apps.yml @@ -0,0 +1,47 @@ +name: Process Apps and Commit Changes + +on: + push: + branches: + - appstore + workflow_dispatch: + +jobs: + process: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pyyaml + + - name: Run processing script + run: python process-apps.py + + - name: Check for changes + id: check_changes + run: | + git diff --exit-code || echo "Changes detected." + + - name: Configure Git + if: steps.check_changes.outcome != 'success' + run: | + git config --global user.name "${{ github.actor }}" + git config --global user.email "${{ github.actor }}@users.noreply.github.com" + + - name: Commit and push changes + if: steps.check_changes.outcome != 'success' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + git add . + git commit -m "Processed apps directory via GitHub Actions" + git push origin appstore diff --git a/process-apps.py b/process-apps.py new file mode 100644 index 000000000..56ff57116 --- /dev/null +++ b/process-apps.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- +import os +import shutil + +import yaml + +if __name__ == '__main__': + + app_dir = r"apps" + if not os.path.exists(app_dir): + print(f'[err] {app_dir} 不存在') + exit(1) + + appstore_dir = r"appstore" + if os.path.exists(appstore_dir): + shutil.rmtree(appstore_dir) + shutil.copytree(app_dir, appstore_dir) + + dockge_dir = r"dockge" + if os.path.exists(dockge_dir): + shutil.rmtree(dockge_dir) + + app_docker_compose_file = [] + for root, dirs, files in os.walk(appstore_dir): + if 'docker-compose.yml' in files: + app_docker_compose_file.append(os.path.join(root, 'docker-compose.yml')) + + for docker_compose_file in app_docker_compose_file: + print(f'[info] Standardized processing: {docker_compose_file} ...') + parent_dir = os.path.dirname(docker_compose_file) + version = os.path.basename(os.path.dirname(docker_compose_file)) + app_name = os.path.basename(os.path.dirname(os.path.dirname(docker_compose_file))) + with open(docker_compose_file, 'r', encoding='utf-8') as f: + docker_compose = yaml.safe_load(f) + services = docker_compose.get('services') + for service_name, service in services.items(): + env_file = service.get('env_file') + if env_file: + if isinstance(env_file, list): + for i, file in enumerate(env_file): + if '${APP_ENV_FILE' in file: + env_file[i] = f'./envs/{app_name}.env' + elif '${ENV_FILE' in file: + env_file[i] = f'.env' + elif '${GLOBAL_ENV_FILE' in file: + env_file[i] = f'./envs/global.env' + else: + print('env_file', env_file) + docker_compose['services'][service_name]['env_file'] = env_file + container_name = service.get('container_name') + if container_name: + docker_compose['services'][service_name]['container_name'] = container_name.replace('${CONTAINER_NAME}', + app_name) + with open(docker_compose_file, 'w', encoding='utf-8') as f: + yaml.dump(docker_compose, f) + print(f'[info] Standardized processing: {app_name} {version} {docker_compose_file} Done!') + data_yml = os.path.join(parent_dir, 'data.yml') + with open(data_yml, 'r', encoding='utf-8') as f: + data = yaml.safe_load(f) + form_fields = data['additionalProperties']['formFields'] + for form_field in form_fields: + key_name = form_field.get('envKey') + key_value = form_field.get('default') + key_desc = form_field.get('labelZh') + required = form_field.get('required') + desc = f'# {key_desc}' + if required: + desc += ' [必填]' + env_file = os.path.join(parent_dir, '.env') + if not os.path.exists(env_file): + with open(env_file, 'w', encoding='utf-8') as f: + f.write(f'{desc} \n') + f.write(f'{key_name}={key_value}\n\n') + else: + with open(os.path.join(parent_dir, '.env'), 'a', encoding='utf-8') as f: + f.write(f'{desc} \n') + f.write(f'{key_name}={key_value}\n\n') + print(f'[info] Standardized processing: {app_name} {version} {data_yml} Done!') + + if os.path.exists(parent_dir): + dockge_app_dir = os.path.join(dockge_dir, app_name) + if os.path.exists(dockge_app_dir): + dockge_version = version.replace('.', '_') + dockge_app_dir = os.path.join(dockge_dir, f'{app_name}_{dockge_version}') + shutil.copytree(parent_dir, dockge_app_dir, ignore=shutil.ignore_patterns('data.yml', 'scripts')) + print(f'[info] Standardized processing: {app_name} {version} {parent_dir} Done!') + + print('Done!')