DEV Community

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

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