OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Pass a JSON variable to a custom GitHub Action

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Guest
I have to pass JSON that is read from a JSON file to a custom action I created. This action takes the text at the moment and simply prints it as seen in the JS code below.

The problem is that I can't pass the JSON as a variable.

JSON:

Code:
[
    {
      "repository": "https://github.com/zed-industries/zed",
      "commit": "fb3ef0d140156511e4880d689a01ae60f5b89fcf"
    },
    {
      "repository": "https://github.com/gdamore/tree-sitter-d",
      "commit": "750dde90ed9cdbd82493bc28478d8ab1976b0e9f"
    },
    {
      "repository": "https://github.com/CodeEditApp/CodeEdit",
      "commit": "051a83737415c71b9d7d0eb9dda751d5232940e6"
    },
    {
      "repository": "https://github.com/zed-industries/zeds",
      "commit": "051a83737415c71b9d7d0eb9dda751d5232940e6"
    }
  ]

JS code of the private repo that is called:

Code:
import { getInput, setFailed } from "@actions/core";

    const list = getInput("list");
    console.log("typeof", typeof list);
    console.log("list", list);
    console.log("length", list.length);


Workflow:

Code:
on:
  workflow_dispatch
name: Check commit
jobs:
  run:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Read repository
        id: read_repo
        run: echo "branchs=$(echo ${cat repository.json})" >> $GITHUB_OUTPUT

      - name: Check repository
        uses: nameRepoPrivate/check_commit@main
        with:
          list: "${{ steps.read_repo.outputs.branchs }}"

Result:

Code:
typeof string
list 
length 0


Other way:

Code:
on:
  workflow_dispatch
name: Check commit
jobs:
  run:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Read repository
        id: read_repo
        run: |
          echo "branchs=$(cat repository.json)" >> $GITHUB_ENV
          echo "::set-output name=branchs::$(cat repository.json)"

      - name: Check repository
        uses: nameRepoPrivate/check_commit@main
        with:
          list: ${{ steps.read_repo.outputs.branchs }}

Result:

Code:
Warning: The `set-output` command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/

..... 

Error: Unable to process file command 'env' successfully.
Error: Invalid format '    {'
...

<p>I have to pass JSON that is read from a JSON file to a custom action I created. This action takes the text at the moment and simply prints it as seen in the JS code below.</p>
<p>The problem is that I can't pass the JSON as a variable.</p>
<p>JSON:</p>
<pre class="lang-json prettyprint-override"><code>[
{
"repository": "https://github.com/zed-industries/zed",
"commit": "fb3ef0d140156511e4880d689a01ae60f5b89fcf"
},
{
"repository": "https://github.com/gdamore/tree-sitter-d",
"commit": "750dde90ed9cdbd82493bc28478d8ab1976b0e9f"
},
{
"repository": "https://github.com/CodeEditApp/CodeEdit",
"commit": "051a83737415c71b9d7d0eb9dda751d5232940e6"
},
{
"repository": "https://github.com/zed-industries/zeds",
"commit": "051a83737415c71b9d7d0eb9dda751d5232940e6"
}
]
</code></pre>
<p>JS code of the private repo that is called:</p>
<pre class="lang-js prettyprint-override"><code>import { getInput, setFailed } from "@actions/core";

const list = getInput("list");
console.log("typeof", typeof list);
console.log("list", list);
console.log("length", list.length);
</code></pre>
<hr />
<p>Workflow:</p>
<pre class="lang-yaml prettyprint-override"><code>on:
workflow_dispatch
name: Check commit
jobs:
run:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Read repository
id: read_repo
run: echo "branchs=$(echo ${cat repository.json})" >> $GITHUB_OUTPUT

- name: Check repository
uses: nameRepoPrivate/check_commit@main
with:
list: "${{ steps.read_repo.outputs.branchs }}"
</code></pre>
<p>Result:</p>
<pre><code>typeof string
list
length 0
</code></pre>
<hr />
<p>Other way:</p>
<pre class="lang-yaml prettyprint-override"><code>on:
workflow_dispatch
name: Check commit
jobs:
run:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Read repository
id: read_repo
run: |
echo "branchs=$(cat repository.json)" >> $GITHUB_ENV
echo "::set-output name=branchs::$(cat repository.json)"

- name: Check repository
uses: nameRepoPrivate/check_commit@main
with:
list: ${{ steps.read_repo.outputs.branchs }}
</code></pre>
<p>Result:</p>
<pre><code>Warning: The `set-output` command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-...precating-save-state-and-set-output-commands/

.....

Error: Unable to process file command 'env' successfully.
Error: Invalid format ' {'
...
</code></pre>
 

Latest posts

M
Replies
0
Views
1
Meliodas Dragon
M
Top