zhaobinglong / myBlog

https://zhaobinglong.github.io/myBlog/
MIT License
7 stars 0 forks source link

web构建自动化之github action #92

Open zhaobinglong opened 3 years ago

zhaobinglong commented 3 years ago

关键概念

(1)workflow (工作流程):持续集成一次运行的过程,就是一个 workflow。

(2)job (任务):一个 workflow 由一个或多个 jobs 构成,含义是一次持续集成的运行,可以完成多个任务。

(3)step(步骤):每个 job 由多个 step 构成,一步步完成。

(4)action (动作):每个 step 可以依次执行一个或多个命令(action)

新建任务

GitHub Actions 的配置文件叫做 workflow 文件,存放在代码仓库的.github/workflows目录。workflow 文件采用 YAML 格式,文件名可以任意取,但是后缀名统一为.yml,比如foo.yml。一个库可以有多个 workflow 文件。GitHub 只要发现.github/workflows目录里面有.yml文件,就会自动运行该文件。

使用开源action

- name: Build and Deploy
  uses: JamesIves/github-pages-deploy-action@master

参考

http://www.ruanyifeng.com/blog/2019/09/getting-started-with-github-actions.html https://github.com/marketplace?type=actions

zhaobinglong commented 3 years ago

基础配置

// 监听master分支的push操作
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

// 定义任一个或者对歌任务,action最重要的就是on和jobs
jobs:
// 可以自定义step名字并且输出到控制台
    steps:
      - uses: actions/checkout@v2
      - name: Run a one-line script
        run: echo Hello, world!
zhaobinglong commented 3 years ago

基于githubAction自动部署vuepress项目到github pages

参考

https://blog.csdn.net/qwe435541908/article/details/103719882

zhaobinglong commented 3 years ago

github action自动部署阿里云ECS

前提:服务器配置了SSH免密登陆

方式一:

定义一个name, 配置其中的 secrets 变量参数,请到对应项目仓库 Settings->Secrets 中设置,

- name: Deploy to Staging server
    uses: easingthemes/ssh-deploy@v2.0.7
      env:
    SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }}
    ARGS: "-avz --delete"
    SOURCE: "public/"
    REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
    REMOTE_USER: ${{ secrets.REMOTE_USER }}
    TARGET: ${{ secrets.REMOTE_TARGET }}

image

方式二:

// 准备一个bash脚本,SSH登陆后,执行脚本中的内容
- name: executing remote ssh commands using password
  uses: appleboy/ssh-action@master
  with:
    host: ${{ secrets.ALIYUN_HOST }}
    username: ${{ secrets.ALIYUN_USER }}
    password: ${{ secrets.ALIYUN_PASSWORD }}
    script: bash /root/del.sh

参考

https://www.cnblogs.com/liuheng/p/12053244.html https://heyuanfei.com/post/%E4%BD%BF%E7%94%A8github-actions%E8%87%AA%E5%8A%A8%E5%8F%91%E5%B8%83hugo-blog%E9%85%8D%E7%BD%AE/

zhaobinglong commented 3 years ago

自动构建部署腾讯云

      # npm install 自动构建
      - name: npm install and build
        run: |
          npm install
          npm run build
        env:
          CI: true

      # Deploy
      # 配置远程登录腾讯云ECS,然后部署到ECS
      # 把这个项目的文件夹发送到目标的地址
      # SERVER_SSH_KEY是服务器的私钥
      # 同时把服务器的公钥添加进入authorized_keys
      - name: Deploy to Teng ECS
        uses: easingthemes/ssh-deploy@v2.1.5
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }}
          ARGS: "-rltgoDzvO --delete"
          SOURCE: "/"
          REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
          REMOTE_USER: ${{ secrets.REMOTE_USER }}
          TARGET: '/home/wwwroot/default/oa'

部署成功但是action提示异常

2020-10-22T07:41:24.5559207Z [SSH] Creating /home/runner/.ssh dir in  /home/runner/work/oa/oa
2020-10-22T07:41:24.5561036Z ✅ [SSH] dir created.
2020-10-22T07:41:24.5562001Z [SSH] Creating /home/runner/.ssh/known_hosts file in  /home/runner/work/oa/oa
2020-10-22T07:41:24.5564401Z ✅ [SSH] file created.
2020-10-22T07:41:24.5567363Z ✅ Ssh key added to `.ssh` dir  /home/runner/.ssh/deploy_key
2020-10-22T07:41:24.5568739Z [Rsync] Starting Rsync Action: /home/runner/work/oa/oa/dist/ to ***@***:***
2020-10-22T07:41:31.1770919Z ⚠️ [Rsync] error:  rsync exited with code 23
2020-10-22T07:41:31.1773617Z ⚠️ [Rsync] stderr:  Warning: Permanently added '***' (ECDSA) to the list of known hosts.
2020-10-22T07:41:31.1775129Z rsync: chgrp "***/." failed: Operation not permitted (1)
2020-10-22T07:41:31.1776683Z rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1196) [sender=3.1.2]
2020-10-22T07:41:31.1777553Z 
2020-10-22T07:41:31.1778526Z ⚠️ [Rsync] stdout:  sending incremental file list

这是因为登陆用户权限的问题,使用root用户,或者给用户root的权限

⚠️ [Rsync] stderr: Warning: Permanently added '***' (ECDSA) to the list of known hosts.

# 编辑服务器上的ssh配置文件,vim ~/.ssh/config
Host *
   StrictHostKeyChecking no
   UserKnownHostsFile /dev/null
   LogLevel ERROR

参考

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