DEV Community

Hong duc
Hong duc

Posted on

Ways to deploy code to production server (linux server) with jenkins ?

I want to automate the process of build then deploy (some nodejs application) to production server with jenkins. Right now my company using svn to deploy code production server, that is commit code to svn then log in production server via ssh and run svn up.
I don't think I can automate the deploy stage with svn, so I want to seek another way to deploy code to production server that is also easy to automate with jenkins.
Thanks

Top comments (9)

Collapse
 
habereder profile image
Raphael Habereder • Edited

That depends on what you want to deploy.
There are dozens of tools that can do the job better than jenkins.

Jenkins should be used to build the artifact and put it to storage.
Letting Jenkins deploy stuff is a security flaw, since everyone with access to create Job-Items can easily get any credential they want from a badly secured Jenkins instance. Most people don't invest the time to secure Jenkins enough to make it a safe deployment-tool.

Ansible, Puppet, Chef, and other configuration management tools can easily take care of deployment for you.

What is it you want to deploy?

Collapse
 
hongduc profile image
Hong duc

I see.
I want to deploy nodejs app. It in typescript I use jenkin to compile it to javascript then deploy
Thanks

Collapse
 
habereder profile image
Raphael Habereder • Edited

I'm not that familiar with JavaScript development, but this could do it for you:


node {
    stage("Checkout") {
        //checkout your app
    }

    stage("Build") {
        //Build your app
    }

    stage("Deployment") {
        sshPublisher(publishers: [
            sshPublisherDesc(configName: '', sshCredentials: [
                encryptedPassphrase: '{encrypted_ssh_passphrase}', 
                key: '', 
                keyPath: '', 
                username: 'user'
            ], 
            transfers: [
                sshTransfer(cleanRemote: false, 
                excludes: '', 
                execCommand: 'commandToExecuteAfterTransfer', 
                execTimeout: 120000, 
                flatten: false, 
                makeEmptyDirs: false, 
                noDefaultExcludes: false, 
                patternSeparator: '[, ]+', 
                remoteDirectory: 'remoteDirectoryOfYourServer', 
                remoteDirectorySDF: false, 
                removePrefix: '', 
                sourceFiles: 'pathToFilesToCopy/*')
            ], 
            usePromotionTimestamp: false, 
            useWorkspaceInPromotion: false, 
            verbose: false)
        ])
    }
}

This is done via the SSH-Publisher Plugin. Though I still do not recommend deploying via Jenkins, since the SSH and SCP Publisher Plugins are very old (2 and 10 years respectively) and may contain vulnerabilities.

Another approach would be this, which is marginally better, if your target-server supports it. This approach again, is not really recommendable either, since it requires NPM to be on a production machine, which is a bad idea tbh.

What I instead would recommend is, let Ansible or other tools take care of it.
Push your built code to a git repo and let ansible pull the production code. That way you don't need a compiler/interpreter on your prod machine. This could also be automated via jenkins easily, since it has ansible plugins that could run your playbooks after the production code is committed to git.

I love jenkins, but it's not the best tool for everything, unfortunately :D

Thread Thread
 
hongduc profile image
Hong duc

I see, I new to jenkins so I don't know, Now this is something I need to think hard about. Thank you

Thread Thread
 
habereder profile image
Raphael Habereder

No worries, Jenkins is a powerful tool for many use-cases and languages.

If you need advice, feel free to hit me up or answer in another comment, I've been doing continuous delivery for a few years now and got a little bit of experience with it :D

Collapse
 
digianpaul profile image
Paul DiGian

Jenkins can definitely log into a remote server using ssh.

I am not sure I understand the question completely...

Collapse
 
hongduc profile image
Hong duc

It seem like I quickly jump to conclusion :) . My point is that I want to know what other ways than using svn to deploy code. It just that I want to know more so that I can choose the one that more fit in my workplace. Thanks

Collapse
 
digianpaul profile image
Paul DiGian

It really depends on your environment and what you are doing and how. In my opinion in standard environments the first step is to contenerize the application during CI/CD.

You commit / release a tag and automatically you get a container. You run the unittest inside the container and the integration test against the container. If everything is green you deploy the new container.

But this is only one approach, whatever you decide to do, is important to make sure it works well also with the cloud provider you are using.

Without more information is hard to help...

Thread Thread
 
hongduc profile image
Hong duc

Thanks. I will rethink every thing again to be more clear :)