Skip to content

Instantly share code, notes, and snippets.

@taras
Created November 27, 2019 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taras/6d90198d4839f20cbc3cc42a5829fb18 to your computer and use it in GitHub Desktop.
Save taras/6d90198d4839f20cbc3cc42a5829fb18 to your computer and use it in GitHub Desktop.
jobs:
job_name:
name: Job Name
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Publish Gateway Releases
uses: thefrontside/actions/publish-gateways@master
with:
npm_publish: npm run my-script
directories: gateways services
get_package_version: date +%s
scopes: minkimcello@npm thefrontisde@gpr
env:
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
jobs:
job_name:
name: Job Name
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Publish Gateway Releases
uses: thefrontside/actions/publish-gateways@master
with:
npm_publish: npm run my-script
directories: gateways services
scopes: minkimcello@npm thefrontisde@gpr
env:
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
#!/usr/bin/env bash
set -e
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
NC='\033[0m'
get_package_version=${INPUT_GET_PACKAGE_VERSION:-node -e \"console.log(require('./package.json').version)\"}
sub_directories=()
for directory in ${INPUT_DIRECTORIES[@]}; do
for directories in $(cd $directory && ls -d */); do
sub_directories+=("$directory/$directories")
done;
done;
for skip_directory in ${INPUT_IGNORE[@]}; do
for i in ${!sub_directories[@]}; do
if [ $(echo "${sub_directories[$i]}" | sed -E "s:^$skip_directory.*::") ]; then
:
else
echo -e "${RED}Removing ${YELLOW}${sub_directories[$i]} ${RED}because of ${YELLOW}${skip_directory}${NC}"
unset sub_directories[$i]
fi
done;
done;
function echo_directories(){
echo -e "${GREEN}Directories:${NC}"
for i in ${sub_directories[@]}; do
echo -e " ${YELLOW}- ${RED}$i"
done;
}
function install_with_CLI(){
npm config set unsafe-perm true
# this scoper is for setting registry/read-permission for installing private dependencies
for scope in ${INPUT_SCOPES}; do
registry=$(echo $scope | sed "s:.*@::")
name=$(echo $scope | sed "s:@.*::")
if [ "$registry" = "gpr" ]; then
echo "//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}" >> ~/.npmrc
echo "@$name:registry=https://npm.pkg.github.com/" >> ~/.npmrc
elif [ "$registry" = "npm" ]; then
if [ "${#NPM_AUTH_TOKEN}" -eq "0" ]; then
echo -e "${RED}ERROR: ${YELLOW}NPM_AUTH_TOKEN not detected. Please add your NPM Token to your repository's secrets.${NC}"
echo -e "${BLUE}Tip: ${YELLOW}If you meant to publish to Github Package Registry, you must specify so in the package.json file.${NC}"
exit 1
else
echo "@${name}:registry=https://registry.npmjs.org/" >> ~/.npmrc
echo "//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}" >> ~/.npmrc
fi
fi
done;
if [ -f "yarn.lock" ]; then
echo -e "${YELLOW}Running yarn...${NC}"
yarn
else
echo -e "${YELLOW}Running npm...${NC}"
npm install
fi
}
function publish(){
# this scoper is for publishing individual packages. might overlap with settings in root but this is more explicit and also if there are no scopes specified in the workflow, the commands from this scoper is necessary.
function scoper(){
name=$(echo "`node -e \"console.log(require('./package.json').name)\"`" | sed "s:\/.*::")
gpr_pre_check=$(jq '."publishConfig"|."registry"' ./package.json | sed 's:.*npm.pkg.github.*:true:');
if [ "$gpr_pre_check" = true ]; then
echo -e "${BLUE}Scoping for ${GREEN}Github Package Registry${BLUE} for publishing ${YELLOW}$name${NC}..."
echo "registry=https://npm.pkg.github.com/" >> .npmrc
echo "//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}" >> .npmrc
else
if [ "${#NPM_AUTH_TOKEN}" -eq "0" ]; then
echo -e "${RED}ERROR: ${YELLOW}NPM_AUTH_TOKEN not detected. Please add your NPM Token to your repository's secrets.${NC}"
echo -e "${BLUE}Tip: ${YELLOW}If you meant to publish to Github Package Registry, you must specify so in the package.json file.${NC}"
exit 1
else
echo -e "${BLUE}Scoping for ${GREEN}NPMjs${BLUE} for publishing ${YELLOW}$name${NC}..."
echo "registry=https://registry.npmjs.org/" >> .npmrc
echo "//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}" >> .npmrc
fi
fi
}
function publish_command(){
if [ "${#INPUT_NPM_PUBLISH}" = "0" ]; then
npm publish
else
$INPUT_NPM_PUBLISH
fi
}
for i in ${sub_directories[@]}; do
cd $GITHUB_WORKSPACE/$i
version="`get_package_version`"
package="`node -e \"console.log(require('./package.json').name)\"`"
echo -e "${GREEN}Running publishing process for: ${YELLOW}$package${RED}${YELLOW}$version${NC}"
scoper
if [ -z "$(npm view ${package}@${version})" ]; then
publish_command --access=public
echo -e "${GREEN}Successfully published version ${BLUE}${version}${GREEN} of ${BLUE}${package}${GREEN}!${NC}"
else
echo -e "${RED}Version ${YELLOW}$version${RED} of ${YELLOW}$package${RED} already exists.${NC}"
echo -e "${BLUE}Tip:${YELLOW}To publish the changes of this commit, you must update package version in the package.json file.${NC}"
fi
cd $GITHUB_WORKSPACE
done;
}
echo_directories
install_with_CLI
publish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment