Github Actions为Lambda函数构建矩阵

时间:2020-08-21 06:41:17

标签: github-actions

寻找有关为文件夹中的每个lambda函数启动一组并行Github操作的最佳方法的建议。文件夹结构如下:

lambdas/example1/index.js
lambdas/example2/index.js

....

然后将它们传递到此矩阵设置

  deploy_source:
name: Deploy Lambda From Source
runs-on: ubuntu-latest
strategy:
  matrix:
    lambdafile:['example1/index.js','example12/index.js',....]
steps:
  - name: checkout source code
    uses: actions/checkout@v1
  - name: default deploy
    uses: appleboy/lambda-action@master
    with:
      aws_access_key_id: '123123123123'
      aws_secret_access_key: '123123123123'
      aws_region: '123123123123'
      function_name: gorush
      source: ${{ matrix.lambdafile }}

1 个答案:

答案 0 :(得分:0)

您可以创建job1来读取文件夹并根据该数据动态创建矩阵数组。然后创建第二项作业以收集动态矩阵并使用它。

以下是lambda目录中每个文件的动态文件矩阵数组的示例工作流程

 name: build
 on: push
 jobs:
   job1:
     runs-on: ubuntu-latest
     outputs:
        matrix: ${{ steps.setmatrix.outputs.matrix }}
     steps:
     - name: checkout source code
       uses: actions/checkout@v1
     - id: setmatrix
       run: |
         matrixArray=$(find ./lambdas -name '*.js') # Creates array of all files .js withing lambdas
         # Start Generate Json String
         echo "$matrixArray" | \
         jq --slurp --raw-input 'split("\n")[:-1]' | \
         jq  "{\"filepath\": .[] }" | \
         jq -sc "{ \"include\": . }" > tmp
         cat ./tmp
         # End Generate Json String
         matrixStringifiedObject=$(cat ./tmp) # Use this as jq @sh wasn't cooperating
         echo "::set-output name=matrix::$matrixStringifiedObject"
   job2:
     needs: job1
     runs-on: ubuntu-latest
     strategy:
       matrix: ${{fromJson(needs.job1.outputs.matrix)}}
     steps:
      - name: checkout source code
        uses: actions/checkout@v1
      - run: echo ${{ matrix.filepath }}

Sample workflow run

Pipeline code repo