简体   繁体   English

Workflow.yaml 文件用于EC2实例持续部署使用GitHub Actions

[英]Workflow .yaml file for EC2 instance continuous deployment using GitHub Actions

I am having issues with the .yaml workflow file, as I do not know what it should look like.我对.yaml工作流文件有疑问,因为我不知道它应该是什么样子。 For instance, I do not know how to set up SSH connection to the server using key-pair and what should be the other jobs that I have to declare in order for the continuous deployment to be functioning properly?例如,我不知道如何使用密钥对设置与服务器的 SSH 连接,以及我必须声明哪些其他作业才能使持续部署正常运行? Thanks in advance.提前致谢。

This is what I have so far in the.yaml file这是我目前在 .yaml 文件中的内容

name: develop-deploy

concurrency: development

on:
  push:
    branches: [ deploy ]
  pull_request:
    branches: [ deploy ]

env:
  AWS_REGION: eu-central-1                  # Frankfurt
  
jobs:
  deployment:
    name: Deploy
    runs-on: self-hosted
    environment: development
    concurrency: development
    
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: ${{ env.AWS_REGION }}

      - name: Connect to EC2 server instance using SSH
        uses: 
      - run: 
 
      - name: Deploy to my EC2 instance
        uses: easingthemes/ssh-deploy@v2.1.5
        env:
          # Have to figure out a way to put the key-pair in secrets
          SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }}
          SOURCE: "./"
          REMOTE_HOST: # address
          REMOTE_USER: # user
          #TARGET:
      - name: Propagate changes and deploy on server
      - run:
          sudo docker-compose down
          git pull
          sudo docker-compose up -d
          # Getting inside the node container
          npm install
          npm run schema:sync

I build a .yml file for my personal project, It basically get the private key , hostname , and username from github secrets and then it stores in env.我为我的个人项目构建了一个.yml文件,它基本上从 github secrets 获取private keyhostnameusername ,然后存储在 env 中。

then I ssh into my ec2, and after it I update my repo.然后我 ssh 进入我的 ec2,然后我更新我的 repo。

After it I upload a folder to my ec2 using ssh之后我使用ssh将一个文件夹上传到我的ec2

And then start the pm2 server again.然后再次启动 pm2 服务器。

For reference you can check this yml file作为参考,您可以查看此yml 文件

To Load private-key , hostname , and username加载private-keyhostnameusername

jobs:
  ci:
    name: CI
    runs-on: ubuntu-latest
    env:
      PRIVATE_KEY: ${{ secrets.CLIENT_KEY  }}
      HOSTNAME : ${{ secrets.CLIENT_HOSTNAME  }}
      USER_NAME : ${{ secrets.CLIENT_USERNAME  }}

Changing private-key permissions and storing it locally inside github-action更改private-key权限并将其存储在本地github-action

- name: Changing permissions of PRIVATE_KEY
  run: echo "$PRIVATE_KEY" > private_key && chmod 600 private_key

To ssh into ec2 and perform some operations ssh 进入 ec2 并执行一些操作

- name: SSH into ec2 and do operations
  run: |
    ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} '
      cd my/folder

      git checkout
      git fetch --all
      git reset --hard origin/main
      git pull origin main

      # These commands are executing inside ec2
    '

To upload a folder into ec2将文件夹上传到 ec2

- name: Uploading folder to ec2
  run: scp -o StrictHostKeyChecking=no -i private_key -r my-folder ${USER_NAME}@${HOSTNAME}:/home/ubuntu/my-folder

For everyone having this or a similar issue, take a look at this link: https://farhan-tanvir.medium.com/ci-cd-from-github-to-aws-ec2-using-github-action-e18b621c0507 .对于遇到此问题或类似问题的每个人,请查看此链接: https://farhan-tanvir.medium.com/ci-cd-from-github-to-aws-ec2-using-github-action-e18b621c0507 I found it extremely helpful, hope it helps anyone with the problem.我发现它非常有帮助,希望它能帮助任何人解决这个问题。 The yaml file looks like this: yaml 文件如下所示:

name: CI/CD counter app名称:CI/CD 计数器应用

on: push: branches: [ master ] on: push: 分支机构: [ master ]

jobs:职位:

build: runs-on: ubuntu-latest构建:运行:ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Build the docker_compose
  run: docker-compose up -d --build
  
- name: Buid the application
  run: docker-compose exec -T counter_app npm run build

Deploy: needs: build runs-on: ubuntu-latest部署:需要:构建运行:ubuntu-latest

steps:
  - uses: actions/checkout@v2 
  - name: Deploy in EC2
    env:
        PRIVATE_KEY: ${{ secrets.AWS_PRIVATE_KEY  }}
        HOSTNAME : ${{ secrets.HOSTNAME  }}
        USER_NAME : ${{ secrets.USER_NAME  }}
        
    run: |
      echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
      ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} '
      
        #Now we have got the access of EC2 and we will start the deploy .
        cd /home/ubuntu/CounterApp &&
        git checkout master &&
        git fetch --all &&
        git reset --hard origin/master &&
        git pull origin master &&
        docker-compose -f docker-compose.prod.yml up -d --build 
      '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM