簡體   English   中英

"Github Action 為數組中的每個值創建一個新文件"

[英]Github Action create a new file for each value in an array

我正在嘗試為數組中的每個值創建一個新文件:

這就是我到目前為止所擁有的,它成功地創建了一個基於 GET 的文件並將其推送到我的存儲庫中。 現在我希望它一次為多個文件執行此操作。 如果它是一個數組,我如何讓它遍歷 myRequest 的結果並為每個值創建一個文件,然后最終提交並推送它們?

name: Manual workflow
on:
  workflow_dispatch:
jobs:
  makefiles:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    - uses: actions/checkout@v2
    - name: Getting
      uses: fjogeleit/http-request-action@master
      id: myRequest
      with:
        url: 'https://domain/api/file'
        method: 'GET'
    - name: Show File
      run: echo ${{ steps.myRequest.outputs.response }}
    - name: Create A File
      uses: 1arp/create-a-file-action@0.2
      with:
        path: 'src'
        file: 'foo.bar'
        content: ${{steps.myRequest.outputs.response}}
    - name: final commit
      uses: zwaldowski/git-commit-action@v1
      id: git_commit
    - name: show
      run: echo "${{ steps.git_commit.outputs.sha }}"

如果您的響應是一個數組,您可以使用toJsonfromJSON將其傳遞到下一步。 這個例子應該會有所幫助:

name: build
on: push
jobs:
  job1:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - id: set-matrix
      run: |
        JSON=$(cat ./your.json)
        echo "::set-output name=matrix::${JSON//'%'/'%25'}"

  job2:
    needs: job1
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJSON(needs.job1.outputs.matrix)}}
    steps:
      - run: build

我不知道你的文件的確切格式以及這些文件是如何寫下來的——但只要你能用它們制作 Array,使用toJSON來獲得正確的輸出格式應該很容易。

一種方法是使用jq在 bash 中生成 JSON,或者更好地使您的https://domain/api/file腳本生成 JSON 作為輸出格式 - 最簡單的解決方案恕我直言。

如果您不想直接在matrix中使用它,也可以使用jq讀取它:

job2:
    needs: job1
    runs-on: ubuntu-latest
    steps:
      - run: |
         json={{fromJSON(needs.job1.outputs.matrix)}}
         valueInJSON=`echo $(jq -r '.keyInYourJSON' <<< "$jsonData")`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM