Take this. It will build you Docker image and push it into you Docker Hub image repository. Do not forget to create REGISTRY_USER
and REGISTRY_PASS
to login to DockerHub under project/settings/"CI/CD"/Variables
.
stages:
- test
- build
image: mcr.microsoft.com/dotnet/sdk:7.0
variables:
OBJECTS_DIRECTORY: obj
NUGET_PACKAGES_DIRECTORY: .nuget
SOURCE_CODE_PATH: src/DemoApp/
IMAGE_NAME: "your-account/demo-app"
IMAGE_TAG: "1.0"
cache:
# Per-stage and per-branch caching.
key: "$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG"
paths:
- $SOURCE_CODE_PATH$OBJECTS_DIRECTORY/project.assets.json
- $SOURCE_CODE_PATH$OBJECTS_DIRECTORY/*.csproj.nuget.*
- $NUGET_PACKAGES_DIRECTORY
policy: pull-push
before_script:
- dotnet restore src/DemoApp/DemoApp.csproj --packages $NUGET_PACKAGES_DIRECTORY
build_image:
stage: build
tags:
- private
image: docker:24.0.0-rc.4-cli
services:
- docker:24.0.0-rc.4-dind
variables:
DOCKER_TLS_CERTDIR: "/certs"
before_script:
- docker login -u $REGISTRY_USER -p $REGISTRY_PASS
script:
- docker build -t $IMAGE_NAME:$IMAGE_TAG .
- docker push $IMAGE_NAME:$IMAGE_TAG
tests:
stage: test
tags:
- private
script:
- dotnet test src/DemoApp/DemoApp.csproj --no-restore