Chore: Release #59
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Release and Publish to GitHub Packages | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
test: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
- name: Install dependencies | |
run: npm install | |
- name: Run vitest | |
run: npm run test || true | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
lint: | |
runs-on: ubuntu-latest | |
needs: test | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
- name: Install dependencies | |
run: npm install | |
- name: Run lint fix | |
run: npm run lint:fix || true | |
- name: Commit changes if any | |
run: | | |
git config user.name "goatee-ci" || true | |
git config user.email "ci@codegoat.dev" || true | |
git restore "./package-lock.json" || true | |
if [[ -n $(git status --porcelain) ]]; then | |
git add . || true | |
git commit -m "CI: Lint code" -m "Lints code." || true | |
git push origin main || true | |
echo "🔄 Merging main into dev..." | |
git fetch origin || true | |
git checkout dev || true | |
git merge origin/main --no-edit || true | |
git push origin dev || true | |
else | |
echo "No lint fixes to commit." | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
release: | |
runs-on: ubuntu-latest | |
needs: lint | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Check if version changed | |
id: check_version | |
run: | | |
git diff --cached --diff-filter=d package.json || { | |
echo "No version change detected in package.json. Skipping release." | |
exit 1 | |
} | |
- name: Get version from package.json | |
id: get_version | |
run: | | |
VERSION=$(jq -r '.version' package.json) | |
echo "version=$VERSION" >> $GITHUB_OUTPUT | |
- name: Extract latest changelog entry | |
id: changelog | |
run: | | |
CHANGELOG_CONTENT=$(awk 'BEGIN {print_section=0;} /^## \[/ {if (print_section == 0) {print_section=1;} else {exit;}} print_section {print;}' CHANGELOG.md) | |
echo "content<<EOF" >> $GITHUB_OUTPUT | |
echo "$CHANGELOG_CONTENT" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
- name: Create GitHub release | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const changelog = `${{ steps.changelog.outputs.content }}`; | |
const release = await github.rest.repos.createRelease({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
tag_name: `v${{ steps.get_version.outputs.version }}`, | |
name: `v${{ steps.get_version.outputs.version }}`, | |
body: changelog, | |
}) | |
console.log(`Created release ${release.data.html_url}`) | |
- name: Upload package to GitHub release | |
uses: actions/upload-artifact@v4 | |
with: | |
name: package | |
path: | | |
. | |
!.git | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
publish: | |
runs-on: ubuntu-latest | |
needs: release | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
registry-url: 'https://npm.pkg.github.com/' | |
scope: '@CodeGoat-dev' | |
- name: Install dependencies | |
run: npm install | |
- name: Publish to GitHub Packages | |
run: npm publish | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
publish-npm: | |
runs-on: ubuntu-latest | |
needs: publish | |
steps: | |
- name: Checkout source | |
uses: actions/checkout@v4 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
registry-url: 'https://registry.npmjs.org/' | |
- name: Install dependencies | |
run: npm install | |
- name: Temporarily update package name for npm | |
run: | | |
cp package.json package.json.bak | |
jq '.name = "@codegoatx/goatee"' package.json > package.tmp.json | |
mv package.tmp.json package.json | |
- name: Publish to npmjs | |
run: npm publish --registry https://registry.npmjs.org --access public | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
- name: Restore original package.json | |
run: mv package.json.bak package.json |