如何使用Gitlab CI/CD在AWS上部署Maven项目 [英] How to deploy maven project on aws with Gitlab CI/CD

查看:616
本文介绍了如何使用Gitlab CI/CD在AWS上部署Maven项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Gitlab CI/CD在aws上部署java maven项目.

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

这是我的.gitlab-ci.yml

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 ubuntu@xxxxxxx.com:*.jar
    when: manual


如果我在PC的终端上执行scp命令,则jar将被上传到aws ec2实例中,而在gitlab中,我将报错并且jar将不会被上传.

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.

这是我使用Gitlab CI和aws的第一种方法,因此有人可以逐步说明使用Gitlab CI在aws ec2实例中部署项目需要做什么吗?

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?

谢谢!

推荐答案

由于您未就问题发表过多意见,也未张贴错误,因此我只建议您看几件事:

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:

从GitLab角度来看:

  1. 在gitlab-runner上运行该命令(maven-deploy)时,您确定存储库中的"mykey.pem"可用吗?
  2. 您还确定使用的是 docker gitlab-runner ,如果不是,则不能使用指令,因此它可能在本地没有mvn/scp.
  3. 您可能希望研究依赖项指令,并确保您使该工件在下一个任务中可用.默认情况下应该这样做!
  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!

从AWS角度来看:

  1. 确保ubuntu目标机器/服务器的运行gitlab-runner的EC2机器具有端口22.

如果您收到的错误与pem文件权限有关,请查看此处.

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.

好像您将chmod 400 mykey.pem放在scp之前,可能会解决您的问题.

这篇关于如何使用Gitlab CI/CD在AWS上部署Maven项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆