conocimientocicd-architecttemplates/4901 B
_reusable-dotnet-build.yml.template
Fichero que /hv:cicd-architect usa o materializa en el proyecto. Se muestra tal cual está en el plugin.
# =============================================================================
# {{PROYECTO_NOMBRE}} — Reusable workflow: build + test + coverage + security-scan (+ publish)
# Ovillo cicd-architect github-reusable @ {{TEMPLATE_STAMP}} (sello de plantilla; /cicd-status detecta drift)
# Reglas: cicd-runtime G1-G19 (G15 permisos minimos, G16 pin por SHA, G8 verdict-file, G10 retention)
# Materializado por /cicd-init en .github/workflows/_reusable-dotnet-build.yml (una vez por repo).
# Lo consumen cd-<slug>.yml (Fase 2). Se ejecuta con el GITHUB_TOKEN de solo lectura del caller.
#
# Scripts que ejecuta y que /cicd-init materializa en el repo (CI no puede leer el plugin):
# ops/cicd/scripts/security-scan.ps1 (G8, via .github/actions/security-scan)
# =============================================================================
name: Reusable .NET build
on:
workflow_call:
inputs:
solution-path:
description: 'Objetivo de compilacion: ruta a la .sln, .slnx o al .csproj unico (el SDK acepta los tres)'
required: true
type: string
dotnet-version:
required: false
type: string
default: '{{NET_VERSION}}'
test-filter:
description: 'Filtro de dotnet test (vacio = todos)'
required: false
type: string
default: ''
runsettings:
description: 'runsettings de cobertura (vacio = sin --settings)'
required: false
type: string
default: 'ops/testing/coverlet.runsettings'
api-project:
description: 'csproj del entrypoint a publicar (vacio = no publica)'
required: false
type: string
default: ''
artifact-name:
required: false
type: string
default: 'publish'
runner:
required: false
type: string
default: 'ubuntu-latest'
permissions:
contents: read
jobs:
build:
name: Build
runs-on: ${{ inputs.runner }}
timeout-minutes: 20
env:
# G17: las entradas van por env y se leen como "$VAR" (nunca ${{ inputs.* }} dentro de run:)
SOLUTION_PATH: ${{ inputs.solution-path }}
TEST_FILTER: ${{ inputs.test-filter }}
RUNSETTINGS: ${{ inputs.runsettings }}
API_PROJECT: ${{ inputs.api-project }}
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Setup .NET
uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
dotnet-version: ${{ inputs.dotnet-version }}
- name: Cache NuGet packages
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ~/.nuget/packages
key: nuget-${{ runner.os }}-${{ hashFiles('**/*.csproj', '**/Directory.Packages.props') }}
restore-keys: nuget-${{ runner.os }}-
- name: Restore
run: dotnet restore "${{ env.SOLUTION_PATH }}"
- name: Build
run: dotnet build "${{ env.SOLUTION_PATH }}" --no-restore --configuration Release
- name: Test
# Rama condicional (a): sin proyecto de test, /cicd-init elimina este step y 'Upload coverage'.
shell: bash
run: |
args=(--no-build --configuration Release --results-directory TestResults)
[ -n "${TEST_FILTER}" ] && args+=(--filter "${TEST_FILTER}")
[ -n "${RUNSETTINGS}" ] && [ -f "${RUNSETTINGS}" ] && args+=(--settings "${RUNSETTINGS}")
dotnet test "${SOLUTION_PATH}" "${args[@]}"
- name: Upload coverage
if: always()
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: coverage-${{ github.run_number }}
path: |
TestResults/**/coverage.cobertura.xml
TestResults/**/coverage.opencover.xml
if-no-files-found: ignore
retention-days: 14
- name: Coverage gate (G7, warn)
if: always()
uses: ./.github/actions/coverage-gate
with:
path: TestResults
threshold-line: '70'
threshold-branch: '60'
mode: warn
- name: Security scan (G8, verdict-file; no tumba el build)
if: always()
uses: ./.github/actions/security-scan
with:
stack: dotnet
solution-path: ${{ inputs.solution-path }}
api-project: ${{ inputs.api-project }}
- name: Publish
if: inputs.api-project != ''
run: dotnet publish "${{ env.API_PROJECT }}" --configuration Release --no-build --output ./publish
- name: Upload publish artifact
if: inputs.api-project != ''
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: ${{ inputs.artifact-name }}-${{ github.sha }}
path: ./publish
retention-days: 30