skip to Main Content

I coded a small Golang program, that takes N arguments (files with resourceQuota requests for K8s) from a GitHub PR, and writes a file with the total amount of CPU and Memory requested that is then exported as GHA output in the next step.

can’t do go run main.go /path/to/file1 /path/to/file2 because Apparently i hit a bug in actions/setup-go with our self-hosted runner, so i had to containerise that.

I’m new to all of these, so my colleagues told me to import the docker program from a self-made GitHub action, all works like a charm when only 1 file is changed in the PR (only 1 arg to handle).

Problem is when 2 or more args are passed, because the action im using: tj-actions/changed-files outputs a single string with all the files names and i’m really clueless on how to work around it.

this is how i call the self-made action:

      - name: Capture request values actions
        uses: ./goCapacityCheck
        with:
          files: ${{ steps.changed-files.outputs.all_changed_files }}

goCapacityCheck action.yml

name: 'goCapacityCheck'
description: 'Capture requests CPU and Memory values from all files in the PR'
inputs:
  files:
    description: 'files changed in the PR.'
    required: false

runs:
  using: 'docker'
  image: './Dockerfile'
  args:
    - ${{ inputs.files }}

is there a way to split that string when passing to the action? or to Docker or something?

I haven’t tried much when i hit this issue, but i’d expect that the sting that looks like "/path/to/file1 /path/to/file2" to be split at some point in order to be able to do
docker run --name mygocap gocap /path/to/file1 /path/to/file2

2

Answers


  1. Chosen as BEST ANSWER

    I ended up having to handle it inside the go program because i would never now how many arguments would come in, so from os.Args[] i called getFileNames() and then i did the rest of the routine with filenames instead of os.Args[1:] directly.

    func main() {
        filenames := getFileNames(os.Args[1:]) {
        for _, file := range filenames {
            // handling of the file data
            fmt.Println(file)
    }
        
    func getFileNames(s []string) []string {
        var filenames []string
        for _, file := range s {
            if strings.Contains(file, " ") {
                filenames = append(filenames, strings.Split(file, " ")...)
            } else {
                filenames = append(filenames, file)
            }
        }
        return filenames
    }
    

  2. You could try to split with some bash tricks, as example:

    
    string="/path/to/file1 /path/to/file2"
    echo "$(echo $string | cut -d' ' -f1)" "$(echo $string | cut -d' ' -f2)"
    

    will print

    "/path/to/file1" "/path/to/file2"
    

    So you could try something like this in a GitHub action workflow:

          - name: run
            run: |
              string=${{ steps.changed-files.outputs.all_changed_files }}
              first=$(echo $string | cut -d' ' -f1)
              second=$(echo $string | cut -d' ' -f2)
              docker run --name mygocap gocap $first $second
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search