0

I currently have a requirement to set up four jobs in a matrix strategy in GitHub Actions. If the conditions are met, the job will be executed, otherwise, it will be skipped. Is it possible to achieve this?

With the current code, I am only able to skip the steps but not the entire job. Can anyone provide guidance on how to set conditions in the matrix strategy to skip the entire job if the conditions are not met?

jobs:
  deploy:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - uppercase_env: 'DEV'
            lowercase_env: 'dev'
            secrets_prefix: 'dev_'
            deploy_condition: ${{ contains(github.ref_name, 'develop') && !inputs.env_name }}
          - uppercase_env: 'TST'
            lowercase_env: 'tst'
            secrets_prefix: 'tst_'
            deploy_condition: ${{ contains(github.ref_name, 'develop') && !inputs.env_name }}
          - uppercase_env: 'STG'
            lowercase_env: 'stg'
            secrets_prefix: 'stg_'
            deploy_condition: ${{ contains(github.ref_name, 'release') && inputs.env_name == 'STG' }}
          - uppercase_env: 'PRD'
            lowercase_env: 'prd'
            secrets_prefix: 'prd_'
            deploy_condition: ${{ (contains(github.ref_name, 'release') || contains(github.ref_name, 'hotfix')) && inputs.env_name == 'PRD' }}
    steps:
      - name: Do something
        if: matrix.deploy_condition
Lex
  • 59
  • 7
  • Can't you use an `if` on job level, in addition to using a matrix strategy? – Benjamin W. Mar 27 '23 at 04:10
  • Yes, I can use an "if" statement to choose whether to execute a step, but I cannot control whether a job is executed, and this is something that troubles me. I've searched on Google for a long time, and it seems that I cannot use "if" statements in a matrix job. I can only split the job into separate ones. – Lex Mar 27 '23 at 04:51
  • @BenjaminW.Yes, that's what I achieved! I've solved the problem myself, thank you! – Lex Mar 28 '23 at 03:47

1 Answers1

0

I solved this problem myself by using python (of course shellscript is fine) to dynamically generate the json of the matrix to achieve the need for dynamic job generation.

You can refer to the following code.

import json
import sys

def generate_matrix(ref_name, env_name):
    return {
        ("develop", ""): [
            {"uppercase_env": "DEV", "lowercase_env": "dev", "secrets_prefix": "dev_", "deploy_condition": "true"},
            {"uppercase_env": "TST", "lowercase_env": "tst", "secrets_prefix": "tst_", "deploy_condition": "true"},
        ],
        ("release", "STG"): [{"uppercase_env": "STG", "lowercase_env": "stg", "secrets_prefix": "stg_", "deploy_condition": "true"}],
        ("release", "PRD"): [{"uppercase_env": "PRD", "lowercase_env": "prd", "secrets_prefix": "prd_", "deploy_condition": "true"}],
        ("hotfix", "PRD"): [{"uppercase_env": "PRD", "lowercase_env": "prd", "secrets_prefix": "prd_", "deploy_condition": "true"}],
    }.get((ref_name.split('/')[0], env_name), [])

if __name__ == "__main__":
    print(json.dumps({"include": generate_matrix(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else "")}))
Lex
  • 59
  • 7