node_version optional #5
This file contains 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: Build and Release | ||
on: | ||
workflow_call: | ||
inputs: | ||
node_version: | ||
required: false | ||
type: string | ||
default: '18.x' | ||
package_manager: | ||
required: false | ||
type: string | ||
default: 'yarn' | ||
skip_ci: | ||
required: false | ||
type: boolean | ||
default: false | ||
commit_sha: | ||
required: true | ||
type: string | ||
permissions: | ||
contents: write | ||
packages: write | ||
jobs: | ||
validate-commit: | ||
name: 'Validate Commit' | ||
runs-on: buildjet-4vcpu-ubuntu-2204 | ||
outputs: | ||
is_merge_commit: ${{ steps.check_merge_commit.outputs.is_merge_commit }} | ||
steps: | ||
- name: Checkout minimal depth | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 1 | ||
token: ${{ secrets.GHCR_TOKEN }} | ||
ref: ${{ inputs.commit_sha }} | ||
- name: Check if commit is a merge from main/master to develop | ||
id: check_merge_commit | ||
run: | | ||
if git log -1 --pretty=%B | grep -qE 'Merge (branch|pull request) .* into develop'; then | ||
echo "is_merge_commit=true" >> $GITHUB_OUTPUT | ||
else | ||
echo "is_merge_commit=false" >> $GITHUB_OUTPUT | ||
fi | ||
release: | ||
name: 'Semantic Release NodeJS' | ||
runs-on: buildjet-4vcpu-ubuntu-2204 | ||
needs: validate-commit | ||
if: ${{ needs.validate-commit.outputs.is_merge_commit == 'false' }} | ||
strategy: | ||
matrix: | ||
node-version: [ ${{ inputs.node_version }} ] | ||
outputs: | ||
NEW_TAG: ${{ steps.tag-version-non-develop.outputs.NEW_TAG || steps.tag-version-develop.outputs.NEW_TAG }} | ||
steps: | ||
- name: Checkout Repository | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ inputs.commit_sha }} | ||
fetch-depth: '0' | ||
token: ${{ secrets.GHCR_TOKEN }} | ||
- name: Setup pnpm | ||
uses: pnpm/action-setup@v2 | ||
if: ${{ inputs.package_manager == 'pnpm' }} | ||
with: | ||
version: 9.0 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
cache: ${{ inputs.package_manager }} | ||
- name: Install dependencies yarn | ||
if: ${{ inputs.package_manager == 'yarn' }} | ||
run: yarn install --frozen-lockfile | ||
- name: Install dependencies pnpm | ||
if: ${{ inputs.package_manager == 'pnpm' }} | ||
run: pnpm install --frozen-lockfile | ||
- name: Install dependencies npm | ||
if: ${{ inputs.package_manager == 'npm' }} | ||
run: npm install --frozen-lockfile | ||
- name: Run Build pnpm | ||
if: ${{ !inputs.skip_ci && inputs.package_manager == 'pnpm' }} | ||
run: pnpm build | ||
- name: Run Build yarn | ||
run: yarn build | ||
if: ${{ !inputs.skip_ci && inputs.package_manager == 'yarn' }} | ||
- name: Run build npm | ||
run: npm run build | ||
if: ${{ !inputs.skip_ci && inputs.package_manager == 'npm' }} | ||
- name: Semantic Release | ||
run: npx semantic-release | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GHCR_TOKEN }} | ||
- name: Get release tag version (non-develop) | ||
if: ${{ github.ref != 'refs/heads/develop' }} | ||
id: tag-version-non-develop | ||
run: | | ||
# Fetch the latest tag | ||
LATEST_TAG=$(git tag --sort=v:refname | tail -n 1) | ||
# Remove the '-develop.<number>' suffix if it exists | ||
NEW_TAG=$(echo "$LATEST_TAG" | sed -E 's/-develop\.[0-9]+$//') | ||
echo "NEW_TAG=$NEW_TAG" >> "$GITHUB_OUTPUT" | ||
- name: Get release tag version (develop) | ||
if: ${{ github.ref == 'refs/heads/develop' }} | ||
id: tag-version-develop | ||
run: | | ||
# Fetch the latest tag | ||
LATEST_TAG=$(git tag --sort=v:refname | tail -n 1) | ||
echo "NEW_TAG=$LATEST_TAG" >> "$GITHUB_OUTPUT" | ||
release-docker: | ||
name: 'Build and Release Docker Image' | ||
runs-on: buildjet-4vcpu-ubuntu-2204 | ||
needs: release | ||
env: | ||
IMAGE_NAME: ${{ github.repository }}:${{ needs.release.outputs.NEW_TAG }} | ||
steps: | ||
- name: Login to GitHub Packages | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GHCR_TOKEN }} | ||
- name: Build and push Docker image | ||
uses: docker/build-push-action@v5 | ||
with: | ||
file: ./Dockerfile | ||
push: true | ||
tags: ghcr.io/${{ env.IMAGE_NAME }} | ||
branch-mapping: | ||
name: 'Branch to Environment' | ||
runs-on: buildjet-4vcpu-ubuntu-2204 | ||
outputs: | ||
environment: ${{ steps.environment.outputs.environment }} | ||
steps: | ||
- name: Branch Mapping | ||
id: environment | ||
run: | | ||
if [ "${{ github.ref }}" = "refs/heads/develop" ]; then | ||
echo "environment=devnet" >> "$GITHUB_OUTPUT" | ||
elif [ "${{ github.ref }}" = "refs/heads/main" ] || [ "${{ github.ref }}" = "refs/heads/master" ]; then | ||
echo "environment=testnet" >> "$GITHUB_OUTPUT" | ||
else | ||
echo "::error::Unsupported branch" | ||
exit 1 | ||
fi | ||
release-helm: | ||
name: 'Release Helm Chart' | ||
needs: [release, release-docker, branch-mapping] | ||
uses: ixofoundation/ixo-github-actions/.github/workflows/node-ci-deploy.yml@main | ||
secrets: inherit | ||
with: | ||
environment: ${{ needs.branch-mapping.outputs.environment }} | ||
version: ${{ needs.release.outputs.NEW_TAG }} | ||
# Notes for Deployment | ||
# TestNet will be deployed using the latest release image ^ | ||
# Main will require deployment review and if approved use the latest release image ^ |