Automatically deploy after each commit


#1

Hi, I have next situation
I have server on Digital Ocean, install gitlab runner and config them as here https://about.gitlab.com/2016/04/19/how-to-set-up-gitlab-runner-on-digitalocean/
All is ok but…
All the time when I want to see changes from my repo on server I must

  1. stop docker
  2. remove image
  3. docker pull registry.gitlab.com/jokerruslan/test/image
  4. docker run -d -p 8080:5000 -t registry.gitlab.com/jokerruslan/test/image
    It’s annoying me. Please Help. I want each my commit should be deployed to the server automatically.

Here is my gitlab-ci.yml file

   image : microsoft/dotnet:latest

services:
  - docker:dind # Обязательно!

stages:
  - build
  - imagecreate

variables:
  CONTAINER_TEST_IMAGE: registry.gitlab.com/jokerruslan/test/image:$CI_COMMIT_REF_NAME
  CONTAINER_RELEASE_IMAGE: registry.gitlab.com/jokerruslan/test/image:latest

build:
  stage: build
  script:
    - 'dotnet restore ./src/DentBox.Web/DentBox.Web.csproj'
    - 'dotnet publish ./src/DentBox.Web/DentBox.Web.csproj'
  only:
    - master
  #artifacts:
    #paths:
      #- /builds/jokerruslan/test/bin/Debug/netcoreapp2.1/ # Для демонстрации - создадим артефакт в виде собранного проекта

imagecreate:
  image: docker:latest
  stage: imagecreate
  script:
    - docker login --username gitlab-ci-token --password $CI_BUILD_TOKEN registry.gitlab.com
    - docker build -t $CONTAINER_TEST_IMAGE .
    - docker push $CONTAINER_TEST_IMAGE
    - docker pull $CONTAINER_TEST_IMAGE
    - docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
    - docker push $CONTAINER_RELEASE_IMAGE
  only:
    - master

Here is my docker file

FROM microsoft/dotnet:2.1-sdk AS builder
WORKDIR /source

COPY src/DentBox.Web/DentBox.Web.csproj ./src/DentBox.Web/
COPY src/DentBox.Models/DentBox.Models.csproj ./src/DentBox.Models/
COPY src/DentBox.Services/DentBox.Services.csproj ./src/DentBox.Services/

RUN dotnet restore ./src/DentBox.Models/DentBox.Models.csproj
RUN dotnet restore ./src/DentBox.Services/DentBox.Services.csproj
RUN dotnet restore ./src/DentBox.Web/DentBox.Web.csproj

COPY src/aspdocker.tests/aspdocker.tests.csproj ./src/aspdocker.tests/
RUN dotnet restore ./src/aspdocker.tests/aspdocker.tests.csproj

COPY . .

#RUN dotnet test ./src/aspdocker.tests/aspdocker.tests.csproj

RUN dotnet publish ./src/DentBox.Web/DentBox.Web.csproj --output /app/ --configuration Release


FROM microsoft/dotnet:2.1-sdk
WORKDIR /app
COPY --from=builder /app .

EXPOSE 5000/tcp
ENTRYPOINT ["dotnet", "DentBox.Web.dll"]