如何 git 拉取远程服务器,然后使用 git 集线器操作重新创建 env 文件

How to git pull at remote server and then recreate the env file using github actions

我想要触发工作流分派GitHub 将

  1. ssh 进入特定的 digitalocean droplet
  2. 通过 运行ning git pull
  3. 部署代码
  4. 然后根据 GITHUB 存储库中的秘密重新创建 .env 文件

我认为 1 和 3 是最难的部分,所以我专注于它们。

两个硬性要求是:

A) 我想避免的是编写任何秘密并将它们直接提交到存储库。

B) 必须为私人 GitHub org/repo 或 user/repo

工作

这是我当前的工作流文件

# This is a basic workflow to help you get started with Actions

name: digitalocean-pull-and-setup-env

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the $default-branch branch
  # push:
  #   branches: [ $default-branch ]
  # pull_request:
  #   branches: [ $default-branch ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # SSH into digitalocean droplet
      - name: executing remote ssh commands using password
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_KEY }}


      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.

      # create the env file
      - name: Make envfile
        uses: SpicyPizza/create-envfile@v1
        with:
          DEBUG: False
          DEVELOPMENT_MODE: False
          DJANGO_SECRET_KEY: ${{ secrets.DJANGO_SECRET }}
          directory: /home/django/all_django_projects
          file_name: .django

它运行成功没有错误。但我没有看到在 DigitalOcean Droplet 中创建的 .django env 文件。

我认为原因是因为 GitHub 操作 运行 这些步骤在 运行ning 容器中作为单独的步骤。

如果有更好的解决方案可以实现我的最终目标,即使用从 GitHub 存储库中的秘密创建的 .env 文件部署到 digitalocean,我不介意放弃我的 3 步算法。

我也找到了 actions/checkoutdigitalocean/action-doctl https://github.com/actions/checkout and https://github.com/digitalocean/action-doctl 但坦率地说,它们让我感到困惑,所以我没有使用它们。

甚至不确定它是否符合我的要求。

我想要的只是一种简单的方法,可以在已经 运行ning 的 digitalocean droplet 上下载最新的更改,并在我重新启动服务器之前重新创建环境文件。

还有这个 ,它似乎将整个 repo 从 GITHUB_WORKSPACE 上传到远程服务器,我认为与简单地在服务器上拉下更改相比,这太过分了。

我找到的最接近的可能是这个 ,这似乎表明我不能将动作嵌套在彼此之间,除此之外我还需要编写一个脚本。

我真的必须在 ssh 操作中编写脚本,或者我可以以某种方式在 ssh 操作中重用 create-envfile 操作吗?

我可以想到几种方法:

  1. 如您所述,用您自己的脚本替换 create-envfile 操作调用以将环境变量转储到 .env 文件并将其作为参数添加到您的 appleboy/ssh-action 调用步骤。我不认为这是一个主要负担 - 你可以从 here.
  2. 复制大部分代码
  3. 保留 SpicyPizza/create-envfile 操作步骤,但将输出路径修改为 GitHub 操作运行程序中的某个位置。然后,scp 将您刚刚创建的 env 文件添加到您的 Droplet 中。您可以使用自己的后续 run 步骤执行此操作,也可以使用此 scp GitHub Action。单独步骤的文件更改保留在运行器工作区中,直到作业结束,后续步骤会在运行器的文件系统上看到先前步骤的更改。

我意识到我应该根据硬性要求更明确地充实整个目标场景。

目标场景:如何从GitHub部署到UbuntuVPS

硬性要求

必须使用:

  • Github 操作数
  • Ubuntu LTS VPS(我用的是 DigitalOcean)

必看:

  • VPS 上的代码库更新到最新的 main 分支
  • 在 VPS
  • 上的项目路径中有一个新的 .env 文件

方法

我发现了 2 种通用方法。

方法一、多动作+rsync方式

大纲:

  1. 签出操作然后 rsync 以更新代码库
  2. create-envfile 然后 rsync

此方法混合使用 GitHub 操作。即,checkout and create-envfile

方法 2. 使用 bash 脚本 + git 克隆方法的 ssh-action

  1. ssh-action 然后
    1. git 使用 bash 脚本克隆以更新代码库
    2. 使用 bash 脚本创建 envfile

此方法使用 1 个 GitHub 操作,但需要更多 bash 脚本

我选择了方法 2,因为 git clone 优于 rsync。

这是我的ci.yml

name: Deploy via SSH
on: [workflow_dispatch]
jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    steps:
    - name: Deploy via SSH
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.SSH_HOST }}
        username: ${{ secrets.SSH_USERNAME }}
        key: ${{ secrets.SSH_KEY }}
        script: |
          # git update to latest codebase on main branch
          cd /path/to/project && git pull
          # updating the .env file
          cat <<EOF > ${{ secrets.ENVFILE_PATH }}
          # start of envfile
          DJANGO_SECRET_KEY=${{ secrets.ENVFILE_DJANGO_SECRET_KEY }}
          # end of envfile
          EOF
          # see end result of envfile
          ls -l ${{ secrets.ENVFILE_PATH }}

至于如何将机密保存到您的 GitHub 存储库以供 SSH 访问,请查看此 article