v1.31.0
conocimientogit-best-practicestemplates/5002 B

github-workflow.yml.template

Fichero que /hv:git-best-practices usa o materializa en el proyecto. Se muestra tal cual está en el plugin.

git-best-practicesskill
templatescarpeta
5002 Btamaño
2026-09-16último cambio del pack
# TEMPLATE: GitHub Actions CI/CD para .NET 10
# Skill: git-best-practices
# Uso: copiar a .github/workflows/ci.yml y sustituir {{SOLUTION_NAME}}.
#
# Decisiones que trae puestas:
#   - permissions minimos: el GITHUB_TOKEN solo lee; cada job eleva lo que necesita.
#   - acciones pinneadas por SHA con la version en comentario (Dependabot actualiza ambos;
#     templates/dependabot.yml.template). Un tag como @v4 puede moverse; un SHA no.
#   - merge_group: necesario para que la merge queue del ruleset ejecute estos checks.
#   - Los name: de los jobs (Build, Security) son los contextos de required_status_checks del ruleset.
#   - concurrency: un push nuevo cancela el run anterior de la misma rama.

name: CI

on:
  push:
    branches: [main]
    paths-ignore:
      - '**/*.md'
      - 'docs/**'
  pull_request:
    branches: [main]
  merge_group:
    branches: [main]

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  DOTNET_VERSION: '10.0.x'
  SOLUTION_PATH: 'src/{{SOLUTION_NAME}}.sln'

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    timeout-minutes: 15

    steps:
      - name: Checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Setup .NET
        uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
        with:
          dotnet-version: ${{ env.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
        # ops/testing/coverlet.runsettings lo escribe /hv:init; sin el, quitar --settings y usar --collect:"XPlat Code Coverage"
        run: dotnet test ${{ env.SOLUTION_PATH }} --no-build --configuration Release --settings ops/testing/coverlet.runsettings

      - name: Upload coverage
        if: github.event_name == 'pull_request'
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: coverage-report
          path: '**/coverage.cobertura.xml'
          retention-days: 14

  security:
    name: Security
    runs-on: ubuntu-latest
    timeout-minutes: 10
    needs: build

    steps:
      - name: Checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Setup .NET
        uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: NuGet audit
        # Falla el job si hay vulnerabilidades: el ruleset exige este check, asi que un paquete vulnerable no llega a main.
        run: |
          dotnet restore ${{ env.SOLUTION_PATH }}
          dotnet list ${{ env.SOLUTION_PATH }} package --vulnerable --include-transitive | tee audit.txt
          if grep -qE "Critical|High" audit.txt; then echo "::error::Vulnerabilidades Critical/High en dependencias"; exit 1; fi

  deploy-staging:
    name: Deploy staging
    runs-on: ubuntu-latest
    timeout-minutes: 15
    needs: [build, security]
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    # El environment concentra secretos, reviewers y reglas de proteccion: configurarlo en
    # Settings > Environments. Para produccion, duplicar este job con environment: production y required reviewers.
    environment:
      name: staging
      url: '{{URL_STAGING}}'
    permissions:
      contents: read
      id-token: write   # OIDC hacia la nube (azure/login, aws-actions/configure-aws-credentials) sin secretos de larga vida

    steps:
      - name: Checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Setup .NET
        uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: Publish
        run: dotnet publish ${{ env.SOLUTION_PATH }} --configuration Release --output ./publish

      - name: Upload publish artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: publish-${{ github.sha }}
          path: ./publish
          retention-days: 30

      - name: Deploy
        # Sustituir por el paso real de la organizacion: azure/webapps-deploy, aws-actions/*, rsync/ssh a IIS...
        # Se deja explicito para que el job falle si nadie lo configura.
        run: |
          echo "::error::Paso de despliegue sin configurar: sustituir este step (ver ecosystem.config.json -> cloud.provider)"
          exit 1

← Volver a la ficha de git-best-practices