6

Right now, I deploy my (Spring Boot) application to EC2 instance like:

  1. Build JAR file on local machine
  2. Deploy/Upload JAR via scp command (Ubuntu) from my local machine

I would like to automate that process, but:

  1. without using Jenkins + Rundeck CI/CD tools
  2. without using AWS CodeDeploy service since that does not support GitLab

Question: Is it possible to perform 2 simple steps (that are now done manualy - building and deploying via scp) with GitLab CI/CD tools and if so, can you present simple steps to do it.

Thanks!

milosdju
  • 783
  • 12
  • 27

3 Answers3

2

You need to create a .gitlab-ci.yml file in your repository with CI jobs defined to do the two tasks you've defined.

Here's an example to get you started.

stages:
  - build
  - deploy

build:
  stage: build
  image: gradle:jdk
  script:
    - gradle build
  artifacts:
    paths:
      - my_app.jar

deploy:
  stage: deploy
  image: ubuntu:latest
  script:
    - apt-get update
    - apt-get -y install openssh-client
    - scp my_app.jar target.server:/my_app.jar

In this example, the build job run a gradle container and uses gradle to build the app. GitLab CI artifacts are used to capture the built jar (my_app.jar), which will be passed on to the deploy job.

The deploy job runs an ubuntu container, installs openssh-client (for scp), then executes scp to open my_app.jar (passed from the build job) to the target server.

You have to fill in the actual details of building and copying your app. For secrets like SSH keys, set project level CI/CD variables that will be passed in to your CI jobs.

King Chung Huang
  • 5,026
  • 28
  • 24
0

Create shell file with the following contents.

#!/bin/bash
# Copy JAR file to EC2 via SCP with PEM in home directory (usually /home/ec2-user)
scp -i user_key.pem file.txt ec2-user@my.ec2.id.amazonaws.com:/home/ec2-user
#SSH to EC2 Instnace
ssh -T -i "bastion_keypair.pem" ec2-user@y.ec2.id.amazonaws.com /bin/bash <<-'END2'
 #The following commands will be executed automatically by bash.
 #Consdier this as remote shell script.
 killall java
 java -jar ~/myJar.jar server ~/config.yml &>/dev/null &
 echo 'done'
 #Once completed, the shell will exit.
END2
mmansoor
  • 580
  • 5
  • 11
0

In 2020, this should be easier with GitLab 13.0 (May 2020), using an older feature Auto DevOps (introduced in GitLab 11.0, June 2018)

Auto DevOps provides pre-defined CI/CD configuration allowing you to automatically detect, build, test, deploy, and monitor your applications.
Leveraging CI/CD best practices and tools, Auto DevOps aims to simplify the setup and execution of a mature and modern software development lifecycle. Overview

But now (May 2020):

Auto Deploy to ECS

Until now, there hasn’t been a simple way to deploy to Amazon Web Services. As a result, Gitlab users had to spend a lot of time figuring out their own configuration.

In Gitlab 13.0, Auto DevOps has been extended to support deployment to AWS!

Gitlab users who are deploying to AWS Elastic Container Service (ECS) can now take advantage of Auto DevOps, even if they are not using Kubernetes. Auto DevOps simplifies and accelerates delivery and cloud deployment with a complete delivery pipeline out of the box. Simply commit code and Gitlab does the rest! With the elimination of the complexities, teams can focus on the innovative aspects of software creation!

In order to enable this workflow, users need to:

  • define AWS typed environment variables: ‘AWS_ACCESS_KEY_ID’ ‘AWS_ACCOUNT_ID’ and ‘AWS_REGION’, and
  • enable Auto DevOps.

Then, your ECS deployment will be automatically built for you with a complete, automatic, delivery pipeline.

See documentation and issue

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250