Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy web application to AWS instance from GitLab repository

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!

like image 339
milosdju Avatar asked Jul 20 '18 12:07

milosdju


People also ask

How do I Deploy an application in AWS?

Sign in to the AWS Management Console and open the CodeDeploy console at https://console.aws.amazon.com/codedeploy . Sign in with the same account or IAM user information that you used in Getting started with CodeDeploy. In the navigation pane, expand Deploy, then choose Applications.


1 Answers

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.

like image 141
King Chung Huang Avatar answered Nov 15 '22 12:11

King Chung Huang