|
| 1 | +--- |
| 2 | +title:"Deploy to a VM via SCP" |
| 3 | +description:"Deploying your application to Tomcat using SCP" |
| 4 | +group:yaml-examples |
| 5 | +sub_group:examples |
| 6 | +toc:true |
| 7 | +--- |
| 8 | + |
| 9 | +##Prerequisites |
| 10 | + |
| 11 | +- A[free Codefresh account](https://codefresh.io/docs/docs/getting-started/create-a-codefresh-account/) |
| 12 | +- A distribution of[Tomcat](https://tomcat.apache.org/download-90.cgi) setup on a remote server (running with port 8080 exposed) |
| 13 | + |
| 14 | +##The Example Java Application |
| 15 | + |
| 16 | +You can find the example project on[Github](https://github.com/codefresh-contrib/scp-war-app). |
| 17 | + |
| 18 | +The example application is a simple Hello World Java application using the[Spark Java framework](http://sparkjava.com/): |
| 19 | + |
| 20 | +{% include image.html |
| 21 | +lightbox="true" |
| 22 | +file="/images/examples/deployments/scp-hello-world.png" |
| 23 | +url="/images/examples/deployments/scp-hello-world.png" |
| 24 | +alt="Hello World!" |
| 25 | +caption="Hello World!" |
| 26 | +max-width="100%" |
| 27 | +%} |
| 28 | + |
| 29 | + |
| 30 | +```java |
| 31 | +@Override |
| 32 | +publicvoid init() { |
| 33 | +get("/hello", (req, res)->"Hello World"); |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +##Create the Pipeline |
| 38 | + |
| 39 | +Our pipeline will have three stages: clone, package, and transfer. |
| 40 | + |
| 41 | +{% include image.html |
| 42 | +lightbox="true" |
| 43 | +file="/images/examples/deployments/scp-pipeline.png" |
| 44 | +url="/images/examples/deployments/scp-pipeline.png" |
| 45 | +alt="SCP pipeline" |
| 46 | +caption="Codefresh UI Pipeline View" |
| 47 | +max-width="100%" |
| 48 | +%} |
| 49 | + |
| 50 | +You should be able to copy and paste this YAML in the in-line editor of the Codefresh UI. It will automatically clone the project for you. |
| 51 | + |
| 52 | +Note that you need to change the environment variables under the`transfer` step to your respective values. |
| 53 | + |
| 54 | +`codefresh.yml` |
| 55 | +```yaml |
| 56 | +# More examples of Codefresh YAML can be found at |
| 57 | +# https://codefresh.io/docs/docs/yaml-examples/examples/ |
| 58 | + |
| 59 | +version:"1.0" |
| 60 | +# Stages can help you organize your steps in stages |
| 61 | +stages: |
| 62 | + -"clone" |
| 63 | + -"package" |
| 64 | + -"transfer" |
| 65 | + |
| 66 | +steps: |
| 67 | +clone: |
| 68 | +title:"Cloning repository..." |
| 69 | +type:"git-clone" |
| 70 | +stage:"clone" |
| 71 | +arguments: |
| 72 | +repo:"codefresh-contrib/scp-war-app" |
| 73 | + |
| 74 | +package: |
| 75 | +title:"Packaging war..." |
| 76 | +type:"freestyle" |
| 77 | +stage:"package" |
| 78 | +arguments: |
| 79 | +image:"maven:3.5.2-jdk-8-alpine" |
| 80 | +working_directory:"${{clone}}" |
| 81 | +commands: |
| 82 | + -"mvn -Dmaven.repo.local=/codefresh/volume/m2_repository clean package" |
| 83 | + |
| 84 | +transfer: |
| 85 | +title:"Transferring war to Tomcat..." |
| 86 | +type:"freestyle" |
| 87 | +stage:"transfer" |
| 88 | +arguments: |
| 89 | +image:"ictu/sshpass:latest" |
| 90 | +working_directory:"${{package}}/target" |
| 91 | +environment: |
| 92 | + -USER=<USER> |
| 93 | + -HOST=<HOST.IP.ADDRESS> |
| 94 | + -PASSWORD=<PASSWORD> |
| 95 | + -TOMCAT_DIR=<path/to/tomcat/webapps> |
| 96 | +commands: |
| 97 | + -"echo | ssh-keygen -P '' -t rsa" |
| 98 | + -"sshpass -p $PASSWORD ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $USER@$HOST" |
| 99 | + -"scp sparkjava-hello-world-1.0.war $USER@$HOST:$TOMCAT_DIR" |
| 100 | +``` |
| 101 | +
|
| 102 | +The above pipeline does the following: |
| 103 | +
|
| 104 | +1. A [git-clone]({{$site.baseurl}}/docs/codefresh-yaml/steps/git-clone/) step that clones the main repository |
| 105 | +2. A [freestyle step]($$site.baseurl}}/docs/codefresh-yaml/steps/freestyle/) that installs the dependencies via Maven and packages our war file |
| 106 | +3. A freestyle step that transfers our application via scp to a Tomcat server. Note that you will need to change the listed environment variables accordingly, either through the YAML itself, or through your pipeline settings: |
| 107 | +
|
| 108 | +{% include image.html |
| 109 | +lightbox="true" |
| 110 | +file="/images/examples/deployments/scp-variables.png" |
| 111 | +url="/images/examples/deployments/scp-variables.png" |
| 112 | +alt="Pipeline variables" |
| 113 | +caption="Pipeline variables" |
| 114 | +max-width="100%" |
| 115 | +%} |
| 116 | +
|
| 117 | +## What to Read Next |
| 118 | +
|
| 119 | +- [Git-clone Step]({{site.baseurl}}/docs/codefresh-yaml/steps/git-clone/) |
| 120 | +- [Freestyle Step]({{site.baseurl}}/docs/codefresh-yaml/steps/freestyle/) |
| 121 | +- [Deploying to a VM using FTP]({{site.baseurl}}/docs/yaml-examples/examples/transferring-php-ftp/) |