Robert Strauch
Robert Strauch

Reputation: 12906

Building a Docker image for a Node.js app in GitLab CI

I'm working on a Node.js application for which my current Dockerfile looks like this:

# Stage 0
# =======
FROM node:10-alpine as build-stage

WORKDIR /app

COPY package.json yarn.lock ./
RUN yarn install

COPY . ./
RUN yarn build

# Stage 1
# =======
FROM nginx:mainline-alpine

COPY --from=build-stage /app/build /usr/share/nginx/html

I'd like to integrate this into a GitLab CI pipeline but I'm not sure if I got the basic idea. So far I know that I need to create a .gitlab-ci.yml file which will be later picked up by GitLab.

My basic idea is:

  1. I push my code changes to GitLab.
  2. GitLab builds a new Docker image based on my Dockerfile.
  3. GitLab pushes this newly create image to a "production" server (later).

So, my question is:
My .gitlab-ci.yml should then contain something like a build job which triggers... what? The docker build command? Or do I need to "copy" the Dockerfile content to the CI file?

Upvotes: 1

Views: 7322

Answers (2)

Carlos Cavero
Carlos Cavero

Reputation: 3196

GitLab CI executes the pipeline in the Runners that need to be registered into the project using generated tokens (Settings/CI CD/Runners). You also can used Shared Runners for multiple projects. The pipeline is configured with the .gitlab-ci.yml file and you can build, test, push and deploy docker images using the yaml file, when something is done in the repo (push to branch, merge request, etc).

It’s also useful when your application already has the Dockerfile that can be used to create and test an image

So basically you need to install the runner, register it with the token of your project (or use Shared Runners) and configure your CI yaml file. The recommended aproach is docker in docker but it is up to you. You can also check this basic example. Finally you can deploy your container directly into Kubernetes, Heroku or Rancher. Remember to safely configure your credentials and secrets in Settings/Variables.

Conclusion

GitLab CI is awesome, but I recommend you to firstly think about your git workflow to use in order to set the stages in the .gitlab-ci.yml file. This will allow you to configure your node project as a pipeline an then it would be easy to export to other tools such as Jenkins pipelines or Travis for example.

Upvotes: 4

n3d4ti
n3d4ti

Reputation: 131

build job trigger:

option 1: add when: manual in the job and you can run the job by manual in CI/CD>Pipelines

option 2:

only:
    - <branchname>

in this case the job start when you push into the defined branch (this my personal suggest)

option 3: add nothin' and the job will run every time when you push code

Of corse you can combine the options above.

In addition may star the job with web request by using the job token.

docker build command will work in pipeline. I think in script section. Requirements docker engine on the gitlab-runner which pick the job.

Or do I need to "copy" the Dockerfile content to the CI file?

no

Upvotes: 0

Related Questions