首页 > 解决方案 > 使用 Github Actions 将代码直接部署到 AWS EC2 实例

问题描述

正如标题所说,我正在尝试使用Github Actions将我的Laravel-Angular应用程序直接从Github部署到AWS EC2实例。

在我的应用程序中,需要在部署之前构建3 个Angular 8+项目。不需要构建laravel。

可用的解决方案建议使用AWS Elastic Beanstalk来部署代码。但是,如果要这样做,如何将弹性豆茎附加到现有实例还不够清楚。

有没有办法在不使用Elastic Beanstalk的情况下将代码部署到AWS EC2

这是我的Github Actions build.yml :

name: Build Develop Branch

on:
  push:
    branches: [ develop ]
  pull_request:
    branches: [ develop ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [14.x]

    steps:
    - name: Code Checkout
      uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - name: App 1 npm install
      run: npm install
      working-directory: angular-app-1
    - name: App 1 Build
      run: npm run build:staging
      working-directory: angular-app-1
    - name: App 2 npm install
      run: npm install
      working-directory: angular-app-2
    - name: App 2 Build
      run: node node_modules/@angular/cli/bin/ng build --configuration=staging
      working-directory: angular-app-2
    - name: App 3 npm install
      run: npm install
      working-directory: angular-app-3
    - name: App 3 Build
      run: node node_modules/@angular/cli/bin/ng build --configuration=staging
      working-directory: angular-app-3

标签: amazon-web-servicesgithubamazon-ec2github-actions

解决方案


有没有办法在不使用 Elastic Beanstalk 的情况下将代码部署到 AWS EC2?

我找到了一种使用GitHub Actions部署到 EC2 实例(或任何接受rsync命令的服务器)的简单方法。ssh

我在 repo 的.github/workflows文件夹中有一个简单的文件,每当向我的 GitHub repo 推送时, GitHub Actions都会运行该文件以部署到我的 EC2 实例。

没有麻烦,没有大惊小怪,没有特殊的咒语或拜占庭式 AWS 配置细节。

文件.github/workflows/pushtoec2.yml

name: Push-to-EC2

on: push

jobs:
  deploy:
    name: Push to EC2 Instance
    runs-on: ubuntu-latest

    steps:
      - name: Checkout the code
        uses: actions/checkout@v1

      - name: Deploy to my EC2 instance
        uses: easingthemes/ssh-deploy@v2.1.5
        env:
          SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }}
          SOURCE: "./"
          REMOTE_HOST: "ec2-34-213-48-149.us-west-2.compute.amazonaws.com"
          REMOTE_USER: "ec2-user"
          TARGET: "/home/ec2-user/SampleExpressApp"

上面使用的ssh deploy GitHub Action的详细信息。


推荐阅读