Home > Software design >  github workflow to push to private repo
github workflow to push to private repo

Time:10-13

I made this workflow to push changes from my private github repo to push it to another private repo on another platform

name: Push to repo
on:
  workflow_dispatch:
    BRANCH:
      description: 'Branch to use'
      required: true
      default: 'production'
      type: choice
      options:
        - production
        - develop
jobs:
  push-to-ccfe:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Setup SSH Keys and known_hosts
        env:
          SSH_AUTH_SOCK: /tmp/ssh_agent.sock
        run: |
          mkdir -p ~/.ssh
          ssh-keyscan git.something  >> ~/.ssh/known_hosts
          ssh-agent -a ${{ env.SSH_AUTH_SOCK }} > /dev/null
          ssh-add - <<< "${{ secrets.SSH_PRIVATE_KEY }}"
          
      - name: setup git and push to remote
        env:
          SSH_AUTH_SOCK: /tmp/ssh_agent.sock
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "${{ secrets.GH_MAIL }}"
          git remote add newremote [email protected]
      - name: Check branch and push
        env:
          SSH_AUTH_SOCK: /tmp/ssh_agent.sock
        run: |
          git fetch origin -a        
          git push newremote ${{ inputs.BRANCH }}:${{ inputs.BRANCH }}

it's all good up until I actually try to push

where I first get a warning:

warning: no common commits

and then the actual error:

 ! [rejected]        develop -> develop (non-fast-forward)
error: failed to push some refs to 'git.something'
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.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I don't understand if I try from the terminal and push I get no errors.

p.s. the point of this workflow is to avoid using the terminal, I want to put a cronjob so that changes to this repo are pushed in another remote.

CodePudding user response:

apparently the solution was to change the checkout command at the beginning.

this gets me what I needed

name: Push to repo
on:
  workflow_dispatch:
    inputs:
      BRANCH:
        description: 'Branch to use'
        required: true

jobs:
  push-to-domainXXX:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup SSH Keys and known_hosts
        env:
          SSH_AUTH_SOCK: /tmp/ssh_agent.sock
        run: |
          mkdir -p ~/.ssh
          ssh-keyscan git.domainXXX  >> ~/.ssh/known_hosts
          ssh-agent -a ${{ env.SSH_AUTH_SOCK }} > /dev/null
          ssh-add - <<< "${{ secrets.SSH_PRIVATE_KEY }}"

      - name: setup git and push to remote
        env:
          SSH_AUTH_SOCK: /tmp/ssh_agent.sock
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "${{ secrets.GH_MAIL }}"
          git remote add second_origin git@domainXXX:myusername/repo_name
          
      - name: Check branch and push
        env:
          SSH_AUTH_SOCK: /tmp/ssh_agent.sock
        run: |
          git push second_origin ${{ inputs.BRANCH }}
          

CodePudding user response:

Your commands are:

git remote add origin [email protected]
git fetch origin -a   
git push origin ${{ inputs.BRANCH }}:${{ inputs.BRANCH }}

You are fetching and pushing to the same origin (same remote repository)

Assuming that the original checkout did set origin, I would rather set a second remote, for referencing the second remote repository (you want to push to).
Then fetch from the first and push from the second:

git remote add second [email protected]
git fetch origin -a   
git push second ${{ inputs.BRANCH }}:${{ inputs.BRANCH }}
  • Related