Deploy to a VM via SCP

Deploy your application to Tomcat using SCP

Prerequisites

The example Java Application

You can find the example project on GitHub.

The example application is a simple Hello World Java application using the Spark Java framework:

Hello World!

Hello World!
	@Override
	public void init() {
		get("/hello", (req, res) -> "Hello World");
	}

Create the pipeline

Our pipeline has three stages: clone, package, and transfer.

SCP pipeline

Codefresh UI Pipeline View

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.

Note that you need to change the environment variables under the transfer step to your respective values.

codefresh.yml

# More examples of Codefresh YAML can be found at
# https://codefresh.io/docs/docs/example-catalog/example

version: "1.0"
# Stages can help you organize your steps in stages
stages:
  - "clone"
  - "package"
  - "transfer"

steps:
  clone:
    title: "Cloning repository..."
    type: "git-clone"
    stage: "clone"
    arguments:
      repo: "codefresh-contrib/scp-war-app"
      
  package:
    title: "Packaging war..."
    type: "freestyle"
    stage: "package"
    arguments:
      image: "maven:3.5.2-jdk-8-alpine"
      working_directory: "${{clone}}"
      commands:
        - "mvn -Dmaven.repo.local=/codefresh/volume/m2_repository clean package"

  transfer:
    title: "Transferring war to Tomcat..."
    type: "freestyle"
    stage: "transfer"
    arguments:
        image: "ictu/sshpass:latest"
        working_directory: "${{package}}/target"
        environment:
          - USER=<USER>
          - HOST=<HOST.IP.ADDRESS>
          - PASSWORD=<PASSWORD>
          - TOMCAT_DIR=<path/to/tomcat/webapps>
        commands:
          - "echo | ssh-keygen -P '' -t rsa"
          - "sshpass -p $PASSWORD ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $USER@$HOST"
          - "scp sparkjava-hello-world-1.0.war $USER@$HOST:$TOMCAT_DIR"

The above pipeline does the following:

  1. Clones the main repository through the git-clone step.
  2. Installs the dependencies via Maven and packages our war file through a freestyle step.
  3. Transfers our application via scp to a Tomcat server through another freestyle step.

Note that you will need to change the listed environment variables accordingly, either through the YAML itself, or through your pipeline settings:

Pipeline variables

Pipeline variables

CD pipeline examples