【问题标题】:Automatically Create tmp/restart.txt for Dreamhost Flask Application为 Dreamhost Flask 应用程序自动创建 tmp/restart.txt
【发布时间】:2018-10-28 08:42:59
【问题描述】:

我有一个在 Dreamhost 服务器上运行的烧瓶应用程序。我正在使用 PyCharm 的部署工具在进行更改时手动和/或自动更新服务器上的文件。

在 Dreamhost 上运行带有 Passenger 的烧瓶应用程序时,该应用程序被缓存,因此,Passenger 知道刷新缓存的唯一方法是让 tmp/restart.txt 文件具有新的时间戳。手动,这是通过带有touch tmp/restart.txt 的 SSH 从终端完成的。

但我想以某种方式在 PyCharm 中自动完成这项工作。有没有办法做到这一点?我什么都找不到。

我看到有人建议他们可以通过 github 执行此操作:“对于我的项目,我在我的 git repo 中添加了一个 post-receive 挂钩来为我修改文件。”坦率地说,我真的不知道这意味着什么,但我认为这意味着如果我部署到 github 然后以某种方式从那里同步到 Dreamhost 服务器,这将是“简单的”。

但这也表明在 PyCharm 中可能有一种方法可以做到这一点。有什么想法吗?

【问题讨论】:

    标签: flask pycharm passenger dreamhost


    【解决方案1】:

    这是a professional feature on Pycharm 的一部分。

    此外,学习通过 Github Actions(或 Bitbucket Pipelines 或 CircleCI 等工具)进行部署将有很多未来的好处,因为您可以自动执行复杂的部署(如果您愿意,还可以进行测试)。

    那么让我分享一下如何使用 Github Actions 将 Flask 应用程序部署到 Dreamhost。

    首先在项目的根文件夹中创建一个名为 .github/workflows/main.yml 的文件

    在此文件中,我们将指定使用 rsync 将更新的代码文件从您的 github 存储库部署到您的 dreamhost 服务器的说明。 Github 将生成一个临时的 ubuntu 实例来运行这些指令。我们还将在激活虚拟环境后运行一些部署后的步骤,例如运行 pip 和从requirements.txt 安装依赖项,并触摸 restart.txt 文件。

    name: ? Deploy to Dreamhost on Push
    
    # Controls when the workflow will run
    on:
      # Triggers the workflow on push events and only for the master branch
      push:
        branches: [ master ]
    
      # 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 two jobs
      rsync-deploy:
        name: ?️ Rsync with Dreamhost
        # 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:
        # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
        - name: ? Get Latest Code
          uses: actions/checkout@v2
    
        # Send files to webserver via rsync
        - name: ? Rsync files
    #      uses: Pendect/action-rsyncer@v1.1.0
          uses: burnett01/rsync-deployments@5.1
          with:
            switches: -avzr --delete --exclude='venv/' --exclude='.git/' --exclude='.github/'
            path: /
            remote_path: /home/yourdreamhostusername/yourwebsite.com/
            remote_host: yourwebsite.com
            remote_user: yourdreamhostusername
            remote_key: ${{ secrets.DREAMHOST_SSH_KEY }}
    
        # Install any new dependencies from requirements.txt
        - name: ? Install Python Dependencies
          uses: appleboy/ssh-action@master
          with:
            host: yourwebsite.com
            username: yourdreamhostusername
            key: ${{ secrets.DREAMHOST_SSH_KEY }}
            port: 22
            script: |
              cd /home/yourdreamhostusername/yourwebsite.com
              source venv/bin/activate
              pip3 install -r app/requirements.txt
              touch /home/yourdreamhostusername/yourwebsite.com/tmp/restart.txt
    

    适当地替换 yourwebsite.comyourdreamhostusername,您还必须通过 SSH 登录并创建虚拟环境(这里假设您之前已通过 SSH 登录并在 Dreamhost 帐户的文件夹 /home/yourdreamhostusername/yourwebsite.com/venv/ 中创建了 venv) .最后,您还需要创建一个 SSH 密钥(公钥和私钥对)。将私钥上传到.ssh文件夹中的dreamhost,将私钥上传到Github secrets为DREAMHOST_SSH_KEY

    【讨论】:

      猜你喜欢
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多