Skip to content

Commit

Permalink
gh: streamline publishable packages and updated branchign
Browse files Browse the repository at this point in the history
  • Loading branch information
shanefontaine committed May 18, 2024
1 parent a25f1ae commit dafa3c5
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 175 deletions.
48 changes: 0 additions & 48 deletions .github/actions/check-package-updates/action.yml

This file was deleted.

31 changes: 11 additions & 20 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ name: Publish Packages to Docker Hub
on:
push:
branches:
- develop
- staging
- production
paths:
- 'packages/hop-node/**'
- 'packages/v2-explorer-backend/**'
- 'packages/api/**'
- 'packages/explorer-backend/**'
- 'packages/stats-worker/**'
env:
DOCKER_TAG: ${{ (github.ref_name == 'production' && 'latest') || 'unstable' }}

jobs:
package-updates:
name: Check for package updates
uses: ./.github/workflows/modified-packages.yml
publishable-packages:
name: Check for package that can be published
uses: ./.github/workflows/publishable-packages.yml
with:
package-names: 'hop-node:v2-explorer-backend:api:explorer-backend:stats-worker'

publish-docker:
name: Publish Docker
needs: package-updates
needs: publishable-packages
runs-on: ubuntu-latest
strategy:
matrix:
package-name: ${{ fromJson(needs.package-updates.outputs.modified-packages) }}
package-name: ${{ fromJson(needs.publishable-packages.outputs.publishable-packages) }}
steps:
- name: Checkout code
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b
Expand All @@ -35,12 +39,6 @@ jobs:
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Check Package Updates
id: check-package-updates
uses: ./.github/actions/check-package-updates
with:
package-name: ${{ matrix.package-name }}

- name: Set Docker Tags
id: set-docker-tag
Expand All @@ -52,17 +50,10 @@ jobs:
echo "IMAGE_NAME=hopprotocol/${{ matrix.package-name }}" >> $GITHUB_OUTPUT
fi
# Set the correct version to release
if [ "${{ steps.check-package-updates.outputs.package-version-changed }}" == "true" ]; then
echo "DOCKER_TAG=latest" >> $GITHUB_OUTPUT
else
echo "DOCKER_TAG=unstable" >> $GITHUB_OUTPUT
fi
- name: Publish Docker Image (${{ steps.set-docker-tag.outputs.DOCKER_TAG }})
uses: docker/build-push-action@2cdde995de11925a030ce8070c3d77a52ffcf1c0
with:
context: .
file: ./packages/${{ matrix.package-name }}/Dockerfile
tags: ${{ steps.set-docker-tag.outputs.IMAGE_NAME }}:${{ steps.set-docker-tag.outputs.DOCKER_TAG }}
tags: ${{ steps.set-docker-tag.outputs.IMAGE_NAME }}:${{ env.DOCKER_TAG }}
push: true
60 changes: 0 additions & 60 deletions .github/workflows/modified-packages.yml

This file was deleted.

25 changes: 7 additions & 18 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ name: Publish Packages to NPM
on:
push:
branches:
- develop
- production
paths:
- 'packages/sdk/**'

jobs:
package-updates:
name: Check for package updates
uses: ./.github/workflows/modified-packages.yml
publishable-packages:
name: Check for package that can be published
uses: ./.github/workflows/publishable-packages.yml
with:
package-names: 'sdk'

publish-sdk:
name: Publish NPM
needs: package-updates
needs: publishable-packages
runs-on: ubuntu-latest
strategy:
matrix:
package-name: ${{ fromJson(needs.package-updates.outputs.modified-packages) }}
package-name: ${{ fromJson(needs.publishable-packages.outputs.publishable-packages) }}
steps:
- name: Checkout code
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b
Expand All @@ -33,23 +33,12 @@ jobs:
with:
package-name: ${{ matrix.package-name }}

- name: Set Update Flag
id: set-update-flag
run: |
if [ "${{ steps.check-package-updates.outputs.npm-version-changed }}" == "true" ]; then
echo "SHOULD_CONTINUE=true" >> $GITHUB_OUTPUT
else
echo "SHOULD_CONTINUE=false" >> $GITHUB_OUTPUT
fi
- name: Install and build package
if: steps.set-update-flag.outputs.SHOULD_CONTINUE == 'true'
run: |
pnpm --filter-prod ${{ matrix.package-name }}... install
pnpm --filter ${{ matrix.package-name }}... build
- name: Publish package
if: steps.set-update-flag.outputs.SHOULD_CONTINUE == 'true'
run: pnpm --filter ${{ matrix.package-name }} publish --publish-branch develop --access public
run: pnpm --filter ${{ matrix.package-name }} publish --publish-branch production --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
73 changes: 73 additions & 0 deletions .github/workflows/publishable-packages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# A publishable package is a package that has been modified and, if the
# if the branch is production, has a new version.

name: Get packages that should be published
on:
workflow_call:
inputs:
package-names:
description: 'Package names to consider, separated by commas.'
required: true
type: string
outputs:
publishable-packages:
description: 'Returns the name of all packages that should be published.'
value: ${{ jobs.publishable-packages.outputs.publishable-packages }}

jobs:
publishable-packages:
runs-on: ubuntu-latest
outputs:
publishable-packages: ${{ steps.publishable-packages.outputs.sanitized_packages }}
steps:
- name: Checkout code
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b

- name: Get modified dirs
id: modified-dirs
uses: tj-actions/changed-files@a29e8b565651ce417abb5db7164b4a2ad8b6155c
with:
since_last_remote_commit: true
dir_names: true
dir_names_max_depth: 2
separator: ','
files: ${{ github.event.inputs.paths }}

- name: Get publishable packages
id: publishable-packages
run: |
# Extract changed directories from output, removing the 'package/' prefix
# Example: packages/hop-node,packages/v2-explorer-backend,packages/frontend
modified_dirs="${{ steps.modified-dirs.outputs.all_changed_files }}"
# Read input package names, compare with modified directories
# Example: packages/hop-node,packages/v2-explorer-backend
IFS=":"
read -a input_packages <<< "${{ inputs.package-names }}"
modified_packages=""

for package in "${input_packages[@]}"; do
package_path="packages/$package"

# Skip packages not modified
if [[ $modified_dirs != *"$package_path"* ]]; then
continue
fi

# On the production branch, publish only if there's a version change
if [ "${{ github.head_ref }}" == "production" ]; then
version_changed=$(git diff HEAD^ HEAD -- "${package_path}/package.json" | grep '"version":' || true)
if [ "$version_changed" ]; then
continue
fi
fi

modified_packages+="$package,"
done

# Format the string of modified packages as an array for matrix consumption
# Example: ["hop-node", "v2-explorer-backend"]
modified_packages="${modified_packages%,}"
modified_packages=$(echo $modified_packages | tr ',' '\n' | sed 's/^ *//;s/ *$//' | jq -R -s -c 'split("\n")[:-1]')

echo "modified_packages=$modified_packages" >> $GITHUB_OUTPUT
Loading

0 comments on commit dafa3c5

Please sign in to comment.