Skip to content

Commit

Permalink
Merge pull request #1 from valtech-commerce/feature/implement-gha-pub…
Browse files Browse the repository at this point in the history
…lish-npmjs

Feature/implement gha publish npmjs
  • Loading branch information
pleminh committed Aug 25, 2023
2 parents 3c18b6b + 6ff2f38 commit d82fac5
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/deploy-npmjs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Adobe - GitHub Actions - Continuous Deployment (CD) - Deploy packages to npmjs

on:
push:
branches:
- main

jobs:
call_workflow_setup:
name: Setup environment
uses: ./.github/workflows/reusable-workflow-setup.yml
with:
branch-ref: ${{ github.head_ref }}
secrets:
auth-token: ${{ secrets.CA_MTLA_VALTECH_ADOBE_PRACTICE_NPMJS_TOKEN }}
call_workflow_build:
name: Build project
needs: [ call_workflow_setup ]
uses: ./.github/workflows/reusable-workflow-build.yml
call_workflow_publish:
name: Publish packages
needs: [ call_workflow_build ]
uses: ./.github/workflows/reusable-workflow-publish.yml
secrets:
auth-token: ${{ secrets.CA_MTLA_VALTECH_ADOBE_PRACTICE_NPMJS_TOKEN }}
30 changes: 30 additions & 0 deletions .github/workflows/reusable-workflow-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Adobe reusable workflows - Build to npmjs

on:
workflow_call:

jobs:
build:
name: Build project
runs-on: ubuntu-latest
steps:
- name: Fetch checkout-repository-cache
uses: actions/cache@v3
id: checkout-repository-cache
with:
path: ./*
key: checkout-repository-cache-${{ github.sha }}

# If we need to have some build process, put here
# - name: Build project via 'ci:build' npm script
# shell: bash
# run: npm run ci:build

- name: Create build-cache
uses: actions/cache@v3
id: build-cache
with:
path: |
./*
!./node_modules
key: build-cache-${{ github.sha }}
32 changes: 32 additions & 0 deletions .github/workflows/reusable-workflow-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Adobe reusable workflows - Publish to npmjs

on:
workflow_call:
secrets:
auth-token:
required: true

jobs:
publish:
name: Publish packages
runs-on: ubuntu-latest

steps:
- name: Fetch build-cache
uses: actions/cache@v3
id: build-cache
with:
path: |
./*
!./node_modules
key: build-cache-${{ github.sha }}

- name: Publish to registry
shell: bash
run: |
OPTS=();
if [[ $(npm pkg get version) == *"-"* ]]; then OPTS+=('--tag=next') ; fi
if [[ $(npm pkg get workspaces) != "{}" ]]; then OPTS+=('--workspaces') ; fi
npm publish $OPTS --access=public
env:
NODE_AUTH_TOKEN: ${{ secrets.auth-token }}
88 changes: 88 additions & 0 deletions .github/workflows/reusable-workflow-setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Adobe reusable workflows - Setup to npmjs

on:
workflow_call:
inputs:
branch-ref:
required: true
type: string
node-version:
required: false
type: string
default: 16
os:
required: false
type: string
default: ubuntu-latest
jfrog-artifactory:
required: false
type: boolean
default: false
secrets:
github-token:
required: false
scope:
required: false
registry-url:
required: false
auth-token:
required: false

env:
create-npmrc: ${{ secrets.scope != '' || secrets.registry-url != '' || secrets.auth-token != '' }}
use-global-registry: ${{ secrets.scope == '' && secrets.registry-url != '' }}
use-scope-registry: ${{ secrets.scope != '' && secrets.registry-url != '' }}
use-standard-authentication: ${{ secrets.auth-token != '' && inputs.jfrog-artifactory == false }}
use-jfrog-artifactory-authentication: ${{ secrets.auth-token != '' && inputs.jfrog-artifactory == true }}

jobs:
setup:
name: Setup for Node.js ${{ inputs.node-version }}
runs-on: ${{ inputs.os }}

steps:
- name: Checkout repository ${{ inputs.branch-ref }}
uses: actions/checkout@v3
with:
ref: ${{ inputs.branch-ref }}
token: ${{ secrets.github-token || github.token }}

- name: Install Node.js ${{ inputs.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ inputs.node-version }}

# Generate .npmrc manually so it part of the cache and thus accessible to other reusable workflows
- name: Generate .npmrc
if: ${{ env.create-npmrc == 'true' }}
shell: bash
run: |
SCOPE=${{ secrets.scope }}
REGISTRY=$([[ "${{ secrets.registry-url }}" != "" ]] && echo "${{ secrets.registry-url }}" || echo "https://registry.npmjs.org/")
AUTH=${{ secrets.auth-token }}
if [ "${{ env.use-global-registry }}" == "true" ]; then
echo "registry=${REGISTRY}" >> .npmrc;
fi
if [ "${{ env.use-scope-registry }}" == "true" ]; then
echo "${SCOPE}:registry=${REGISTRY}" >> .npmrc;
fi
if [ "${{ env.use-standard-authentication }}" == "true" ]; then
echo "//${REGISTRY##*//}:_authToken=${AUTH}" >> .npmrc
fi
if [ "${{ env.use-jfrog-artifactory-authentication }}" == "true" ]; then
echo "//${REGISTRY##*//}:_auth=${AUTH}" >> .npmrc
fi
- name: Install dependencies
run: npm ci

- name: Create cache
uses: actions/cache@v3
id: checkout-repository-cache
with:
path: ./*
key: checkout-repository-cache-${{ github.sha }}

0 comments on commit d82fac5

Please sign in to comment.