How to write a .gitlab-ci.yml file?

Hello,
I have two servers as follows:

GitLab: GitLab Server
Runner: Docker and GitLab-Runner

I created a Node.js file on the GitLab server and I want to run this file through Nginx and Node.js containers. I know that I should have two Dockerfile and .gitlab-ci.yml files on the GitLab server.
The Dockerfile is as follows:

FROM node:latest as build-stage
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package*.json /usr/src/app/package.json
RUN npm install
RUN npm update
COPY . /usr/src/app
EXPOSE 3000


#production stage
FROM nginx:latest as production-stage
COPY --from=build-stage /usr/src/app /usr/share/nginx/html
COPY ./cfiles/default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD nginx -g 'daemon off;'

The default.conf is as follows:

upstream nodejs {
    server nodejs:3000;
}

server {
   listen 80;
   server_name default_server;
   error_log  /var/log/nginx/error.system-default.log;
   access_log /var/log/nginx/access.system-default.log;
   charset utf-8;


    root /usr/share/nginx/html;
    index index.html index.php index.js;

location ~ \.js$ {
    proxy_pass http://nodejs;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }

    location / {
     autoindex on;
     try_files $uri $uri/ $uri.html =404;
    }
}

But I don’t know what to do about the .gitlab-ci.yml file. I searched the internet for examples, but none of them related to what I want to do.
Please advice me.

Thank you.

Hi @hack3rcon ,

It seems like you want to do two things here:

  1. Build Docker Images from the Dockerfile
  2. Deploy a built Docker image to a server

See here for ways build and save container images: Build and push container images to the container registry | GitLab

Here are some examples of .gitlab-ci.yml configurations to accomplish this: Build and push container images to the container registry | GitLab

Deployment of container images to the cloud or other servers is not easy for others to advise you on, as the .gitlab-ci.yml configuration you need will depend on where the container image is deployed to and operated. Here are some examples:

1 Like

Hello,
Thank you so much for your reply.
I created a file like below:

image: docker:latest
services:
  - docker:dind
stages:
  - build
  - deploy
build:
  stage: build
  script:
    - docker compose build
deploy:
  stage: deploy
  script:
    - docker compose up -d

In your opinion, is this OK?