diff --git a/.github/workflows/dev-build-cm-reusable-workflow.yaml b/.github/workflows/dev-build-cm-reusable-workflow.yaml new file mode 100644 index 00000000..2c1e1ac2 --- /dev/null +++ b/.github/workflows/dev-build-cm-reusable-workflow.yaml @@ -0,0 +1,153 @@ +name: Dev Build and Deploy Config Maps (Reusable) + +on: + workflow_call: + inputs: + app-name: + description: 'Human-readable name for commit messages' + required: true + type: string + configmap_file: + description: 'Repo-relative path to the ConfigMap manifest' + type: string + required: true + configmap_name: + description: 'Name of the Config map' + type: string + required: true + data_key: + description: 'The key under which the file content appears in ConfigMap .data.' + type: string + required: true + namespace: + description: 'Kubernetes namespace. Defaults to the apps if omitted.' + type: string + required: true + gitops-repo: + required: false + type: string + default: 'isisbusapps/gitops' + runner_label: + description: 'Self-hosted runner label to target e.g. self-hosted or k8s-prod-runner)' + type: string + required: false + default: 'self-hosted' + secrets: + workflow-token: + description: 'PAT with repo+workflow scope for the GitOps repo' + required: true + + outputs: + configmap_output_name: + description: 'Name of the ConfigMap that was created/updated' + value: ${{ jobs.generate-configmap.outputs.configmap_name }} + data_key_used: + description: 'The .data key the file was stored under' + value: ${{ jobs.generate-configmap.outputs.data_key_used }} + +jobs: + generate-configmap:: + name: Generate & Commit ConfigMap + runs-on: ${{ inputs.runner_label }} + + timeout-minutes: 30 + + permissions: + packages: write + + configmap_name: ${{ steps.resolve.outputs.configmap_name }} + + steps: + - name: Checkout source repository + uses: actions/checkout@v6 + + - name: Validate source file + run: | + FILE="${{ inputs.configmap_file }}" + + if [[ ! -f "${FILE}" ]]; then + echo "::error file=${FILE}::Source file not found: ${FILE}" + exit 1 + fi + + # Basic sanity — reject empty files + if [[ ! -s "${FILE}" ]]; then + echo "::error file=${FILE}::Source file is empty: ${FILE}" + exit 1 + fi + echo "Source file found: ${FILE} ($(wc -l < "${FILE}") lines)" + + - name: Resolve parameters + id: resolve + run: | + # Namespace + if [[ -n "${{ inputs.namespace }}" ]]; then + NS="${{ inputs.namespace }}" + else + echo "::error::The namespace input cannot be empty!" + exit 1 + fi + + # Data key + if [[ -n "${{ inputs.data_key }}" ]]; then + KEY="${{ inputs.data_key }}" + else + echo "::error::The data key input cannot be empty!" + exit 1 + fi + + # Config Map + if [[ -n "${{ inputs.configmap_name }}" ]]; then + CM="${{ inputs.configmap_name }}" + else + echo "::error::The configmap name cannot be empty!" + exit 1 + fi + + echo "namespace=${NS}" >> "$GITHUB_OUTPUT" + echo "data_key=${KEY}" >> "$GITHUB_OUTPUT" + echo "configmap_name=${CM}" >> "$GITHUB_OUTPUT" + + echo "ConfigMap name : ${CM}" + echo "Namespace : ${NS}" + echo "Data key : ${KEY}" + + - name: Generate ConfigMap manifest + id: generate + run: | + NS="${{ steps.resolve.outputs.namespace }}" + CM="${{ steps.resolve.outputs.configmap_name }}" + KEY="${{ steps.resolve.outputs.data_key }}" + FILE="${{ inputs.yaml_file }}" + + echo "Generating ConfigMap manifest..." + + kubectl create configmap "${CM}" \ + --from-file="${KEY}=${FILE}" \ + --namespace "${NS}" \ + --dry-run=client \ + -o yaml > /tmp/configmap-generated.yaml + + echo "============= Generated manifest===================" + cat /tmp/configmap-generated.yaml + echo "===================================================" + + update_gitops: + name: Update GitOps Repository + runs-on: self-hosted + timeout-minutes: 10 + needs: build_and_push + if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' + steps: + - name: Checkout GitOps repository + uses: actions/checkout@v6 + with: + repository: ${{ inputs.gitops-repo }} + token: ${{ secrets.workflow-token }} + + - name: Commit and push changes + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git commit -am "[CD] Update ${{ inputs.app-name }} with this commit ${{ github.event.head_commit.url }}" + git push \ No newline at end of file