Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve go version from go.mod file #174

Closed
pantelis-karamolegkos opened this issue Dec 13, 2021 · 7 comments
Closed

Retrieve go version from go.mod file #174

pantelis-karamolegkos opened this issue Dec 13, 2021 · 7 comments
Labels
feature request New feature or request to improve the current logic

Comments

@pantelis-karamolegkos
Copy link

Description:
Instead of hard-coding the go version, the action could (optionally)

  • search in the root of the project (or in any other configurable place) for a go.mod file
  • extract and use the go version specified there.

Justification:
Justification or a use case for your proposal.

Are you willing to submit a PR?

@pantelis-karamolegkos pantelis-karamolegkos added feature request New feature or request to improve the current logic needs triage labels Dec 13, 2021
@TMaYaD
Copy link

TMaYaD commented Feb 10, 2022

FWIW, this is the workaround I use:

      - run: sed -En 's/^go[[:space:]]+([[:digit:].]+)$/GO_VERSION=\1/p' go.mod >> $GITHUB_ENV
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GO_VERSION }}

@thetillhoff
Copy link

thetillhoff commented Apr 3, 2022

Thanks @TMaYaD, that helped me a lot!
Here's a slightly shorter version that IMO is a bit easier to understand:

      - run: sed -En 's/^go (.*)$/GO_VERSION=\1/p' go.mod >> $GITHUB_ENV
      - uses: actions/setup-go@v2
        with:
          go-version: "${{ env.GO_VERSION }}"

@Prajwal-Koirala
Copy link

Linux

- name: Get the system requirements.
  run: sudo apt-get install coreutils -y
- name: Get the current version of Go from project.
  run: cat go.mod | head -3 | tail -1 | echo "GO_VERSION_FROM_PROJECT=$(cut -d' ' -f2)" >>${GITHUB_ENV}
- name: Set up Go version "${{ env.GO_VERSION_FROM_PROJECT }}"
  uses: actions/setup-go@v2
  with:
    go-version: "${{ env.GO_VERSION_FROM_PROJECT }}"
  id: go

MacOS

- name: Get the system requirements.
  run: brew install coreutils
- name: Get the current version of Go from project.
  run: cat go.mod | head -3 | tail -1 | echo "GO_VERSION_FROM_PROJECT=$(cut -d' ' -f2)" >>${GITHUB_ENV}
- name: Set up Go version "${{ env.GO_VERSION_FROM_PROJECT }}"
  uses: actions/setup-go@v2
  with:
    go-version: "${{ env.GO_VERSION_FROM_PROJECT }}"
  id: go

Windows

- name: Get the current version of Go from project.
  run: type go.mod | select -first 3 | select -last 1 | %{ 'GO_VERSION_FROM_PROJECT={1}' -f ($_ -split ' ') } >>$env:GITHUB_ENV
- name: Set up Go version "${{ env.GO_VERSION_FROM_PROJECT }}"
  uses: actions/setup-go@v2
  with:
    go-version: "${{ env.GO_VERSION_FROM_PROJECT }}"
  id: go

@tisba
Copy link

tisba commented Apr 12, 2022

Since Go is already available on the GH Action runners, you can have it as simple as:

go mod edit -json | jq -r .Go

@Prajwal-Koirala
Copy link

Since Go is already available on the GH Action runners, you can have it as simple as:

go mod edit -json | jq -r .Go

What's for windows??

@vsafonkin
Copy link

@prajwal-koirala-0000, it works for Windows as well: https://github.com/vsafonkin/test-repo/runs/6173404643?check_suite_focus=true
Screenshot 2022-04-26 at 11 58 17 AM

@IvanZosimov
Copy link
Contributor

Hello, @pantelis-karamolegkos 👋 Thank you and all others for contributing! We are closing this issue because this PR has been successfully merged. If you have any questions or additions feel free to ping us.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request to improve the current logic
Projects
None yet
Development

No branches or pull requests

8 participants