simone
simone

Reputation: 119

How to deploy maven project on aws with Gitlab CI/CD

I'm trying to deploy a java maven project on aws with Gitlab CI/CD.

This is my .gitlab-ci.yml


image: maven:3-jdk-8
services:
    - docker:dind

stages:
    - test
    - build
    - deploy

maven-test:
    stage: test
    script:
        - echo "Test stage"
        - mvn clean validate compile test -B


maven-build:
    stage: build
    script:
        - echo "Build stage"
        - mvn install -B -DskipTests
    artifacts:
        paths:
            - ./target/*.jar

maven-deploy:
    stage: deploy
    script:
        - echo "Deploy stage"
        - scp -v -o StrictHostKeyChecking=no -I "mykey.pem" ./target/*.jar [email protected]:*.jar
    when: manual


If I execute the scp command on a terminal in my pc then the jar is uploaded in aws ec2 instance while in gitlab I have errors and the jar is not uploaded.

This is my first approach with Gitlab CI and aws, so can someone explain step by step what I need to do to deploy the project in aws ec2 instance with Gitlab CI?

Thanks!

Upvotes: 0

Views: 1733

Answers (1)

hmanolov
hmanolov

Reputation: 113

Since you have not posted much about your problem nor did you post the error I will just suggest a few things to look at:

From a GitLab perspective:

  1. Are you sure that the "mykey.pem" is available within the repository when running that command(maven-deploy) on the the gitlab-runner.?
  2. Also are you sure that you are using a docker gitlab-runner, if you are not then you can't use the image: directive and therefore it might not not have mvn/scp locally.
  3. You might want to look into the dependencies directive and ensure you make that artifact available in next task. This should be done by default!

From an AWS perspective:

  1. Make sure that the ubuntu target machine/server has port 22 exposed to the EC2 machine running the gitlab-runner.

Edit:

If the error you are receiving is with the pem files permissions then take a look at this resolution for AWS EC2 pem file issue. Another similar resolution is here.

Seems like if you put chmod 400 mykey.pem before the scp it might fix your problem.

Upvotes: 1

Related Questions