Jenkins deploy artifact on same server - jenkins

I'm trying to create a Jenkins Pipeline or group of itens to help me create a custom CI/CD for my projects and right now i'm stuck at the deploy part, i want to deploy on the same server that my jenkins is running (Windows Server/IIS). I would also like to know how to deploy to another server (Windows Server/IIS), this second one would be my production env.
I have managed to clone, build and archive using two approaches with Jenkins:
Pipelines
I have managed to create a pipeline that will clone my project, execute my build and then archive the artifacts from my build. The problem is, how do i deploy the artifact now?
This is my pipeline script
node {
stage('git clone') {
// Get some code from a GitHub repository
git 'my-git-url'
}
stage('npm install') {
bat label: 'npm install',
script: '''cd app
npm install'''
}
stage('gulp install') {
bat label: 'gulp install',
script: '''cd app
npm i gulp'''
}
stage('gulp production') {
bat label: 'gulp production',
script: '''cd app
gulp production'''
}
stage('create artifact') {
archiveArtifacts artifacts: 'app/dist/**',
onlyIfSuccessful: true
}
}
Freestyle projects
I have managed to create a project that will build and then archive the artifact using Execute shell build step and the Archive the artifacts post-build actions. How can i deploy the artifact using this approach? On this case i'm trying to trigger a second freestyle project to execute the deploy.

According to your question : "I want to deploy on the same server that my jenkins is running (Windows Server/IIS)" .. and comments I will suggest some approaches.
Windows
Use windows as operative system for production environments is not recommended. Linux is the only and the best choice.
IIS
I don't recommed IIS to deploys static assets. You need something more light and scalable. You could use :
nodejs with pm2 (https://expressjs.com/en/starter/static-files.html)
nginx (https://medium.com/#jgefroh/a-guide-to-using-nginx-for-static-websites-d96a9d034940)
apache (http://book.seaside.st/book/advanced/deployment/deployment-apache/serving-files)
docker
Deploy on IIS
Deploy static assets on IIS is just copy and paste the files on some folder and point IIS configurations to that folder:
https://www.atlantic.net/hipaa-compliant-hosting/how-to-build-static-website-iis/
Basic deploy on IIS using Jenkins
After your build commands, you just need to copy the build results (css.js.html.etc) and paste to some folder like c://webapps/my-app (pre-configured in IIS).
You can do this using a simple shell execution in free style project or pipeline script like https://stackoverflow.com/a/53159387/3957754
You could use this approach to deploy your static assets on the same server that your jenkins is running.
Advanced deploy on IIS using Jenkins
Microsoft has a tool called MSDeploy. Basically is a command line tool to deploy apps on remote IIS:
msdeploy.exe -verb:sync -source:contentPath="" -dest:contentPath=""
More details here:
https://stackoverflow.com/a/12032030/3957754
Note: You can't run MS deploy commands that talk to the MSDeploy service on the same machine
Jenkins Agent
Jenkins agent is an application that runs on a remote server, not where jenkins master node runs.
https://wiki.jenkins.io/display/JENKINS/Step+by+step+guide+to+set+up+master+and+agent+machines+on+Windows
Your master jenkins could use an agent in the remote or localhost IIS and execute jenkins jobs with copy and paste approach.

Related

Jenkins to automate process on Server

Currently on a server, an application is being deployed.
Structure of the project on the server:
start-switch.json
application_snapshot-001.jar
appconfig.properties
workflow.yaml
other folders
Every time there is a change, we have to manually stop the server (pm2 stop APP_NAME), delete/replace the jar file and restart server (pm2 start start-switch.JSON) which is really cumbersome.
I want to automate this process using Jenkins to stop the server, replace the jar file which is pulled from GitHub repository and restart the server.
This will also help to keep the version of each deployment on the server.
How can I do these steps on the Jenkins file?
Thanks in advance.
This is a fairly normal implementation you can do with Jenkins. First, switch to your project directory and use sh to stop the server. Then use the GitSCM plugin to pull your jar. Delete the old jar, paste in the new one, and use sh again to start the server. Something like this:
dir("path/to/proj_dir"){
sh "pm2 stop APP_NAME"
sh "rm application_snapshot-001.jar"
checkout scmGit(
branches: [[name: 'master']],
userRemoteConfigs: [[url: '<repo>']])
sh "mv path/to/new/jar path/to/old/jar application_snapshot-001.jar"
sh "pm2 start start-switch.JSON"
}
If you are using a Windows machine then you will have to use bat instead of sh.

Problems transferring build artifacts from Jenkins running in a docker container

I'm a little bit of a newb, with this CI/CD container stuff so please correct me anywhere I'm wrong.
I can't seem to find out how to send by npm build files created on my jenkins instance (workspace) to a remote server. I have a pipeline that successfully pulls in my github repo, does all my fun npm stuff (npm install, test, build). I see my build dir in my jenkins instance /workspace.
My environment is as follows. We have a server where docker (with Portainer) is installed. Jenkins is running in a container with a volume mounted (my react build dir goes here). No issues with the pipeline or building etc. I just can't figure out how to push my artifacts from my jenkins workspace directory to my 'remote' dev server.
I can successfully open a console in my jenkins container (portainer as the jenkins user) and scp files from the workspace directory using my remote server creds(but password is necessary).
I installed and used "Publish Over SSH" Jenkins plugin and get a successful "Test Configuration" from my setup.
I created my RSA keys on the REMOTE machine (that I'm trying to push my build files to).
I then pasted the private key (created without a password) into the plugin at the 'Use password authentication, or use a different key' section. Again, I get a successful test connection.
In my pipeline the last step is deploying and I use this command
sh 'scp -r build myusername#xx.xx.xx.xx:/var/files/react-tester'
I get a 'Permission denied (publickey,password).' error. I have no password associated with the rsa key. I tried both ways, creating the rsa key on the remote machine as my remote user, and the jenkins machine as the jenkins user. I've read examples of people creating the keys both ways, but not sure which user/machine combo to create the keys and paste to which section of the 'Publish Over SSH' plugin.
I'm out of ideas.
First, go to "Manage Jenkins" > "Credentials", add a new SSH credential of type "SSH Username with private key" and fill the "Username" and your private key (generate one if you haven't done it yet) fields (you can also upload one). Don't forget that you have to copy the generated public key to the ${SSH_USERNAME}/.ssh/authorized_keys file on the remote server.
I'm assuming you're using a scripted or DSL pipeline here. In your code, after you've builded your application, you can push it to your server adding this step to your pipeline:
pipeline {
stages {
stage("Pushing changes to remote server") {
steps {
script {
def remote_server = "1.2.3.4"
withCredentials([sshUserPrivateKey(credentialsId: 'my-key', keyFileVariable: 'SSH_KEY', passphraseVariable: '', usernameVariable: 'SSH_USERNAME')]) {
sh "scp -i \${SSH_KEY} build/ ${SSH_USERNAME}#${remote_server}:/var/files/react-tester/"
}
}
}
}
}
}
Best regards.

How to deploy React Django app automatically on AWS EC2 using Jenkins

I am new to Jenkins. I have a project that runs Django and React on the same port which is running on AWS EC2 machine. My Database runs on RDS. Now I am trying to implement pipeline and automatic deployment using Jenkins. All the tutorials I was reading or watching suggest using AWS CodeDeploy service.
This is my sample Jenkinsfile now that tests my React code. As now the main intention is to do automatic code deployment so I am not thinking about testing python here. I just want whatever my code to be deployed on EC2 automatically
pipeline {
agent any
stages {
stage('Install dependency') {
steps {
sh "yarn install"
}
}
stage('Test project') {
steps {
sh "yarn test"
}
}
stage('Build project') {
steps {
sh "yarn build"
}
}
}
}
I know about AWS CodeDeploy plugin on Jenkins. But it uses S3 and AWS Code Deploy. What I am really trying to achieve is, Can I do something like this
I will build my project in Jenkins and it will send my project automatically to the ec2 machine and make it live.
OR
Can I connect to my ec2 instance and do the same that we do manually. Like - Login to instance, fetch the code from git, merge it, build it, restart the service

Copy file from Jenkins master to slave in Pipeline

I have some windows slave at my Jenkins so I need to copy file to them in pipeline. I heard about Copy To Slave and Copy Artifact plugins, but they doesn't have pipeline syntax manual. So I don't know how to use them in pipeline.
Direct copy doesn't work.
def inputFile = input message: 'Upload file', parameters: [file(name: 'parameters.xml')]
new hudson.FilePath(new File("${ENV:WORKSPACE}\\parameters.xml")).copyFrom(inputFile)
This code returns and error:
Caused: java.io.IOException: Failed to copy /var/lib/jenkins/jobs/_dev/jobs/(TEST)job/builds/107/parameters.xml to d:\Jenkins\workspace\_dev\(TEST)job\parameters.xml
Is there any way to copy file from master to slave in Jenkins Pipeline?
As I understand copyFrom is executed on your Windows node, therefore the source path is not accessible.
I think you want to look into the stash/unstash steps (Jenkins Pipeline: Basic Steps), which work across different nodes. Also this example might be helpful.
Pipeline DSL context runs on master node even that your write node('someAgentName') in your pipeline.
Try to use stash/unstash, but it is bad for large files.
Try External Workspace Manager Plugin. It has
pipelines steps and good for large files.
Try to use an intermediate storage. archive() and sh("wget $url") will be helpful.
If the requirement is to copy an executable to the test slave and to publish the test results, this is easy to do without the Copy to Slave plugin.
A shared folder should be created on each test slave (normal Windows shared folder).
After build: Build script copies the executable to the shared directory on each slave. A simple batch script using copy command is sufficient for this.
stage ('Copy to slaves') {
steps {
bat 'call "copy-to-slave.bat"'
}
}
During test: The test script copies the executable to another directory and runs it.
After test: Post-build action "Publish Robot Framework test results" can be used to report the test results. It is not necessary to copy the test result files back to the master first.
I recommend on Pipeline: Phoenix AutoTest plugin
Jenkins plugin website:
https://plugins.jenkins.io/phoenix-autotest/#documentation
GitHub repository of plugin:
https://github.com/jenkinsci/phoenix-autotest-plugin

Jenkins Build Flow AND Build Pipeline plugins

Will the Build Pipeline plugin still show the sequence of jobs properly if the jobs are run using Build Flow (including a repeated job)?
Here is pseudocode for our build flow:
build("Package")
build("Deploy", destination: "http://test") // deploy to our test environment
build("IntegrationTests", target: "http://test") // run automated tests
build("Deploy", destination: "http://stage") // deploy to stage
Package will pull code from source control, compile it, and store it as an artifact
Deploy will copy artifacts from the upstream Package job then copy it to the URL provided in the destination parameter
IntegrationTests will run a suite of integration tests against the URL provided in the destination parameter.
Will the Build Pipeline plugin show this pipeline as 4 steps even though the Deploy job is repeated?
Package => Deploy (test) => IntegrationTests => Deploy (stage)
The new solution, which is being adopted with Jenkins 2.0, is the Pipeline plugin. Also, CloudBees open-sourced their Pipeline Stage View plugin which provides a visualization to the Pipeline plugin.

Resources