DEV Community

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

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