Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 11

Best way to automate versioning + publishing to GitHub Package Registry

I have a CI/CD workflow that consists of running a bunch of standard commands (linting, unit tests, e2e) on an Angular 8 project when a pull request event occurs (you can not directly push to the master branch since it is protected...). If all is well, the version inside the package.json is bumped, then this file is commited and pushed. Then it generates a production build inside a dist folder then packages and publishes the result to GitHub Package Repository.

 

This is a simplified version of my package.json:

 

{
  "name": "@peterhewat/test-actions-gpr",
  "version": "0.0.1",
  "files": [
    "dist"
  ],
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/"
  }
}

 

And this is a simplified version of my .github/workflow/build.yml

name: Publish to GitHub Package Registry

on:
  pull_request:
    branches:
      - master

jobs:

  publish-gpr:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: 12
          registry-url: https://npm.pkg.github.com/
          scope: '@peterhewat'
          
# This is where I would run my linting, unit tests, e2e...
      
      - name: Setup GIT
        run: |
          git switch -c master
          git config --global user.email "<email>"
          git config --global user.name "PeterHewat"
          
      - name: Bump version
        run: |
          npm version patch
          git push https://${{secrets.GPR_TOKEN}}@github.com/PeterHewat/test-actions-gpr
        

# Mock generation of production build then publish package - name: Build & Publish package run: | mkdir dist echo "$(date)" >> dist/README.md npm publish env: NODE_AUTH_TOKEN: ${{secrets.GPR_TOKEN}}

 

Is there a better way of doing this ?

 

The reason I have the "Setup GIT" step is because when "- uses: actions/checkout@v1" is performed, I get the following message at the end:

git checkout --progress --force refs/remotes/pull/1/merge
Note: switching to 'refs/remotes/pull/1/merge'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 082ec79 Merge 6f7edec364aaad1484f7a16150238c47d9fa9018 into 6b3673c2a03dbac8371699af14b8d1b3559c1074
Removed matchers: 'checkout-git'
##[remove-matcher owner=checkout-git]

 

I would rather not have to specify my email + name manually there. I would also rather not have to manually specify the repo URL (along with an access token in a secret) during the git push command. Is this possible ?

 

Also, I would rather not use an access token that is linked to a user since this repo is going to be hosted in a GitHub enterprise account and the user may disapear independently of the repo. Is there a way to use a read/write deploy key that is linked to the repo instead ?

10 Replies
Copilot Lvl 2
Message 2 of 11

Re: Best way to automate versioning + publishing to GitHub Package Registry

I suspect I am suppose to use GITHUB_TOKEN to perform a push inside an action but my attemps to do so have failed.

 

This is what I have attempted so far:

 

name: CI

on:
  push:
    branches:
    - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - run: echo "$(date)" >> README.md
      - run: git add .
      - run: git commit -m "test"
      - run: git push
#     - run: git push https://"$GITHUB_ACTOR":"$GITHUB_TOKEN"@github.com/"$GITHUB_REPOSITORY".git HEAD:"$GITHUB_REF"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

 

But I get stuck when it performs the git commit complaining as follows:

> Run git commit -m "test"

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: empty ident name (for <runner@fv-az59.ku0w1gboi0rutked1kh2bzkgvh.cx.internal.cloudapp.net>) not allowed
##[error]Process completed with exit code 128.

What is the proper way of using GITHUB_TOKEN ?

 

Copilot Lvl 3
Message 3 of 11

Re: Best way to automate versioning + publishing to GitHub Package Registry

You need to do what the error says. Run the following commands to setup your Git identity before attempting a push.

 

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

 

 Sorry, I had an example with it, but I cant find it right now ;(

Copilot Lvl 2
Message 4 of 11

Re: Best way to automate versioning + publishing to GitHub Package Registry

Deleted duplicate post

Copilot Lvl 2
Message 5 of 11

Re: Best way to automate versioning + publishing to GitHub Package Registry

I am a little closer to what I am trying to do:

name: Publish to GitHub Package Registry

on:
  pull_request:
    branches:
      - master

jobs:

  publish-gpr:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: 12
          registry-url: https://npm.pkg.github.com/
          scope: '@peterhewat'
          
      - run: git config --local --list
      
      - name: Setup GIT
        run: |
          git switch -c master
          git config user.email "my@email.com"
          git config user.name "$GITHUB_ACTOR"
          
      - name: Bump version
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        run: |
          npm version patch
          git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY"
#         git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY" "HEAD:$GITHUB_REF"

# Mock generation of production build then publish package
- name: Build & Publish package
env:
NODE_AUTH_TOKEN: ${{secrets.GPR_TOKEN}}
# NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
run: |
mkdir dist
echo "$(date)" >> dist/README.md
npm publish

I now use GITHUB_TOKEN to push my commit and GITHUB_ACTOR to specify the user that initiated the action. I still haven't found how to specify the email of the user that initiated the action. It is hard coded for the moment.

 

Unfortunately, I still use a PAT (personel access token) to publish my package (cf. secrets.GPR_TOKEN). If I try to use GITHUB_TOKEN, I get the following error message:

mkdir dist
cp test.txt dist/
npm publish
shell: /bin/bash -e {0}
env:
NPM_CONFIG_USERCONFIG: /home/runner/work/_temp/.npmrc
NODE_AUTH_TOKEN: ***
npm notice 
npm notice 📦 @peterhewat/test-actions-gpr@0.0.152
npm notice === Tarball Contents === 
npm notice 197B package.json 
npm notice 16B README.md 
npm notice 7B dist/test.txt
npm notice === Tarball Details === 
npm notice name: @peterhewat/test-actions-gpr 
npm notice version: 0.0.152 
npm notice package size: 335 B 
npm notice unpacked size: 220 B 
npm notice shasum: 3bd55f75b81bc5d131c5274ad3f99ca5ee5f63b0
npm notice integrity: sha512-f0v5eOYeszog4[...]6PAEzEy53T9Kw==
npm notice total files: 3 
npm notice 
npm ERR! code E500
npm ERR! 500 Internal Server Error - PUT https://npm.pkg.github.com/@peterhewat%2ftest-actions-gpr - Internal Server Error

npm ERR! A complete log of this run can be found in:
npm ERR! /home/runner/.npm/_logs/2019-09-10T23_16_41_989Z-debug.log
##[error]Process completed with exit code 1.

Any ideas how to solve these remaining issues ?

Copilot Lvl 3
Message 6 of 11

Re: Best way to automate versioning + publishing to GitHub Package Registry

I think you need the personal access token to be able to push code. I had the same problem when using the GITHUB_TOKEN and had to use a personal access token. But I hope I am wrong and there is a way to be able to push with just the GITHUB_TOKEN.

 

As an e-mail you could use:

git config user.email "$GITHUB_ACTOR@users.noreply.github.com"

 

EDIT:

I think I misunderstood your text. If you need to authenticate against the GitHub Package Registry. Try this as a step:

      - name: (optional) Authenticate against GitHub package registry
        run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc

 

And a second edit:

I did not have any luck by git pushing the code your way:

git config user.name "$GITHUB_ACTOR"
git config user.email "$GITHUB_ACTOR@users.noreply.github.com"

git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY"

I get a

remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/tangro/actions-test/'

The only thing that works for me is having a RELEASE_USERNAME and RELEASE_TOKEN (which is a personal access token) in my secrets configuration. This has also the added benefit that I can have a separate bot user be able to push and publish the code. 

      - name: add and push
        run: |
          git config user.name "${{ secrets.RELEASE_USERNAME }}"
          git config user.email "${{ secrets.RELEASE_USERNAME }}@users.noreply.github.com"
          git add -A
          git commit -m "released new version"
          git push --force https://${{ secrets.RELEASE_TOKEN }}@github.com/${{ github.repository }}.git $(node -p -e "require('./package.json').version")

I use it in my workflow to automatically publish an action on a push to the master branch 

Copilot Lvl 2
Message 7 of 11

Re: Best way to automate versioning + publishing to GitHub Package Registry

Hi ankri,

 

Thank you for your trick concerning the user.email. I confirm that I managed to perform a push using the GITHUB_TOKEN. Here is an example that works:

name: Push Test
on:
  push:
    branches:
      - master

jobs:

  publish-gpr:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
                
      - name: Setup GIT
        run: |
          git switch -c master
          git config user.email "$GITHUB_ACTOR@users.noreply.github.com"
          git config user.name "$GITHUB_ACTOR"
          
      - name: Update & Push
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        run: |
          printf "\n\n$(date)" >> README.md
          git add .
          git commit -m "test"
          git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY"

The big difference compared to using a personal access token (PAT) is that with the PAT, the push inside the action will generate a new push event which will make the action loop forever... Using GITHUB_TOKEN, doesn't.

 

My remaining issue is that I am unable to perform an npm publish with GITHUB_TOKEN.

 

I have tried your solution:

echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc

But I get: npm ERR! 500 Internal Server Error - PUT https://npm.pkg.github.com/...

 

This hack theoretically replaces:

with:
  registry-url: https://npm.pkg.github.com/

and:

env:
  NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

This works with a PAT but not with GITHUB_TOKEN :-(

 

Copilot Lvl 3
Message 8 of 11

Re: Best way to automate versioning + publishing to GitHub Package Registry

I asked a similiar question here. The only thin that worked for me was putting the GITHUB_TOKEN to the .npmrc - Have you tried actions/npm to publish?

Copilot Lvl 2
Message 9 of 11

Re: Best way to automate versioning + publishing to GitHub Package Registry

If you copy past my push example, it should work as is.

 

Concerning actions/npm to publish, it uses the previous HCL syntax (Actions v1) and not the YAML syntax (Actions v2). Not sure it is compatible.

Copilot Lvl 3
Message 10 of 11

Re: Best way to automate versioning + publishing to GitHub Package Registry