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

ci: Automated lib version bumping #213

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
220 changes: 220 additions & 0 deletions .github/workflows/npm-publish-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
# SPDX-License-Identifier: Apache-2.0

name: Publish package to GitHub Packages
on:
push:
branches: [ "dev" ]
workflow_dispatch:

jobs:

build-and-publish:
if: github.actor != 'dependabot[bot]' && github.actor != 'dependabot-preview[bot]'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4

# Setup .npmrc file to publish to GitHub Packages
- name: Setup Node.js (.npmrc)
uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://npm.pkg.github.com'
# Defaults to the user or organization that owns the workflow file
scope: 'tazama-lf'

- name: Version prerelease
run: npm version prerelease
env:
GH_TOKEN: '${{ secrets.GH_TOKEN }}'

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

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

sync-version-across-tazama-repos:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use dependabot instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rtkay123 is this something we're currently doing? I don't think I've seen dependabot being used for this use case but I'd like to if we're doing this.

Let us wait for what @cshezi thinks, I did have a chat with him on Friday before making these changes.

Context:

The frms-coe-lib and frms-coe-startup-lib versions don't reflect correctly in most rule processors except 901. If I also remember well, before the last release we manually created PRs to do the version bumping and all.

Briefly what these changes will do is ensure every time new versions of frms-coe-lib and frms-coe-startup-lib are published, a PR will be created in each repo that lists the lib as a dependency in package.json

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not currently doing that, but the change in my opinion should be in the dependabot config so that it's able to update frms-coe-lib and frms-coe-startup-lib.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to look into this today

if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
needs: build-and-publish
env:
PR_NUMBER: ${{ github.event.number }}
GH_TOKEN: '${{ secrets.GH_TOKEN }}'
steps:
- uses: actions/checkout@v4

- name: Get the new Verion and store it as a variable
run: NEW_VERSION=$(npm pkg get version --workspaces=false | tr -d \")

- name: update frms-coe-lib version across tazama-lf org repos in dev
env:
BRANCH: sync-frms-coe-lib-version-dev
GH_TOKEN: '${{ secrets.GH_TOKEN }}'
PR_REVIEWERS: |
rtkay123
# Lenbkr
# Justus-at-Tazama
cshezi
# JeanPierreNell
scott45
REPOS: | # List of repositories to have version updated
# relay-service
rule-901
# typology-processor
# channel-router-setup-processor
# event-sidecar
# lumberjack
# nats-utilities
# batch-ppa
# admin-service
# payment-platform-adapter
rule-executer
# tms-service
# transaction-aggregation-decisioning-processor
frms-coe-startup-lib
run: |
for repo in $REPOS; do
git clone https://github.com/frmscoe/$repo.git
cd $repo
git remote set-url origin https://x-access-token:${{ secrets.GH_TOKEN }}@github.com/frmscoe/$repo.git

if git ls-remote --heads origin "${{ env.branch }}" | grep "${{ env.branch }}"; then
# Branch exists, pull the latest changes
git checkout "${{ env.branch }}"
git pull origin "${{ env.branch }}"
else
# Branch does not exist, create it
git checkout -b "${{ env.branch }}"
fi

sed -i 's/"version": "[^"]*"/"version": "$NEW_VERSION"/' package.json

if [[ `git status --porcelain=v1 2>/dev/null | wc -l` == 0 ]]; then
echo "no diff detected, exiting..."
exit 0;
fi

git add -A
git commit -m "chore: update frms-coe-lib to $NEW_VERSION" || exit 0
git push origin "${{ env.branch }}"

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

# Create the PR with reviewers
IFS=',' read -ra REVIEWERS <<< "${PR_REVIEWERS}"
REVIEWERS_ARGS=""
for reviewer in "${REVIEWERS[@]}"; do
REVIEWERS_ARGS+="--reviewer $reviewer "
done

gh pr create --title "chore: update frms-coe-lib to $NEW_VERSION in $repo" --body "update frms-coe-lib to $NEW_VERSION based on [this pull request](https://github.com/tazama-lf/frms-coe-lib/pull/$PR_NUMBER)" --base dev --head "${{ env.branch }}" $REVIEWERS_ARGS || echo "PR already exists, updating existing PR"

# Cleanup
rm /tmp/gh_token

cd ..
done

- name: update frms-coe-lib version across frmscoe org repos in dev
env:
BRANCH: sync-frms-coe-lib-version-dev
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
PR_REVIEWERS: |
rtkay123
Lenbkr
Justus-at-Tazama
cshezi
JeanPierreNell
scott45
REPOS: | # List of repositories to have version updated
rule-001
# rule-002
# rule-003
# rule-004
# rule-006
# rule-007
# rule-008
# rule-010
# rule-011
# rule-016
# rule-017
# rule-018
# rule-020
# rule-021
# rule-024
# rule-025
# rule-026
# rule-027
# rule-028
# rule-030
# rule-044
# rule-045
# rule-048
# rule-054
# rule-063
# rule-074
# rule-075
# rule-076
# rule-078
# rule-083
# rule-084
# rule-090
# rule-091
run: |
for repo in $REPOS; do
git clone https://github.com/frmscoe/$repo.git
cd $repo
git remote set-url origin https://x-access-token:${{ secrets.GH_TOKEN }}@github.com/frmscoe/$repo.git

if git ls-remote --heads origin "${{ env.branch }}" | grep "${{ env.branch }}"; then
# Branch exists, pull the latest changes
git checkout "${{ env.branch }}"
git pull origin "${{ env.branch }}"
else
# Branch does not exist, create it
git checkout -b "${{ env.branch }}"
fi

sed -i 's/"version": "[^"]*"/"version": "$NEW_VERSION"/' package.json

if [[ `git status --porcelain=v1 2>/dev/null | wc -l` == 0 ]]; then
echo "no diff detected, exiting..."
exit 0;
fi

git add -A
git commit -m "chore: update frms-coe-lib to $NEW_VERSION" || exit 0
git push origin "${{ env.branch }}"

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

# Create the PR with reviewers
IFS=',' read -ra REVIEWERS <<< "${PR_REVIEWERS}"
REVIEWERS_ARGS=""
for reviewer in "${REVIEWERS[@]}"; do
REVIEWERS_ARGS+="--reviewer $reviewer "
done

gh pr create --title "chore: update frms-coe-lib to $NEW_VERSION in $repo" --body "update frms-coe-lib to $NEW_VERSION based on [this pull request](https://github.com/tazama-lf/frms-coe-lib/pull/$PR_NUMBER)" --base dev --head "${{ env.branch }}" $REVIEWERS_ARGS || echo "PR already exists, updating existing PR"

# Cleanup
rm /tmp/gh_token

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

name: Publish package to GitHub Packages
on:
push:
branches: [ "main" ]
workflow_dispatch:

jobs:
build-and-publish:
if: github.actor != 'dependabot[bot]' && github.actor != 'dependabot-preview[bot]'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4

# Setup .npmrc file to publish to GitHub Packages
- name: Setup Node.js (.npmrc)
uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://npm.pkg.github.com'
# Defaults to the user or organization that owns the workflow file
scope: 'tazama-lf'

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

- name: Build library
run: npm run build

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