From 60f89fc78000137e3af1086764a3b5920224bd79 Mon Sep 17 00:00:00 2001 From: Vyacheslav Sheptihin Date: Wed, 23 Apr 2025 17:13:11 +0000 Subject: [PATCH] Add CI/CD --- .gitlab-ci.yml | 25 ++++++++ .../SurveyBackend.API.csproj | 20 ++++--- SurveyBackend/backend.gitlab-ci.yml | 59 +++++++++++++++++++ SurveyFrontend/frontend.gitlab-ci.yml | 37 ++++++++++++ 4 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 .gitlab-ci.yml create mode 100644 SurveyBackend/backend.gitlab-ci.yml create mode 100644 SurveyFrontend/frontend.gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..8851546 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,25 @@ +stages: + - .pre + - build + - test + - deploy + +variables: + GIT_SUBMODULE_STRATEGY: recursive + PUBLISH_DIR: BackendPublish + +top-level-job: + stage: .pre + image: alpine:latest + script: + - echo "Starting pipeline" + +include: + - local: '/SurveyBackend/backend.gitlab-ci.yml' + rules: + - changes: + - 'SurveyBackend/*' + - local: '/SurveyFrontend/frontend.gitlab-ci.yml' + rules: + - changes: + - 'SurveyFrontend/*' diff --git a/SurveyBackend/SurveyBackend.API/SurveyBackend.API.csproj b/SurveyBackend/SurveyBackend.API/SurveyBackend.API.csproj index b736412..fef66e7 100644 --- a/SurveyBackend/SurveyBackend.API/SurveyBackend.API.csproj +++ b/SurveyBackend/SurveyBackend.API/SurveyBackend.API.csproj @@ -8,20 +8,26 @@ - + + Never + + + + + - all - runtime; build; native; contentfiles; analyzers; buildtransitive + all + runtime; build; native; contentfiles; analyzers; buildtransitive - + - - - + + + diff --git a/SurveyBackend/backend.gitlab-ci.yml b/SurveyBackend/backend.gitlab-ci.yml new file mode 100644 index 0000000..be8a91b --- /dev/null +++ b/SurveyBackend/backend.gitlab-ci.yml @@ -0,0 +1,59 @@ +stages: + - build + - test + - deploy + +backend-build-job: + image: mcr.microsoft.com/dotnet/sdk:8.0 + stage: build + script: + - dotnet restore SurveyBackend/SurveyBackend.sln + - dotnet tool install --global dotnet-ef + - export PATH="$PATH:/root/.dotnet/tools" + - > + dotnet ef migrations bundle + --self-contained + -r linux-x64 + -p SurveyBackend/SurveyBackend.Infrastructure/SurveyBackend.Infrastructure.csproj + -s ./SurveyBackend/SurveyBackend.API/SurveyBackend.API.csproj + -o $PUBLISH_DIR/efbundle + - > + dotnet publish SurveyBackend/SurveyBackend.API/SurveyBackend.API.csproj + -c Release -r linux-x64 --self-contained true + /p:PublishReadyToRun=true + /p:PublishSingleFile=true + /p:IncludeNativeLibrariesForSelfExtract=false + /p:TrimUnusedDependencies=false + -o $PUBLISH_DIR + artifacts: + paths: + - $PUBLISH_DIR + expire_in: 1 hour + +backend-test-job: + image: alpine:latest + stage: test + script: + - echo "Here we would run some tests if I knew how to use them" + +backend-deploy-job: + image: alpine:latest + stage: deploy + rules: + - if: '$CI_COMMIT_BRANCH == "main"' + before_script: + - apk add --no-cache openssh + - mkdir -p ~/.ssh + - echo "$DEPLOY_KEY" | base64 -d > ~/.ssh/id_rsa + - chmod 600 ~/.ssh/id_rsa + - ssh-keyscan -H "$DEPLOY_HOST" >> ~/.ssh/known_hosts + script: + - echo "Deploying backend to $DEPLOY_HOST" + - scp -r $PUBLISH_DIR/. $DEPLOY_USER@$DEPLOY_HOST:$BACKEND_DEPLOY_PATH + - > + ssh $DEPLOY_USER@$DEPLOY_HOST " + cd $BACKEND_DEPLOY_PATH && + echo 'Running migrations' && + ./efbundle && + echo 'Restarting systemd service' && + systemctl --user restart SurveyBackend.service" \ No newline at end of file diff --git a/SurveyFrontend/frontend.gitlab-ci.yml b/SurveyFrontend/frontend.gitlab-ci.yml new file mode 100644 index 0000000..d5736da --- /dev/null +++ b/SurveyFrontend/frontend.gitlab-ci.yml @@ -0,0 +1,37 @@ +stages: + - build + - test + - deploy + +frontend-build-job: + image: node:20-alpine + stage: build + script: + - cd SurveyFrontend + - npm install + - npm run build + artifacts: + paths: + - SurveyFrontend/dist + expire_in: 1 hour + +frontend-test-job: + image: alpine:latest + stage: test + script: + - echo "Here we would run some tests if I knew how to use them" + +frontend-deploy-job: + image: alpine:latest + stage: deploy + rules: + - if: '$CI_COMMIT_BRANCH == "main"' + before_script: + - apk add --no-cache openssh + - mkdir -p ~/.ssh + - echo "$DEPLOY_KEY" | base64 -d > ~/.ssh/id_rsa + - chmod 600 ~/.ssh/id_rsa + - ssh-keyscan -H "$DEPLOY_HOST" >> ~/.ssh/known_hosts + script: + - echo "Deploying frontend to $DEPLOY_HOST" + - scp -r SurveyFrontend/dist/* $DEPLOY_USER@$DEPLOY_HOST:$FRONTEND_DEPLOY_PATH