name: Reusable Deploy on: workflow_call: inputs: env: required: true type: string # dev | prod secrets: MANIFESTS_TOKEN: required: true jobs: deploy: runs-on: docker steps: - name: Checkout app repo uses: actions/checkout@v4 - name: Checkout ci-workflows uses: actions/checkout@v4 with: repository: sys/ci-workflows ref: master path: .ci-workflows token: ${{ secrets.MANIFESTS_TOKEN }} - name: Check authorization and read config id: config env: REPO: ${{ github.repository }} ENV: ${{ inputs.env }} SHA: ${{ github.sha }} run: | python3 << 'PYEOF' import yaml, os, sys repo = os.environ['REPO'] env = os.environ['ENV'] sha = os.environ['SHA'][:7] with open('.ci-workflows/allowed-repos.yaml') as f: allowed = yaml.safe_load(f) or {} namespace = next( (ns for ns, repos in allowed.items() if repo in (repos or [])), None ) if not namespace: print(f'::error::Repository {repo} is not in allowed-repos.yaml', flush=True) sys.exit(1) with open('deploy.yaml') as f: cfg = yaml.safe_load(f) app_name = cfg['app']['name'] port = str(cfg['app']['port']) ingress_host = cfg['ingress'][env] replicas = str(cfg['replicas'][env]) cpu_req = cfg['resources']['requests']['cpu'] mem_req = cfg['resources']['requests']['memory'] cpu_lim = cfg['resources']['limits']['cpu'] mem_lim = cfg['resources']['limits']['memory'] sha_prefix = 'dev-sha-' if env == 'dev' else 'sha-' image = f"git.vodyanoysh.ru/{repo}:{sha_prefix}{sha}" resource_name = f"{env}-{app_name}" with open(os.environ['GITHUB_OUTPUT'], 'a') as f: for k, v in { 'namespace': namespace, 'app_name': app_name, 'resource_name': resource_name, 'port': port, 'ingress_host': ingress_host, 'replicas': replicas, 'cpu_request': cpu_req, 'memory_request': mem_req, 'cpu_limit': cpu_lim, 'memory_limit': mem_lim, 'image': image, }.items(): f.write(f'{k}={v}\n') PYEOF - name: Render manifests env: APP_NAME: ${{ steps.config.outputs.app_name }} RESOURCE_NAME: ${{ steps.config.outputs.resource_name }} PORT: ${{ steps.config.outputs.port }} INGRESS_HOST: ${{ steps.config.outputs.ingress_host }} REPLICAS: ${{ steps.config.outputs.replicas }} CPU_REQUEST: ${{ steps.config.outputs.cpu_request }} MEMORY_REQUEST: ${{ steps.config.outputs.memory_request }} CPU_LIMIT: ${{ steps.config.outputs.cpu_limit }} MEMORY_LIMIT: ${{ steps.config.outputs.memory_limit }} IMAGE: ${{ steps.config.outputs.image }} run: | VARS='${APP_NAME} ${RESOURCE_NAME} ${PORT} ${INGRESS_HOST} ${REPLICAS} ${CPU_REQUEST} ${MEMORY_REQUEST} ${CPU_LIMIT} ${MEMORY_LIMIT} ${IMAGE}' mkdir -p /tmp/rendered > /tmp/rendered/all.yaml for tpl in \ .ci-workflows/templates/deployment.yaml \ .ci-workflows/templates/service.yaml \ .ci-workflows/templates/ingress.yaml; do envsubst "$VARS" < "$tpl" >> /tmp/rendered/all.yaml printf '\n---\n' >> /tmp/rendered/all.yaml done - name: Commit to manifests repo env: TOKEN: ${{ secrets.MANIFESTS_TOKEN }} NAMESPACE: ${{ steps.config.outputs.namespace }} RESOURCE_NAME: ${{ steps.config.outputs.resource_name }} run: | git clone https://x-token:${TOKEN}@git.vodyanoysh.ru/vodyanoy/manifests.git /tmp/manifests python3 << 'PYEOF' import yaml, os, shutil namespace = os.environ['NAMESPACE'] resource_name = os.environ['RESOURCE_NAME'] base = f"/tmp/manifests/cluster/namespaces/{namespace}" dest = f"{base}/{resource_name}" os.makedirs(dest, exist_ok=True) shutil.copy('/tmp/rendered/all.yaml', f'{dest}/all.yaml') with open(f'{dest}/kustomization.yaml', 'w') as f: yaml.dump( {'apiVersion': 'kustomize.config.k8s.io/v1beta1', 'kind': 'Kustomization', 'resources': ['all.yaml']}, f, default_flow_style=False, sort_keys=False ) ns_kust_path = f'{base}/kustomization.yaml' with open(ns_kust_path) as f: ns_kust = yaml.safe_load(f) entry = f'{resource_name}/' resources = ns_kust.get('resources') or [] if entry not in resources: resources.append(entry) ns_kust['resources'] = resources with open(ns_kust_path, 'w') as f: yaml.dump(ns_kust, f, default_flow_style=False, sort_keys=False) PYEOF cd /tmp/manifests git config user.email "ci@vodyanoysh.ru" git config user.name "Forgejo CI" git add . git diff --cached --quiet || git commit -m "deploy ${RESOURCE_NAME} → ${NAMESPACE}" git push