Deploy codes to remote server over SSH method
Tags: linux, devops, jenkins
Published: 2023-11-13 13:46:57

- Create a Git Repository name "deploycode" add README.md file mention description as deploycodetoremoteserver.

- Clone the Git repo to local machine.

- Create Git branch as "dev" & "test"

- Create index.html file for both branches

```
kannan@kannan-PC:~/deploycode$ ls
index.html  README.md
kannan@kannan-PC:~/deploycode$ cat index.html 
<h1>Testcode</h1>
<h1>testcode version-1</h1>
<h1>testcode version-2</h1>

kannan@kannan-PC:~/deploycode$ git branch 
  dev
  main
* test
kannan@kannan-PC:~/deploycode$ git checkout dev
Switched to branch 'dev'
kannan@kannan-PC:~/deploycode$ git branch 
* dev
  main
  test
kannan@kannan-PC:~/deploycode$ ls
index.html  README.md
kannan@kannan-PC:~/deploycode$ cat index.html 
<h1>Devcode </h1>
<h1>dev code version-1</h1>
<h1>dev code version-2</h1>

```

- Git add . and commit, Push to the Git repo.

## Remote server setup

- Create a remote server 

- Do the "apt update" and and "install apache2" on the both server

- Do the the "systemctl start, enable, status of the apache service"

## Jenkins setup
- Go to Jenkins dashboard > Manage Jenkins > Plugins > "publish over SSH"

- Manage Jenkins > System > SSH Servers (Add the SSH server details for dev and test)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcqbl1567zt7amw4g07e.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mo48lyjwjqur1dptzhxe.png)

- Click on advance "Use password authentication" enter the password for the remote server.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yzqxvpb6nb6y6w0powl.png)

- Test run the configuration to check the remote server connectivity.

## Jenkins Project for devserver.
- Go to Jenkins dashboard > Add items > Freestyle project> ok

- Select the Github project and paste the Git repo "URL".
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ojxo45ox2aek9xx09mx.png)

- Copy the Git repo http url and mention the branch as "dev"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gp3kx134l6vb1961svdf.png)

- Select the "Poll SCM" and set the schedule period.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t39xidpvvg74scl1orlw.png)

- Select send files or execute commands over SSH.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kqv02t1djn1mzudixywo.png)

- Select Editable e mail Notification, add the email on "Project recipient list"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fa61w4jyrcxq7r86fnsd.png)

- Once done it will automatically Build Now and provides the output on Console output.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yapkszc8bk8pq9cc7mwf.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3w15t8hxz6fe392icd3.png)

- verify the output by http://devserver_ip.

## Jenkins Project for testserver.

- Follow the same configuration procedure as the above "General, source code management, Build trigger, Post buils action". 
- Only need to modify the Build steps to run the  "testserver"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5j9gw87wedjrtfll4e0.png)

- Once done it will automatically Build Now and provides the output on Console output.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohjkw8tqt0s2jugjdndm.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4466aqzjam8dsvuzst15.png)

- verify the output by http://testserver_ip.