How to push multiple changes from matrix in Github Actions

14 Views Asked by At

I'm creating workflow which accepts some inputs and based on that creates files which have to be committed and pushed. Also i need to create PR from those commits. Im creating array of inputs and using them as matrix values.

the issue appears when i'm selecting more than one 'name' from inputs and action can't commit or create pull requests. Any suggestion how it should be done correctly? It works if only one input is selected or when i'm creating separate pull requests for each input. I need to create only one pull request with all changes tho.

Here is example workflow:

name: Project
on:
  workflow_dispatch:
    inputs:
      title:
        description: 'title'
        type: string
      name1:
        description: 'name1'
        type: boolean
      name2:
        description: 'name2'
        type: boolean
      name3:
        description: 'name3'
        type: boolean

jobs:
  create-matrix:
    name: create matrix
    runs-on: [self-hosted, linux]
    outputs:
      names: ${{ steps.names.outputs.names }}
    steps:
      - name: matrix
        id: names
        uses: actions/github-script@v6
        with:
          script: |
            const names = [];
            if (${{ github.event.inputs.name1 }}) names.push("name1");
            if (${{ github.event.inputs.name2 }}) names.push("name2");
            if (${{ github.event.inputs.name3 }}) names.push("name3");
            core.setOutput("names", names);

  create:
    name: Create
    needs: names
    runs-on: [self-hosted, linux]
    strategy:
      matrix:
        environment: ${{fromJson(needs.names.outputs.names)}}
    steps:
      - uses: actions/checkout@v3
      - name: Render project template
        uses: chuhlomin/render-template@v1
        with:
          template: templates/template.txt
          result_path: workspaces/${{ matrix.environment }}/file_name.txt
          vars: |
            name: ${{ github.event.inputs.title }}
      - uses: EndBug/add-and-commit@v8
        with:
          new_branch: ${{ github.event.inputs.title }}
          push: true

  pull-request:
    name: Create Pull Request
    needs: create
    runs-on: [self-hosted, rocky]
    steps:
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v5
        with:
          branch: ${{ github.event.inputs.title }}
          base: main
          title: "Created ${{ github.event.inputs.title }}"

I tried using different inputs of actions peter-evans/create-pull-request and EndBug/add-and-commit. also other actions

sometimes i manage to create a branch with all files. but i can create PR Then. Or im getting these kind of errors when wanting to commit:

error: failed to push some refs to 'https://github.com/my/repo'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
Error: Error: Pushing to https://github.com/my/repo
POST git-receive-pack (971 bytes)
error: failed to push some refs to 'https://github.com/my/repo'

0

There are 0 best solutions below