Search code examples
gitgithubdevopsgithub-actions

How to commit and push to a private repo(A), from a different repo(B), in github actions workflow (B) , using personal access token


name: deploy-me
on: [push]
jobs:
  deploys-me:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '14'
      - run: npm install
      - run: npm run dev

     //Next  I want to copy some file from this repo and commit to a different repo and push it

This is my workflow.yaml file,

After npm run dev, I want to be able to copy a file to another directory, commit and push to that another repo


Solution

  • name: deploy-me
    'on':
        - push
    jobs:
        deploy-me:
            runs-on: ubuntu-latest
            steps:
                - uses: actions/checkout@v2
                - uses: actions/setup-node@v2
                  with:
                      node-version: '14'
                  env:
                      ACCESS_TOKEN: '${{ secrets.ACCESS_TOKEN }}'
                - run: npm install
                - run: npm run build
                - run: |
                      cd lib
                      git config --global user.email "[email protected]"
                      git config --global user.name "aliasifk"
                      git config --global credential.helper cache
                      git clone https://${{secrets.ACCESS_TOKEN}}@github.com/aliasifk/xxxxxx
                      cp index.js clonedFolder/ -f
                      cd clonedFolder
                      git add .
                      git commit -m "$(date)"
                      git push
    

    This is how I fixed my problem,

    1. I created a Personal Access Token from my developer settings and copied it for later step.

    2. I added ACCESS_TOKEN environment variable by navigating to my repository settings and adding a secret. Here I pasted the previously created access token.

    3. Then simply that code and using secrets context to access that token, note that name is similar to the one we created before

    In left menu of repo settings

    1. Now just push the code to repo and everything will work like charm :).

    Happy Hacking!