默认分类
只是一个默认分类

Github Action 打包 Docker 镜像&自动部署

根据 git tags 触发部署,用版本号生成镜像,上传至腾讯云私有镜像仓库。
服务器拉取最新版本部署上线。

name: Docker Build And Push
on:
  push:
    tags:
      - 'v*'

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Get version
        id: version
        run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}

      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          registry: ccr.ccs.tencentyun.com
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: ${{ secrets.REGISTRY }}:latest,${{ secrets.REGISTRY }}:${{ steps.version.outputs.VERSION }}
      -
        name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}

  pull-docker:
    needs: [ docker ]
    name: Pull Docker
    runs-on: ubuntu-latest
    steps:
      - name: Deploy
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          key: ${{ secrets.KEY }}
          script: |
            docker stop $(docker ps --filter ancestor=${{ secrets.REGISTRY }} -q)
            docker rm -f $(docker ps -a --filter ancestor=${{ secrets.REGISTRY }}:latest -q)
            docker rmi -f $(docker images  ${{ secrets.REGISTRY }}:latest -q)
            docker login --username=${{ secrets.DOCKERHUB_USERNAME }} --password ${{ secrets.DOCKERHUB_TOKEN }} ccr.ccs.tencentyun.com
            docker pull ${{ secrets.REGISTRY }}:latest
            docker run -d -p 8060:8060 ${{ secrets.REGISTRY }}:latest

Github Action 前后端项目模板

android-github-actions-setup-image-35b6a79fea4a7289acb6796cd4ad05b4.png
GitHub Actions 用起来非常简单,只要在你的仓库根目录建立.github/workflows文件夹,将你的工作流配置(YAML 文件)放到这个目录下,就能启用 GitHub Actions 服务

前端项目部署到服务器

name: Deploy
on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@master

      - name: use Node.js 14
        uses: actions/setup-node@v1
        with:
          node-version: 14

      - name: npm ci and build
        run: |
          npm ci
          npm run build
        env:
          NODE_ENV: development
          CI: true

      - name: Prepare SSH
        run: >
          cd ~ && mkdir .ssh &&
          touch ~/.ssh/known_hosts &&
          ssh-keyscan -H "$IP" >>~/.ssh/known_hosts
        env:
          IP: ${{ secrets.HOST }}

      - name: Deploy to Server
        uses: easingthemes/ssh-deploy@v2.1.6
        env:
          REMOTE_HOST: ${{ secrets.HOST }}
          SSH_PRIVATE_KEY: ${{ secrets.TOKEN }}
          ARGS: "-avzr --delete"
          SOURCE: "build/"
          REMOTE_USER: "ubuntu"
          TARGET: "/var/www/scholar.run"
          

后端 Docker 镜像

GitHub Actions Docker镜像编译和推送到云镜像仓储

Github:https://github.com/risfeng/docker-image-build-push-action

Github Marketplace:https://github.com/marketplace/actions/docker-images-build-and-push

name: Docker Build And Push To Aliyun Hub

on:
  push:
    branches:
      - master

jobs:
  build:
    name: Build Spring Boot
    runs-on: ubuntu-latest
    steps:
      - name: Git Checkout Code
        uses: actions/checkout@v1
        id: git_checkout

      - name: Set up JDK 12.0
        uses: actions/setup-java@v1
        with:
          java-version: 12.0

      - name: Build with Gradle
        run: ./gradlew build

      - name: Build Docker Image
        id: buildAndPushImage
        uses: risfeng/docker-image-build-push-action@v1.0
        with:
          registry_url: 'registry.us-east-1.aliyuncs.com'
          namespaces: 'adotcode'
          repository_name: 'adc.ms.eureka.usa'
          user_name: ${{ secrets.ALIYUN_IMAGES_HUB_USER_NAME }}
          password: ${{ secrets.ALIYUN_IMAGES_HUB_TOKEN }}
          image_version: 'v1.0'
          docker_file: '.'
      - name: Get pre step result output image_pull_url
        run: echo "The time was ${{ steps.buildAndPushImage.outputs.image_pull_url }}"

其中 registry_url、namespaces、repository_name、user_name、password 为自己云镜像仓库设置,${{ secrets.ALIYUN_IMAGES_HUB_USER_NAME }} ${{ secrets.ALIYUN_IMAGES_HUB_TOKEN }}是调用Github仓库settings配置的云镜像仓库的登录用户名和密码,防止密码硬编码被泄漏,配置路径:Github代码仓库-->[settings]->[Secrets]中添加对应的Key。

输入参数说明

以下参数均可参见云私有镜像仓库,如:阿里云私有镜像仓库

参数是否必传描述
registry_url仓库地址,eg: registry.us-east-1.aliyuncs.com
namespaces命名空间
repository_name镜像仓库名称
user_name云登录账户
password登录个人容器镜像服务设置
image_version生成镜像的版本,可以写死,也可以通过上下文自行动态赋值
docker_file构建镜像的Dockerfile目录,默认当前目录(.)

输出参数说明

脚本执行完成后会输出镜像pull地址,便于后续直接docker pull 使用

参数是否必传描述
image_pull_url镜像上传成功后返回的pull地址,eg: registry.us-east-1.aliyuncs.com/ns/adc.ms.erika:v1.0 使用示例: docker pull ${{ steps.<steps.id>.outputs.image_pull_url }}

参考文章

https://frostming.com/2020/04-26/github-actions-deploy/#%E4%BD%BF%E7%94%A8-github-actions-%E8%87%AA%E5%8A%A8%E5%8C%96
https://github.com/risfeng/docker-image-build-push-action

心流

人家说
把心打开
爱才会流进来
但为何
打开之后
留下了道疤呀
我在想
下次应该
再多打开些吧
阳光下
那晶莹的
是眼底的浪花

2021.1.5 凌晨

📺 使用 Node 实现命令行调用播放器看斗鱼&虎牙直播

仓库地址
https://github.com/zhangchen915/live-url

斗鱼

斗鱼的解析

async function getUrl(roomId = 3484) {
    try    {
        const res = await fetch(`https://www.douyu.com/${roomId}`)
        const body = await res.text();
        const id =body.match(/live\/(\S*)_/)[1]
        return `http://tx2play1.douyucdn.cn/live/${id}.flv?uuid=`
    } catch (e) {
    }
}

调起 PotPlayer

PotPlayer播放器所在路径通常如 C:\\Program Files\\DAUM\\PotPlayer\\PotPlayerMini64.exe,通过 exe 加入真实直播 url 参数即可播放直播。

const player = cp.spawn(PotPlayer, [url],{
        detached: true,
        stdio: 'ignore'
    });
    player.unref();