Commit 1646b3fe authored by shRabbit's avatar shRabbit
Browse files

Fix build jobs: self-contained restore (drop --no-restore/--no-build), add NuGet cache

parent 6b17ff5a
Loading
Loading
Loading
Loading
Loading
+19 −22
Original line number Diff line number Diff line
# GitLab CI/CD component to restore, build, test and publish .NET Core / .NET projects.
# GitLab CI/CD component to build, test and publish .NET Core / .NET projects.
#
# Reference: https://docs.gitlab.com/ee/ci/components/
#
@@ -12,21 +12,22 @@
#         tests_enabled: true
#
# All jobs run in the official .NET SDK image `mcr.microsoft.com/dotnet/sdk`.
# Every job performs its own restore+build so it works from a clean checkout
# (the repository must NOT contain committed `obj/` / `bin/` folders).

spec:
  inputs:
    # .NET SDK version (major.minor) used as the image tag.
    dotnet_version:
      default: "8.0"
    # The MsBuild configuration to restore/build/publish with.
    # The MsBuild configuration to build/publish with.
    configuration:
      default: Release
    # Glob path to the .sln / .csproj to build. Leave empty to build the default
    # project found in the repository root.
    solution_path:
      default: ""
    # Set to "true" to enable the test job. The test command targets the restored
    # solution unless `test_project_path` is provided.
    # Set to "true" to enable the test job.
    tests_enabled:
      default: false
    # Set to "true" to enable the publish job.
@@ -48,21 +49,14 @@ variables:
  RUN_TESTS: "$[[ inputs.tests_enabled ]]"
  ENABLE_PUBLISH: "$[[ inputs.publish_enabled ]]"
  PUBLISH_OUTPUT: "$[[ inputs.publish_output ]]"
  # Keep the NuGet package cache inside the project dir so jobs share it.
  NUGET_PACKAGES: "$CI_PROJECT_DIR/.nuget/packages"

# Restore NuGet packages. Skipped automatically if `solution_path` is empty.
dotnet-restore:
  image: "mcr.microsoft.com/dotnet/sdk:$DOTNET_VERSION"
  stage: $[[ inputs.stage ]]
  variables:
    NUGET_RESTORE_ARGS: "$[[ inputs.solution_path ]]"
  script:
    - if [ -z "$NUGET_RESTORE_ARGS" ]; then echo "No solution/project specified, skipping restore." && exit 0; fi
    - dotnet restore "$NUGET_RESTORE_ARGS" -p:Configuration="$BUILD_CONFIGURATION"
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - when: always
# Share restored NuGet packages across the parallel jobs of a pipeline.
cache:
  key: "nuget-$DOTNET_VERSION-$CI_COMMIT_REF_SLUG"
  paths:
    - ".nuget/packages"

# Compile the solution/project and keep the build output as an artifact.
dotnet-build:
@@ -71,7 +65,8 @@ dotnet-build:
  variables:
    DOTNET_BUILD_ARGS: "$[[ inputs.solution_path ]]"
  script:
    - dotnet build "$DOTNET_BUILD_ARGS" -p:Configuration="$BUILD_CONFIGURATION" --no-restore
    - if [ -z "$DOTNET_BUILD_ARGS" ]; then echo "No solution/project specified, skipping build." && exit 0; fi
    - dotnet build "$DOTNET_BUILD_ARGS" -p:Configuration="$BUILD_CONFIGURATION"
  artifacts:
    paths:
      - "**/bin/$BUILD_CONFIGURATION/**"
@@ -91,7 +86,8 @@ dotnet-test:
  variables:
    DOTNET_TEST_ARGS: "$[[ inputs.solution_path ]]"
  script:
    - dotnet test "$DOTNET_TEST_ARGS" -p:Configuration="$BUILD_CONFIGURATION" --no-restore --no-build
    - if [ -z "$DOTNET_TEST_ARGS" ]; then echo "No solution/project specified, skipping tests." && exit 0; fi
    - dotnet test "$DOTNET_TEST_ARGS" -p:Configuration="$BUILD_CONFIGURATION"
  artifacts:
    when: always
    reports:
@@ -112,7 +108,8 @@ dotnet-publish:
  variables:
    DOTNET_PUBLISH_ARGS: "$[[ inputs.solution_path ]]"
  script:
    - dotnet publish "$DOTNET_PUBLISH_ARGS" -p:Configuration="$BUILD_CONFIGURATION" --no-restore --no-build --output "$PUBLISH_OUTPUT"
    - if [ -z "$DOTNET_PUBLISH_ARGS" ]; then echo "No solution/project specified, skipping publish." && exit 0; fi
    - dotnet publish "$DOTNET_PUBLISH_ARGS" -p:Configuration="$BUILD_CONFIGURATION" --output "$PUBLISH_OUTPUT"
  artifacts:
    paths:
      - "$PUBLISH_OUTPUT/**"