jactor-rises
jactor-rises

Reputation: 3091

How to give Github Action the content of a file as input?

I have a workflow with an action that creates a version number when building an artefact. This version number is written to file.

How can I give that as an input to another action?

I.e: How can I use this version number as part of a commit message in another action?

Upvotes: 19

Views: 27730

Answers (4)

rugk
rugk

Reputation: 5513

There is also a GitHub Action that allows you to do this:

- name: Read README.md
  id: package
  uses: jaywcjlove/github-action-read-file@main
  with:
    path: package.json

- name: Echo package.json
  run: echo "${{ steps.package.outputs.content }}"

https://github.com/marketplace/actions/read-file-content

Upvotes: 0

The accepted answer is outdated as per this blog post from GitHub.

It is still possible to do this as one step from your workflow though:

- name: Read VERSION file
  id: getversion
  run: echo "version=$(cat VERSION)" >> $GITHUB_OUTPUT

This will set an output named version which you can access just as before using ${{ steps.getversion.outputs.version }}:

- uses: "marvinpinto/action-automatic-releases@latest"
  with:
    repo_token: "${{ secrets.GITHUB_TOKEN }}"
    automatic_release_tag: v${{ steps.getversion.outputs.version }}.${{ steps.buildnumber.outputs.build_number }}
    prerelease: false

Upvotes: 13

aaronsteers
aaronsteers

Reputation: 2571

Per the fabulous answer here, there's actually an inline way to accomplish this. Not intuitive at all, except that the ::set-output... syntax matches the same expected output format for GitHub Actions.

The below step loads the VERSION file into ${{ steps.getversion.outputs.version }}:

      - name: Read VERSION file
        id: getversion
        run: echo "::set-output name=version::$(cat VERSION)"

I had the same use case as OP, so I'm pasting below my entire code, which does three things:

  1. Pull first three-parts of the 4-part version string from the file VERSION.
  2. Get a sequential build number using the einaregilsson/build-number@v2 action.
  3. Concatenate these two into an always-unique 4-part version string that becomes a new GitHub release.
name: Auto-Tag Release

on:
  push:
    branches:
      - master

jobs:
  release_new_tag:
    runs-on: ubuntu-latest
    steps:
      - name: "Checkout source code"
        uses: "actions/checkout@v1"
      - name: Generate build number
        id: buildnumber
        uses: einaregilsson/build-number@v2
        with:
          token: ${{secrets.github_token}}
      - name: Read VERSION file
        id: getversion
        run: echo "::set-output name=version::$(cat VERSION)"
      - uses: "marvinpinto/action-automatic-releases@latest"
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          automatic_release_tag: v${{ steps.getversion.outputs.version }}.${{ steps.buildnumber.outputs.build_number }}
          prerelease: false

Fully automated release management! :-)

  • Note: The branch filter at top ensures that we only run this on commits to master.

Upvotes: 16

jactor-rises
jactor-rises

Reputation: 3091

It is possible to use the filesystem to communicate between actions. But if you have input on 3rd party actions, you need to give this from the outputs of another action

Ie. you need to read this file in your action and present it as output in your action.yml. Then you can use this output as input to another action in your workflow.yaml

Upvotes: 1

Related Questions