Search code examples
node.jsjenkinsdotenv

how to create .env file for nodejs project while jenkins job is running


I have a nodejs MERN stack project. I have configured Jenkins job on my server Linux instance in a docker container. I have a jenkins job to pull the code and build the application. I have used the .env file to provide some variables and credentials how can I create this file while this Jenkins job is running. As I want to dockerize the nodejs project content and run it somewhere else.

I am totally new for DevOps

It's a Jenkins job.

I want to parameterize the Jenkins job and it should create a .env file at the root of my nodejs project.


Solution

  • Why do you want to generate your envfile with jenkins ?

    You can create your environment files by hand for each of your environments and push them into your image as you run your image like:

    sudo docker run -d --restart=always  --name=${appName}  --env-file ${envfilepath}/${envfile}.env -p ${externe appPort}:${interne appPort} ${your registry}/${appName}:${your tag}
    

    You can also make a combination of rundeck to cron and start your job, kubernetes for the management of your variables of env.

    If you really want to create your envfile when you build your image, in your dockerfile, you can do :

    ################################ builder
    FROM ${BUILDER_IMAGE} as builder
    RUN npm i
    RUN npm run `command to create envfile`
    # see content of your file
    RUN cat `path of your envfile / your envfile`
    
    ################################ final build
    FROM ${BUILDER_IMAGE} 
    COPY --from=builder `path of your envfile / your envfile` `definitive path of your envfile / definitive envfile`
    

    And if you want to export your file from your docker image

    (docker run -t --rm --entrypoint cat $(IMAGE) `path of your envfile / your envfile`) > `local path of your envfile / local envfile`