【问题标题】:Github Actions conditional statement for job not working工作不工作的 Github Actions 条件语句
【发布时间】:2021-09-10 20:50:50
【问题描述】:

我正在尝试在 Github 操作上创建 workflow_dispatch 管道,我希望在某些情况下执行一些作业,但我无法实现。

这是我的完整 yaml 文件:

name: AppleDistribution

on:
  workflow_dispatch:
    inputs:
      target_name_input:
        description: "The name of the target => WhiteLabel"
        required: true
      version_number_input:
        description: "The version number for the release => X.Y"
        required: true
      build_number_input: 
        description: "The build number for the release => Z"
        required: true
      should_run_test_input:
        description: "Whether or not TESTS (Unit/UI) will be run => true/false"
        required: true
        default: "true"
      test_devices_input:
        description: "Coma separated list of simulators to run the tests => iPhone 12 Pro,iPhone 8"
        required: true
        default: "iPhone 12 Pro"
      should_deploy_input:
        description: "Whether or not the product will be deployed to AppStoreConnect => true/false"
        required: true
        default: "true"
      deploy_type_input:
        description: "The type of deploy: appstore/adhoc"
        required: true
        default: "appstore"
      build_test_info_input:
        description: "Description of what's new"
        required: false
        default: "Bug fixes"

env:
  TARGET: ${{ github.event.inputs.target_name_input }}
  VERSION_NUMBER: ${{ github.event.inputs.version_number_input }}
  BUILD_NUMBER: ${{ github.event.inputs.build_number_input }}
  SHOULD_RUN_TEST: ${{ github.event.inputs.should_run_test_input }}
  TEST_DEVICES: ${{ github.event.inputs.test_devices_input }}
  SHOULD_DEPLOY: ${{ github.event.inputs.should_deploy_input }}
  DEPLOY_TYPE: ${{ github.event.inputs.deploy_type_input }}
  BUILD_TEST_INFO: ${{ github.event.inputs.build_test_info_input }}
  CI_APPLE_USER: ${{ secrets.CI_APPLE_USER }}
  CI_APPLE_PASSWORD: ${{ secrets.CI_APPLE_PASSWORD }}
  CI_APPLE_APP_PASSWORD: ${{ secrets.CI_APPLE_APP_PASSWORD }}
  CI_MATCH_PASSWORD: ${{ secrets.CI_MATCH_PASSWORD }}
  S3_MATCH_BUCKET: ${{ secrets.S3_MATCH_BUCKET }}
  S3_MATCH_REGION: ${{ secrets.S3_MATCH_REGION }}
  S3_MATCH_ACCESS_KEY: ${{ secrets.S3_MATCH_ACCESS_KEY }}
  S3_MATCH_SECRET_ACCESS_KEY: ${{ secrets.S3_MATCH_SECRET_ACCESS_KEY }}

jobs:
  welcome:
    name: Welcome
    runs-on: macos-11
    steps:
      - name: Log inputs and variables
        run: |
          echo "SYSTEM:"
          sw_vers
          uname -m
          echo -e "\nXCODE VERSION"
          /usr/bin/xcodebuild -version
          echo -e "\nINPUT ARGUMENTS:"
          echo -e "\tTARGET: $TARGET\n\tVERSION_NUMBER: $VERSION_NUMBER\n\tBUILD_NUMBER: $BUILD_NUMBER\n\tSHOULD_RUN_TEST: $SHOULD_RUN_TEST\n\tTEST_DEVICES: $TEST_DEVICES\n\tSHOULD_DEPLOY: $SHOULD_DEPLOY\n\tDEPLOY_TYPE: $DEPLOY_TYPE\n\tBUILD_TEST_INFO: $BUILD_TEST_INFO"
          echo -e "\nSECRET ARGUMENTS:"
          echo -e "\tCI_APPLE_USER: $CI_APPLE_USER\n\tCI_APPLE_PASSWORD: $CI_APPLE_PASSWORD\n\tCI_APPLE_APP_PASSWORD$CI_APPLE_APP_PASSWORD\n\tCI_MATCH_PASSWORD: $CI_MATCH_PASSWORD\n\tS3_MATCH_BUCKET: $S3_MATCH_BUCKET\n\tS3_MATCH_REGION: $S3_MATCH_REGION\n\tS3_MATCH_ACCESS_KEY: $S3_MATCH_ACCESS_KEY\n\tS3_MATCH_SECRET_ACCESS_KEY: $S3_MATCH_SECRET_ACCESS_KEY"
          sleep 1
  installer:
    name: Dependencies Installer
    needs: welcome
    runs-on: macos-11
    steps:
      - name: Git LFS
        run: |
          if brew ls --versions git-lfs; then brew upgrade git-lfs; else brew install git-lfs; fi
      - name: Fastlane
        run: |
          if brew ls --versions fastlane; then brew upgrade fastlane; else brew install fastlane; fi
  tests:
    name: Test
    needs: installer
    runs-on: macos-11
    steps:
      - uses: actions/checkout@v2
      - name: Install Bundle
        run: |
          bundle install
          bundle update fastlane
      - name: Clean up and install dependencies
        run: |
          echo "Hello"
      - name: CIM Tests
        run: |
          cd CIManager
          ruby cim_manager.rb test  \
          --target=$TARGET \
          --devices="$TEST_DEVICES"
  deploy:
    name: Deployment
    if: ${{ github.inputs.should_deploy_input == 'true' }} # Also tried github.env.SHOULD_DEPLOY == 'true'
    needs: tests
    runs-on: macos-11
    steps:
      - uses: actions/checkout@v2
      - name: Install Bundle
        run: |
          bundle install
          bundle update fastlane
      - name: Clean up and install dependencies
        run: |
          rm -rf ~/Library/Developer/Xcode/DerivedData
          echo "Rebuilding Cocoapods"
          rm -rf Pods
          rm Podfile.lock
          pod repo update
          pod install
          echo "Rebuilding Carthage"
          rm Cartfile.resolved
          rm -rf Carthage
          carthage update --use-xcframeworks
      - name: CIM Deployment
        run: |
          cd CIManager
          ruby cim_manager.rb deploy \
          --target=$TARGET \
          --version=$VERSION_NUMBER \
          --build=$BUILD_NUMBER \
          --configuration=Release \
          --deploy_type=$DEPLOY_TYPE \
          --user_name=$CI_APPLE_USER \
          --user_password=CI_APPLE_PASSWORD \
          --match_password=$CI_MATCH_PASSWORD \
          --s3_match_bucket=$S3_MATCH_BUCKET \
          --s3_match_region=$S3_MATCH_REGION \
          --s3_match_access_key=$S3_MATCH_ACCESS_KEY \
          --s3_match_secret_access_key=$S3_MATCH_SECRET_ACCESS_KEY \
          --build_test_info="$BUILD_TEST_INFO"

基本上我希望能够在用户将 should_deploy_input 设置为 true 时执行 deploy,否则忽略它。

有什么想法吗?

【问题讨论】:

    标签: yaml github-actions


    【解决方案1】:

    这不起作用,因为您在使用 github.inputs.should_deploy_input 时忘记了 event 上的单词 if condition

    正确的语法是github.event.inputs.should_deploy_input

    我刚刚使用此工作流程对其进行了测试:

    name: Deployment
    
    on:
      workflow_dispatch:
        inputs:
          should_deploy_input:
            description: "Whether or not the product will be deployed to AppStoreConnect => true/false"
            required: true
            default: "true"
    
    jobs:
      tests:
        runs-on: macos-latest
        steps:
          - run: echo Hello World
    
      deploy:
        name: Deployment
        runs-on: macos-latest
        if: ${{ github.event.inputs.should_deploy_input == 'true' }} # Also tried github.env.SHOULD_DEPLOY == 'true'
        needs: tests
        steps:
          - name: get the source code
            uses: actions/checkout@v2
          - run: echo Deployment Job
    

    可以查看工作流运行输出here

    【讨论】:

    • 太棒了!我以为我正确阅读了文档...显然我错过了...对于全局环境变量是否相同?
    • 我不确定我是否理解你想用 env 变量做什么。但如果它在您的工作流程中使用if expression 执行操作或不使用,答案是肯定的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2021-02-10
    • 1970-01-01
    • 2022-11-08
    • 2016-03-22
    • 2023-01-11
    • 2015-01-05
    相关资源
    最近更新 更多