Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update and optimise workflows before syncing them across tazama-lf repos #12

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 0 additions & 46 deletions .github/workflows/bench.yml

This file was deleted.

80 changes: 0 additions & 80 deletions .github/workflows/dockerhub-image-build-main.yml

This file was deleted.

52 changes: 0 additions & 52 deletions .github/workflows/npm-publish-dev.yml

This file was deleted.

44 changes: 0 additions & 44 deletions .github/workflows/npm-publish-main.yml

This file was deleted.

161 changes: 161 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# SPDX-License-Identifier: Apache-2.0

name: Publish dev npm package to GitHub

on:
push:
branches:
- 'dev'
paths-ignore:
- package.json
- package-lock.json
workflow_dispatch:

jobs:
build-and-publish:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN_LIB }}
permissions:
Dismissed Show dismissed Hide dismissed
packages: write
contents: read

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node.js (.npmrc)
uses: actions/setup-node@v3
with:
node-version: 16.x
registry-url: https://npm.pkg.github.com/
# Defaults to the user or organization that owns the workflow file
scope: '@frmscoe'

- name: Set up NPM authentication
run: |
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GH_TOKEN_LIB }}" > ~/.npmrc
cat .npmrc

- name: Configure Git
run: |
git config user.email ${{ secrets.GH_EMAIL }}
git config user.name ${{ secrets.GH_USERNAME }}

- name: Version bumping
env:
GH_TOKEN: '${{ secrets.GH_TOKEN }}'
run: |
commit_message=$(git log -1 --pretty=%B)
echo "Commit message: $commit_message"
if [[ "$commit_message" == *'feat!:'* ]]; then
npm version major
elif [[ "$commit_message" == *"feat:"* ]]; then
npm version minor
else
npm version prerelease --preid=rc
fi

- name: Install dependencies
run: npm ci
env:
GH_TOKEN: '${{ secrets.GITHUB_TOKEN }}'

- name: Build library
run: npm run build

- name: Publish package
run: npm publish
env:
GH_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
NODE_AUTH_TOKEN: '${{ secrets.GITHUB_TOKEN }}'

- name: Capture Version
id: capture_version
run: |
export version=$(jq -r '.version' package.json)
echo "VERSION=$version" >> $GITHUB_ENV

- name: Push Changes in package.json and make PRs
run: |
export GH_USERNAME=${{ secrets.GH_USERNAME }}
export GH_TOKEN=${{ secrets.GH_TOKEN_LIB }}
git config --global user.name ${{ secrets.GH_USERNAME }}

# Clear the GITHUB_TOKEN environment variable and use a temporary file for gh authentication
echo "${{ secrets.GH_TOKEN_LIB }}" > /tmp/gh_token
unset GITHUB_TOKEN
unset GH_TOKEN
gh auth login --with-token < /tmp/gh_token

git clone https://${{ secrets.GH_USERNAME }}:${{ secrets.GH_TOKEN_LIB }}@github.com/${{ github.repository }}.git
REPO_NAME=$(basename -s .git https://github.com/${{ github.repository }}.git)
cd $REPO_NAME
echo "Currently in repository directory: $(pwd)"

if git ls-remote --heads origin version-bump | grep version-bump; then
# Branch exists, pull the latest changes
git checkout version-bump
git pull origin version-bump
else
# Branch does not exist, create it
git checkout -b version-bump
fi

git config --global user.email ${{ secrets.GH_EMAIL }}
git config --global user.name ${{ secrets.GH_USERNAME }}

# print current version
sed -i 's/"version": "[^"]*"/"version": "'"${{ env.VERSION }}"'"/' package.json
cat package.json
git add .
git commit -m "chore: Bump version after publishing to Github NPM" || echo "No changes to commit"
git push origin version-bump || git push origin version-bump --force

gh pr create --title "build: Automated PR; Bump version after publishing to Github NPM" --body "This pull request updates the version in the `package.json` and `package-lock.json` after the package was published." --base dev --head version-bump --assignee ${{ secrets.GH_USERNAME }} --label build || echo "PR already exists, updating existing PR"
PR_ID=$(gh pr view --json number -q ".number")
echo "PR_ID=$pr_id" >> $GITHUB_ENV

# Cleanup
rm /tmp/gh_token

# Send Slack Notification
- name: Send Slack Notification
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
# Fetch the PR ID from the environment
PR_ID=${{ env.PR_ID }}

curl -X POST -H 'Content-type: application/json' --data '{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "New NPM GitHub package published :white_check_mark:",
"emoji": true
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Github Repository:*\nhttps://github.com/${{ github.repository }}"
},
{
"type": "mrkdwn",
"text": "*Pull Requests:*\n<https://github.com/${{ github.repository }}/pull/${{ steps.capture_pr.outputs.PR_ID }}|List${{ steps.capture_pr.outputs.PR_ID }}>"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Please head over to the github repository and merge the PR linked above to update the `package.json` with the newly published npm package."
}
}
]
}' $SLACK_WEBHOOK_URL
Loading
Loading