How To Set Up GitHub Actions to Publish a Lerna Monorepo
Answer a question
I maintain a lerna/yarn monorepo. I'm in the process of migrating the CI/CD from circle to the new GitHuba Actions publish beta. I've created the following workflow:
name: CD
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Checkout master
run: git checkout master
- name: Install rsync
run: sudo apt install rsync
- name: Install yarn
run: |
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update
sudo apt-get install yarn
- name: Install Packages
run: yarn install
- name: Test
run: yarn test
- name: Upload coverage results to Code Climate
run: sh ./scripts/upload-coverage.sh
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
- name: Authenticate with Registry
run: echo "registry=//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Configure CI Git User
run: |
git config --global user.email octobot@github.com
git config --global user.name GitHub Actions
- name: Publish package
run: yarn deploy --yes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Build Docs
run: yarn docs
- name: Deploy Docs
run: |
echo "apolloelements.dev" > docs/CNAME
npx gh-pages --dist docs
It fails at the Publish Packages step with this message:
lerna info git Pushing tags...
lerna ERR! Error: Command failed: git push --follow-tags --no-verify origin master
lerna ERR! fatal: could not read Username for 'https://github.com': No such device or address
lerna ERR!
lerna ERR! at makeError (/home/runner/work/apollo-elements/apollo-elements/node_modules/execa/index.js:174:9)
lerna ERR! at Promise.all.then.arr (/home/runner/work/apollo-elements/apollo-elements/node_modules/execa/index.js:278:16)
lerna ERR! Error: Command failed: git push --follow-tags --no-verify origin master
lerna ERR! fatal: could not read Username for 'https://github.com': No such device or address
lerna ERR!
lerna ERR! at makeError (/home/runner/work/apollo-elements/apollo-elements/node_modules/execa/index.js:174:9)
lerna ERR! at Promise.all.then.arr (/home/runner/work/apollo-elements/apollo-elements/node_modules/execa/index.js:278:16)
lerna ERR! lerna Command failed: git push --follow-tags --no-verify origin master
lerna ERR! lerna fatal: could not read Username for 'https://github.com': No such device or address
lerna ERR! lerna
error Command failed with exit code 128.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Changing the remote to use HTTPS and the github token hasn't helped:
git remote rm origin
git remote add origin "https://$USER_NAME:$GITHUB_PERSONAL_ACCESS_TOKEN@github.com/apollo-elements/apollo-elements.git"
Where GITHUB_PERSONAL_ACCESS_TOKEN is a PAT passed via secrets.
In that case, I received this error:
lerna ERR! ENOREMOTEBRANCH Branch 'master' doesn't exist in remote 'origin'.
How should I set up the project to be able to push tags and commits back to the repository from CD?
Answers
UPDATED:
This configuration actually works end-to-end. The key features of this config are:
- setting the remote with
git remote set-url origin https://$GITHUB_ACTOR:$GITHUB_PAT@github.com/bennypowers/apollo-elementsGITHUB_ACTORis provided by the runner,GITHUB_PATis a Github Personal Access Token set in the repository's secrets. - re-checking and pulling with
git checkout "${GITHUB_REF:11}" && git pull - logging out of yarn, since
lernacannot handle yarn for whatever reason. - using the specific, finicky
.npmrcsetup shown below, since this is a scoped package. - running
npm whoamiafter setting up auth. This will throw if authentication is brokenlerna publishwill push tags for each of your packages, and maybe write to the CHANGELOG.md and package.json files as well, even if it doesn't publish due to bad auth. Runningnpm whoamihere to check that you actually can publish before runninglernaprevents the headache of manually restoring the state of the repo. - passing
GITHUB_TOKEN,GH_TOKEN, andNPM_TOKENtolerna publish
name: CD
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v1
- name: Configure CI Git User
run: |
git config --global user.name '@bennypowers'
git config --global user.email 'bennypowers@users.noreply.github.com'
git remote set-url origin https://$GITHUB_ACTOR:$GITHUB_PAT@github.com/bennypowers/apollo-elements
env:
GITHUB_PAT: ${{ secrets.GITHUB_PAT }}
- name: Checkout and pull branch
run: git checkout "${GITHUB_REF:11}" && git pull
- name: Install Packages
run: yarn install
- name: Authenticate with Registry
run: |
yarn logout
echo "@apollo-elements:registry=http://registry.npmjs.org/" > .npmrc
echo "registry=http://registry.npmjs.org/" >> .npmrc
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc
npm whoami
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Publish package
run: lerna publish --yes --message 'chore: release new versions'
env:
GH_TOKEN: ${{ secrets.GITHUB_PAT }}
GITHUB_TOKEN: ${{ secrets.GITHUB_PAT }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
Note the above config redacts some irrelevant steps. See the complete workflow for the unredacted version
ORIGINAL ANSWER:
With help from StackOverflow user @rmunn, I arrived at this solution:
- name: Configure CI Git User
run: |
git remote rm origin
git remote add origin "https://$USER_NAME:$GITHUB_PAT@github.com/apollo-elements/apollo-elements.git"
git fetch
git config --global user.email octobot@github.com
git config --global user.name GitHub Actions
env:
USER_NAME: ${{ secrets.DEPLOYING_USER_NAME }}
GITHUB_PAT: ${{ secrets.GITHUB_PAT }}
Where GITHUB_PAT is a personal access token with repo scope, saved in secrets.
The git fetch is required to set up local branches on the altered remote. The personal access token is required to push back to the repository.
更多推荐


所有评论(0)