GitHub Action Runner: 在同一台服务器上部署2个环境(开发和生产)

huangapple go评论76阅读模式
英文:

GitHub Action Runner: deploy 2 environments (dev and prod) on a same server

问题

I have a webserver and I want to deploy 2 environments: dev and prod on it. Don't understand how GitHub Actions works.

I have succeeded in deploying one environment (prod) but not both. I have 2 branches: one for dev (it's called "dev") and one for prod (it's called "main").

How do I suppose to do? Do I have to make a YAML file for each environment on their respective branches or do I have to write all deployments in the same file? How can I specify an environment to deploy a specific branch of my repository? On my webserver, do I have to deploy 2 services runner applications or just 1 for both environments? How can I have both branches in the _work directory?

This is my main.yaml file (runner) for only the prod environment (it works):

name: Deployment

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

permissions:
  contents: read

jobs:
  build:
    runs-on: [self-hosted, linux]
    
    environment:
      name: myprod-environment
      url: https://www.mywebsite.com

    steps:
    - uses: actions/checkout@v3

    - name: Validate composer.json and composer.lock
      run: composer validate --strict

    - name: Cache Composer packages
      id: composer-cache
      uses: actions/cache@v3
      with:
        path: vendor
        key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
        restore-keys: |
                    ${{ runner.os }}-php-
    - name: Install dependencies
      run: composer install --prefer-dist --no-progress

I've tried to write a dev.yml file in the .github/workflows directory, but I can't specify for my GitHub Action runner service to deploy this environment instead of main.yml.

I've also tried to add a new section in the jobs of main.yml (similar to "build") for dev deployment, but even if I change the branch and force the environment to use a branch, it doesn't deploy both jobs to the _work directory.

英文:

I have a webserver and I want to deploy 2 environment: dev and prod on it. Don't understand how GitHub Actions works.

I have succeeded to deploy one environment (prod) but not both. I have 2 branches: one for the dev (it called "dev") and one for the prod (it called "main").

How do I suppose to do? Do I have to make a YAML file for each environment on their respective branches or do I have to write all deployments on the same file? How can I say for an environment to deploy a specific branch of my repository? On my webserver, do I have to deploy 2 services runner application or just 1 for both environment? How can I suppose to have both branches on _work directory?

This is my main.yaml file (runner) for only prod environment (it works):

name: Deployment

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

permissions:
  contents: read

jobs:
  build:
    runs-on: [self-hosted, linux]
    
    environment:
      name: myprod-environment
      url: https://www.mywebsite.com

    steps:
    - uses: actions/checkout@v3

    - name: Validate composer.json and composer.lock
      run: composer validate --strict

    - name: Cache Composer packages
      id: composer-cache
      uses: actions/cache@v3
      with:
        path: vendor
        key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
        restore-keys: |
          ${{ runner.os }}-php-
    - name: Install dependencies
      run: composer install --prefer-dist --no-progress

I've tried to write a dev.yml on .github/workflows directory but I can't say for my github action runner service to deploy this environnment instead of main.yml.

I've also tried to add a new section on jobs into main.yml (like "build") but for dev deployment but even if I change the branch and I force the environnment to use a branch, it doesn't deploy both of jobs on _work directory.

答案1

得分: 1

Sure, here's the translated content:

这看起来像是一个矩阵工作的任务

> 使用 jobs.<job_id>.strategy.matrix 来定义不同作业配置的矩阵。
在矩阵内部,定义一个或多个变量,然后跟上一个值数组。

但在矩阵中使用非原始值是不支持的。
但是,您可以通过作业输出和一个单独的作业来实现相同的目标。

这意味着使用一个处理两个环境的单个YAML文件:

name: 部署

on:
  push:
    branches:
      - main
      - dev

jobs:
  构建:
    runs-on: [自托管, linux]

    steps:
    - name: 检出仓库
      uses: actions/checkout@v3

    - name: 打印当前分支
      run: echo "当前分支: ${{ github.ref }}"

    - name: 根据分支设置部署路径和环境
      run: |
        if [ "${{ github.ref }}" == "refs/heads/main" ]; then
          echo "DEPLOY_PATH=/var/www/mywebsite/prod" >> $GITHUB_ENV
          echo "ENV_NAME=myprod-environment" >> $GITHUB_ENV
          echo "ENV_URL=https://www.mywebsite.com" >> $GITHUB_ENV
        elif [ "${{ github.ref }}" == "refs/heads/dev" ]; then
          echo "DEPLOY_PATH=/var/www/mywebsite/dev" >> $GITHUB_ENV
          echo "ENV_NAME=mydev-environment" >> $GITHUB_ENV
          echo "ENV_URL=https://dev.mywebsite.com" >> $GITHUB_ENV
        fi        

    - name: 配置环境
      echo "环境:" ${{ env.ENV_NAME }}

    - name: 验证composer.json和composer.lock
      run: composer validate --strict

    - name: 缓存Composer包
      id: composer-cache
      uses: actions/cache@v3
      with:
        path: vendor
        key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
        restore-keys: |
                    ${{ runner.os }}-php-

    - name: 安装依赖
      run: composer install --prefer-dist --no-progress

    - name: 将代码部署到服务器
      run: |
        sudo chown -R your-user:your-group .
        sudo rsync -av --delete --exclude '.git' . ${{ env.DEPLOY_PATH }}        

这将创建一个单一的构建作业。"设置基于分支的部署路径和环境" 步骤根据当前分支设置了 DEPLOY_PATHENV_NAMEENV_URL 环境变量。

这意味着您只需要在您的Web服务器上运行一个自托管的运行器。
当工作流程运行时,它将根据配置为每个环境创建单独的作业,并根据配置部署各自的分支。

但这也假设您的服务器(Apache?, NGiNX?, ...)需要配置以正确提供两个环境,通常包括:

  • 两个文件夹(/var/www/mywebsite/prod/var/www/mywebsite/dev
  • 两个虚拟主机,一个用于每个文件夹

这就是为什么工作流程在 "部署代码到服务器" 步骤之后有一个将文件复制到服务器上适当目录的步骤。
然后在 "部署代码到服务器" 步骤中使用了 DEPLOY_PATH 变量。

不要忘记在 "部署代码到服务器" 步骤中用您服务器的适当用户和组替换 your-useryour-group

英文:

That looks like a job for matrices

> Use jobs.<job_id>.strategy.matrix to define a matrix of different job configurations.
Within your matrix, define one or more variables followed by an array of values

But using non-primitive values in the matrix is not supported.
However, you can achieve the same goal using job outputs and a separate job for each environment.

That means using a single YAML file that handles both environments:

name: Deployment

on:
  push:
    branches:
      - main
      - dev

jobs:
  build:
    runs-on: [self-hosted, linux]

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Print current branch
      run: echo "Current branch: ${{ github.ref }}"

    - name: Set deploy path and environment based on branch
      run: |
        if [ "${{ github.ref }}" == "refs/heads/main" ]; then
          echo "DEPLOY_PATH=/var/www/mywebsite/prod" >> $GITHUB_ENV
          echo "ENV_NAME=myprod-environment" >> $GITHUB_ENV
          echo "ENV_URL=https://www.mywebsite.com" >> $GITHUB_ENV
        elif [ "${{ github.ref }}" == "refs/heads/dev" ]; then
          echo "DEPLOY_PATH=/var/www/mywebsite/dev" >> $GITHUB_ENV
          echo "ENV_NAME=mydev-environment" >> $GITHUB_ENV
          echo "ENV_URL=https://dev.mywebsite.com" >> $GITHUB_ENV
        fi        

    - name: Configure environment
      echo "Environment:" ${{ env.ENV_NAME }}

    - name: Validate composer.json and composer.lock
      run: composer validate --strict

    - name: Cache Composer packages
      id: composer-cache
      uses: actions/cache@v3
      with:
        path: vendor
        key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
        restore-keys: |
                    ${{ runner.os }}-php-

    - name: Install dependencies
      run: composer install --prefer-dist --no-progress

    - name: Deploy code to server
      run: |
        sudo chown -R your-user:your-group .
        sudo rsync -av --delete --exclude '.git' . ${{ env.DEPLOY_PATH }}        

That creates a single build job. The "Set deploy path and environment based on branch" step sets the DEPLOY_PATH, ENV_NAME, and ENV_URL environment variables based on the current branch.

That means you only need a single self-hosted runner on your web server.
When the workflow runs, it will create separate jobs for each environment and deploy the respective branches based on the configuration.

But that also supposes your server (Apache?, NGiNX?, ...) needs to be configured to serve both environments correctly, typically with:

  • two folders (/var/www/mywebsite/prod, /var/www/mywebsite/dev)
  • two virtual hosts, one for each folder

That is why the workflow has a DEPLOY_PATH env variable, and a step to copy the files to the appropriate directory on your server after the "Install dependencies" step.
The DEPLOY_PATH variable is then used in the "Deploy code to server" step.

Don't forget to replace your-user and your-group in the "Deploy code to server" step with the appropriate user and group for your server.

huangapple
  • 本文由 发表于 2023年5月13日 15:54:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定