如何跳过特定日期(例如星期五)的工作流程作业?

问题描述 投票:0回答:1

我在 GitHub Actions 中有一个 CI/CD 管道,当更改推送到

production
分支时,它会部署代码。但是,我希望在周五自动执行某些维护任务(例如清理 Docker、临时文件和执行数据库备份),在此期间不应进行任何部署。因此,我需要跳过周五管道中的部署作业。

这是我当前的部署步骤的 GitHub Actions 工作流程配置:

deploy:
  runs-on: ubuntu-latest
  needs: [pytest] # Deploy only if tests pass
  if: github.ref_name == 'production' && github.event_name == 'push'
  steps:
    - name: Checkout Code
      uses: actions/checkout@v4

    - name: Add SSH Key
      uses: webfactory/[email protected]
      with:
        ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

    - name: Add known hosts
      run: ssh-keyscan -H ${{ secrets.VM_IP }} >> ~/.ssh/known_hosts

    - name: Deploy to Production Server
      run: |
        ssh -o StrictHostKeyChecking=no ${{ secrets.VM_USER }}@${{ secrets.VM_IP }} << 'EOF'
          cd /home/${{ secrets.VM_USER }}/client-portal
          git pull origin production
          git checkout production
          docker compose -f docker-compose.production.yml build
          docker compose -f docker-compose.production.yml down --remove-orphans
          docker compose -f docker-compose.production.yml up -d
        EOF

如何向工作流程添加条件以在周五或每个午夜跳过此部署步骤?我正在寻找一种方法来防止部署作业在星期五运行,同时允许其他日子照常运行。有没有办法检查当前星期几并根据 GitHub Actions 中的条件应用条件?

github-actions
1个回答
0
投票

您可以尝试一种解决方法,例如使用

date %u
$GITHUB_OUTPUT
自己计算日期名称:

name: CI/CD Pipeline

on:
  push:
    branches:
      - production

jobs:
  pytest:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
      - name: Run Tests
        run: pytest

  deploy:
    runs-on: ubuntu-latest
    needs: [pytest]
    if: github.ref_name == 'production' && github.event_name == 'push'
    steps:
      - name: Get Day of Week
        id: get_day
        shell: bash
        run: |
          DAY_OF_WEEK=$(date +'%u')
          echo "day_of_week=$DAY_OF_WEEK" >> $GITHUB_OUTPUT

      - name: Deploy to Production Server
        if: steps.get_day.outputs.day_of_week != '5'
        uses: appleboy/[email protected]
        with:
          host: ${{ secrets.VM_IP }}
          username: ${{ secrets.VM_USER }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            cd /home/${{ secrets.VM_USER }}/client-portal
            git pull origin production
            git checkout production
            docker compose -f docker-compose.production.yml build
            docker compose -f docker-compose.production.yml down --remove-orphans
            docker compose -f docker-compose.production.yml up -d
© www.soinside.com 2019 - 2024. All rights reserved.