From 811a54e24dbc6f3082c342475e994bab8f83550e Mon Sep 17 00:00:00 2001 From: Mohammad Mahdi Mohammadi Date: Sun, 15 Dec 2024 17:36:22 +0330 Subject: [PATCH] Add Dockerfile and modify gitlab CI to build the docker --- .dockerignore | 25 ++++++++++++++++++++++++ .gitlab-ci.yml | 49 +++++++++++++++++++++++++++++++----------------- Dockerfile | 21 +++++++++++++++++++++ mstdnCats.csproj | 1 + 4 files changed, 79 insertions(+), 17 deletions(-) create mode 100755 .dockerignore create mode 100755 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100755 index 0000000..cd967fc --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9e5ab6e..416f424 100755 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,21 +1,36 @@ -stages: - - build +# To contribute improvements to CI/CD templates, please follow the Development guide at: +# https://docs.gitlab.com/ee/development/cicd/templates.html +# This specific template is located at: +# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Docker.gitlab-ci.yml -variables: - DOTNET_CLI_TELEMETRY_OPTOUT: "1" - DOTNET_SKIP_FIRST_TIME_EXPERIENCE: "1" +# Build a Docker image with CI/CD and push to the GitLab registry. +# Docker-in-Docker documentation: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html +# +# This template uses one generic job with conditional builds +# for the default branch and all other (MR) branches. -before_script: - - apt-get update && apt-get install -y tar - -build: +docker-build: + # Use the official docker image. + image: docker:cli stage: build - image: mcr.microsoft.com/dotnet/sdk:9.0-bookworm-slim + services: + - docker:dind + variables: + DOCKER_IMAGE_NAME: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG + before_script: + - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY + # All branches are tagged with $DOCKER_IMAGE_NAME (defaults to commit ref slug) + # Default branch is also tagged with `latest` script: - - dotnet nuget add source https://pkgs.dev.azure.com/tgbots/Telegram.Bot/_packaging/release/nuget/v3/index.json -n Telegram.Bot - - dotnet restore --no-cache - - dotnet publish -c Release -r linux-x64 --self-contained true /p:PublishSingleFile=true /p:PublishTrimmed=false /p:EnableCompressionInSingleFile=true - - tar -czvf publish.tar.gz -C bin/Release/net9.0/linux-x64/publish/ . - artifacts: - paths: - - publish.tar.gz + - docker build --pull -t "$DOCKER_IMAGE_NAME" . + - docker push "$DOCKER_IMAGE_NAME" + - | + if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then + docker tag "$DOCKER_IMAGE_NAME" "$CI_REGISTRY_IMAGE:latest" + docker push "$CI_REGISTRY_IMAGE:latest" + fi + # Run this job in a branch where a Dockerfile exists + rules: + - if: $CI_COMMIT_BRANCH + exists: + - Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..2a730a8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM mcr.microsoft.com/dotnet/aspnet:9.0-bookworm-slim-amd64 AS base +USER $APP_UID +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/sdk:9.0-bookworm-slim-amd64 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["mstdnCats.csproj", "./"] +RUN dotnet restore "mstdnCats.csproj" +COPY . . +WORKDIR "/src/" +RUN dotnet build "mstdnCats.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "mstdnCats.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "mstdnCats.dll"] diff --git a/mstdnCats.csproj b/mstdnCats.csproj index 2b0d7af..1c9ebea 100755 --- a/mstdnCats.csproj +++ b/mstdnCats.csproj @@ -6,6 +6,7 @@ enable enable true + Linux