GithubAction对接阿里云ECS实现自动化部署

参考:https://github.com/easingthemes/ssh-deploy

https://frostming.com/2020/04-26/github-actions-deploy/

https://cloud.tencent.com/developer/article/1720500

https://ld246.com/article/1602747692300

对于githubaction 部署到ECS,网上有很多文章,但是可能你看完并没有太多理解,跟我一起 一步一步部署;

1.github action yaml文件

GithubAction对接阿里云ECS实现自动化部署_第1张图片

# This workflow will build a Java project with Gradle
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle

name: Java CI with Gradl

on:
  push:                     #只要push代码 就触发流程
    branches: [ main ]
  pull_request:
    branches: [ main ]     

jobs:                   #任务
  build:

    runs-on: ubuntu-latest      #运行的平台

    steps:                       #步骤
    - uses: actions/checkout@v2          #下载代码。可以访问的github 仓库
    - name: Set up JDK 1.8              #设置环境。可以根据环境取消
      uses: actions/setup-java@v1       #又是一个仓库 我们的运行的命令都是基于被封装的仓库
      with:
        java-version: 1.8              #应该是 使用java版本
    - name: Grant execute permission for gradlew      #-name 自定义执行的名字
      run: chmod +x gradlew            #加权限
    - name: Build with Gradle
      run: ./gradlew build             #执行的代码 给予当前目录
    - name: Deploy to Staging server        #对接阿里云服务
      uses: easingthemes/ssh-[email protected]      #可以访问的仓库,实现的步骤被封装在此仓库
      env:
        SSH_PRIVATE_KEY: ${
     {
      secrets.SERVER_SSH_KEY }}  #这个是阿里云的私钥
        ARGS: "-rltgoDzvO"
        SOURCE: "./"
        REMOTE_HOST: ${
     {
      secrets.REMOTE_HOST }}    #阿里云的 ip
        REMOTE_USER: ${
     {
      secrets.REMOTE_USER }}    #阿里云用户
        TARGET: ${
     {
      secrets.REMOTE_TARGET }}       #被部署的路径 

2.secret添加

GithubAction对接阿里云ECS实现自动化部署_第2张图片

我们只有这样写。才能被CI文件进行引用;

3.服务器配置

  • ${ { secrets.SERVER_SSH_KEY }} :ssh-keygen 生成: 取id_rsa的值填入,一般都在/root/.ssh/下
  • ${ { secrets.REMOTE_USER }}: root
  • ${ { secrets.REMOTE_HOST }}: 阿里云的公网ip
  • ${ { secrets.REMOTE_TARGET }}:自定义站点目录 /root/web

还有一步

cat /root/.ssh/id_rsa_pub >>/root/.ssh/authorized_keys

这样的话。可以github进行免密钥部署服务;

4.push代码进行触发

GithubAction对接阿里云ECS实现自动化部署_第3张图片

[root@k8s-m01 ~]# ls /root/web
10.txt  13.txt  16.txt  2.xtt  6.txt  9.txt         Dockerfile  gradlew.bat      src
11.txt  14.txt  17.txt  3.txt  7.txt  build         gradle      README.md
12.txt  15.txt  1.txt   5.txt  8.txt  build.gradle  gradlew     settings.gradle

你可能感兴趣的:(Github,阿里云,git,github,ci/cd,devops)