Skip to content

Commit

Permalink
FABN-1386: Doc publishing fixes (#40)
Browse files Browse the repository at this point in the history
- Reference credentials in pipeline script
- Git and find usage tweaked for cross-platform compatibility
- Ensure publishing script fails fast on any errors
- Add some additional information output
- Correct target URL for the root index.html redirect
- Replace all root publish files if building master branch

Signed-off-by: Mark S. Lewis <[email protected]>
  • Loading branch information
bestbeforetoday authored and andrew-coleman committed Nov 28, 2019
1 parent fb17112 commit b4b04ad
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 93 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ trim_trailing_whitespace = false
[*.{yml,yaml}]
indent_style = space
indent_size = 2

[*.sh]
indent_style = space
indent_size = 4
17 changes: 10 additions & 7 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pool:
vmImage: 'ubuntu-18.04'

variables:
SOFTHSM2_CONF: "$(Build.Repository.LocalPath)/test/fixtures/hsm/softhsm2.conf"
- group: credentials
- name: SOFTHSM2_CONF
value: "$(Build.Repository.LocalPath)/test/fixtures/hsm/softhsm2.conf"

steps:
- task: NodeTool@0
Expand All @@ -35,12 +37,12 @@ steps:
- script: npx gulp run-test-all
displayName: 'Run tests'

#- script: scripts/ci_scripts/azurePublishNpmPackages.sh
# condition: and(succeeded(), eq(variables['Build.Reason'], 'IndividualCI'))
# displayName: 'Publish npm packages'
# env:
# NPM_TOKEN: $(NPM)
# PROJECT_DIR: "$(Build.Repository.LocalPath)"
- script: scripts/ci_scripts/azurePublishNpmPackages.sh
condition: and(succeeded(), eq(variables['Build.Reason'], 'IndividualCI'))
displayName: 'Publish npm packages'
env:
NPM_TOKEN: $(NPM)
PROJECT_DIR: "$(Build.Repository.LocalPath)"

- script: scripts/ci_scripts/azurePublishApiDocs.sh
condition: and(succeeded(), eq(variables['Build.Reason'], 'IndividualCI'))
Expand All @@ -51,3 +53,4 @@ steps:
PUBLISH_URL: "https://$(GITHUB-PAT)@github.com/hyperledger/fabric-sdk-node.git"
PROJECT_DIR: "$(Build.Repository.LocalPath)"
STAGING_DIR: "$(Build.StagingDirectory)/gh-pages"
SOURCE_BRANCH: "$(Build.SourceBranchName)"
14 changes: 2 additions & 12 deletions build/tasks/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,9 @@ const jsdoc = require('gulp-jsdoc3');
const fs = require('fs-extra');
const path = require('path');
const replace = require('gulp-replace');
let currentBranch = process.env.GERRIT_BRANCH;

if (!currentBranch) {
currentBranch = 'master';
}
let docsRoot;
if (process.env.DOCS_ROOT) {
docsRoot = process.env.DOCS_ROOT;
} else {
docsRoot = './docs/gen';
}
const currentBranch = process.env.BUILD_BRANCH || 'master';
const docsRoot = process.env.DOCS_ROOT || './docs/gen';

gulp.task('clean', () => {
return fs.removeSync(path.join(docsRoot, currentBranch));
Expand Down Expand Up @@ -58,7 +50,6 @@ gulp.task('docs-dev', ['docs'], () => {


gulp.task('docs', ['jsdocs'], () => {
const relativePath = '..';
const packageJson = require(path.join(__dirname, '../..', 'package.json'));

// jsdocs produced
Expand All @@ -67,7 +58,6 @@ gulp.task('docs', ['jsdocs'], () => {
if (currentBranch === 'master') {
gulp.src('./docs/redirectTemplates/*.html')
.pipe(replace('LATEST__VERSION', packageJson.docsLatestVersion))
.pipe(replace('RELATIVE__PATH', relativePath))
.pipe(gulp.dest(docsRoot));
} else { // eslint-disable-next-line
console.log(`Not updating or routing logic, as not master branch - it is ${currentBranch}`);
Expand Down
2 changes: 1 addition & 1 deletion docs/redirectTemplates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<meta http-equiv="Refresh" content="0; url=RELATIVE__PATH/LATEST__VERSION/index.html">
<meta http-equiv="Refresh" content="0; url=LATEST__VERSION/index.html">
29 changes: 22 additions & 7 deletions scripts/ci_scripts/azurePublishApiDocs.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#!/bin/bash -e
#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#

set -e -o pipefail

# Input environment variables:
: "${GITHUB_USER:?}" # The GitHub user name for publishing
: "${GITHUB_EMAIL:?}" # Email address of the GitHub user
: "${PUBLISH_URL:?}" # Git URL used to push published content
: "${PROJECT_DIR:?}" # Root directory for the Git project
: "${STAGING_DIR:?}" # Directory used to store content to publish to GitHub Pages
: "${SOURCE_BRANCH:?}" # Source code branch name

readonly COMMIT_HASH=$(git rev-parse HEAD)
readonly BUILD_DIR="${PROJECT_DIR}/docs/gen"
Expand All @@ -19,7 +22,7 @@ readonly DOCS_BRANCH='gh-pages'
prepareStaging() {
echo "Preparing staging directory: ${STAGING_DIR}"
rm -rf "${STAGING_DIR}"
rsync -r --exclude-from=${PROJECT_DIR}/.gitignore "${PROJECT_DIR}/" "${STAGING_DIR}"
rsync -r --exclude-from="${PROJECT_DIR}/.gitignore" "${PROJECT_DIR}/" "${STAGING_DIR}"
(cd "${STAGING_DIR}" && _stagingGitSetUp)
}

Expand All @@ -34,26 +37,37 @@ _stagingGitSetUp() {
}

buildDocs() {
echo 'Building documentation'
echo "Building documentation for ${SOURCE_BRANCH} branch in ${BUILD_DIR}"
rm -rf "${BUILD_DIR}"
npx gulp docs
BUILD_BRANCH="${SOURCE_BRANCH}" DOCS_ROOT="${BUILD_DIR}" npx gulp docs
}

copyToStaging() {
echo "Copying built documentation from ${BUILD_DIR} to ${STAGING_DIR}"
cleanStaging
cp -r "${BUILD_DIR}"/* "${STAGING_DIR}"
rsync -r "${BUILD_DIR}/" "${STAGING_DIR}"
}

cleanStaging() {
local releaseDir targetDir
# Remove release sub-directories that have been re-built
find "${BUILD_DIR}" -type d -maxdepth 1 -depth 1 -print | while read -r subdir; do
find "${BUILD_DIR}" -type d -maxdepth 1 -mindepth 1 -print | while read -r subdir; do
releaseDir=$(basename "${subdir}")
targetDir="${STAGING_DIR}/${releaseDir}"
echo "Removing ${targetDir}"
rm -rf "${targetDir}"
done

if [[ ${SOURCE_BRANCH} = master ]]; then
removeStagingRootFiles
fi
}

removeStagingRootFiles() {
local rootFile
find "${STAGING_DIR}" -type f -maxdepth 1 -mindepth 1 -print | while read -r rootFile; do
echo "Removing ${rootFile}"
rm -f "${rootFile}"
done
}

publishDocs() {
Expand All @@ -69,5 +83,6 @@ _stagingPushDocs() {

prepareStaging
buildDocs
cleanStaging
copyToStaging
publishDocs
151 changes: 85 additions & 66 deletions scripts/ci_scripts/azurePublishNpmPackages.sh
Original file line number Diff line number Diff line change
@@ -1,89 +1,108 @@
#!/bin/bash -e
#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#

set -e -o pipefail

# Input environment variables:
: "${NPM_TOKEN:?}" # The npm publishing auth token
: "${PROJECT_DIR:?}" # The project root directory

readonly NODE_MODULES="fabric-protos fabric-common fabric-ca-client fabric-client fabric-network"

npmPublish() {
if [[ "$CURRENT_TAG" = *"skip"* ]]; then
echo -e "\033[34m----> Don't publish $1 npm modules on skip tag \033[0m"
elif [[ "$CURRENT_TAG" = *"unstable"* ]]; then
echo
# Get the current unstable version of a module from npm registry
UNSTABLE_VER=$(npm dist-tags ls "$1" | awk "/$CURRENT_TAG"":"/'{
ver=$NF
rel=$NF
sub(/.*\./,"",rel)
sub(/\.[[:digit:]]+$/,"",ver)
print ver"."rel+1}')
if [[ $UNSTABLE_VER = "" ]]; then
publishAllPackages() {
local module moduleDir
for module in ${NODE_MODULES}; do
moduleDir="${PROJECT_DIR}/${module}"
if [ -d "${moduleDir}" ]; then
echo -e "\033[32m Publishing ${module} \033[0m"
(cd "${moduleDir}" && publishPackage "${module}")
fi
done
}

publishPackage() {
readPackageVersion

if [[ ${CURRENT_TAG} = *"skip"* ]]; then
echo -e "\033[34m----> Don't publish $1 npm modules on skip tag \033[0m"
return
fi

configureNpm

if [[ ${CURRENT_TAG} = *"unstable"* ]]; then
publishUnstablePackage "$1"
else
publishReleasePackage "$1"
fi
}

readPackageVersion() {
# Get the unstable tag from package.json
CURRENT_TAG=$(grep '"tag":' package.json | cut -d\" -f4)
echo -e "\033[32m ======> Current TAG: ${CURRENT_TAG} \033[0m"
# Get the version from package.json
RELEASE_VERSION=$(grep '"version":' package.json | cut -d\" -f4)
echo -e "\033[32m ======> Current Version: ${RELEASE_VERSION} \033[0m"
}

configureNpm() {
echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > '.npmrc'
}

publishUnstablePackage() {
local module
# Get the current unstable version of a module from npm registry
UNSTABLE_VER=$(npm dist-tags ls "$1" | awk "/${CURRENT_TAG}:"/'{
ver=$NF
rel=$NF
sub(/.*\./,"",rel)
sub(/\.[[:digit:]]+$/,"",ver)
print ver"."rel+1
}')
if [[ -z ${UNSTABLE_VER} ]]; then
echo -e "\033[34m ----> unstable ver is blank \033[0m"
UNSTABLE_INCREMENT=1
else
else
# Get last digit of the unstable version built above
UNSTABLE_INCREMENT=$(echo $UNSTABLE_VER| rev | cut -d '.' -f 1 | rev)
fi
echo -e "\033[32m======> UNSTABLE_INCREMENT:" $UNSTABLE_INCREMENT "\033[0m"
# Append last digit with the package.json version
export UNSTABLE_INCREMENT_VERSION=$RELEASE_VERSION.$UNSTABLE_INCREMENT
echo -e "\033[32m======> UNSTABLE_INCREMENT_VERSION:" $UNSTABLE_INCREMENT_VERSION "\033[0"
for module in ${NODE_MODULES}; do
sed -i "s/\"${module}\": \".*\"/\"${module}\": \"${CURRENT_TAG}\"/" package.json
done

# Replace existing version with $UNSTABLE_INCREMENT_VERSION
sed -i 's/\(.*\"version\"\: \"\)\(.*\)/\1'$UNSTABLE_INCREMENT_VERSION\"\,'/' package.json
# Show Version after modify the package.json with latest version to publish
grep '"version":' package.json | cut -d\" -f4
# Publish unstable versions to npm registry
npm publish --tag $CURRENT_TAG
if [ $? != 0 ]; then
echo -e "\033[31m FAILED to publish $CURRENT_TAG of $1 npm module" "\033[0m"
exit 1
fi
echo -e "\033[32m ========> PUBLISHED $CURRENT_TAG tag of $1 npm module SUCCESSFULLY" "\033[0m"
UNSTABLE_INCREMENT=$(echo "${UNSTABLE_VER}" | rev | cut -d '.' -f 1 | rev)
fi
echo -e "\033[32m======> UNSTABLE_INCREMENT: ${UNSTABLE_INCREMENT} \033[0m"
# Append last digit with the package.json version
export UNSTABLE_INCREMENT_VERSION="${RELEASE_VERSION}.${UNSTABLE_INCREMENT}"
echo -e "\033[32m======> UNSTABLE_INCREMENT_VERSION: ${UNSTABLE_INCREMENT_VERSION} \033[0m"
for module in ${NODE_MODULES}; do
sed -i "s/\"${module}\": \".*\"/\"${module}\": \"${CURRENT_TAG}\"/" package.json
done

# Replace existing version with $UNSTABLE_INCREMENT_VERSION
sed -i 's/\(.*\"version\"\: \"\)\(.*\)/\1'"${UNSTABLE_INCREMENT_VERSION}"\"\,'/' package.json
# Show Version after modify the package.json with latest version to publish
grep '"version":' package.json | cut -d\" -f4
# Publish unstable versions to npm registry
npmPublish "$1"
}

else
# Publish node modules on latest tag
echo -e "\033[32m ========> PUBLISH $RELEASE_VERSION" "\033[0m"
publishReleasePackage() {
local module
echo -e "\033[32m ========> PUBLISH $RELEASE_VERSION \033[0m"
for module in ${NODE_MODULES}; do
sed -i "s/\"${module}\": \".*\"/\"${module}\": \"${CURRENT_TAG}\"/" package.json
sed -i "s/\"${module}\": \".*\"/\"${module}\": \"${CURRENT_TAG}\"/" package.json
done

npm publish --tag $CURRENT_TAG
if [ $? != 0 ]; then
echo -e "\033[31m FAILED TO PUBLISH $CURRENT_TAG of $1 npm module" "\033[0m"
exit 1
fi
echo -e "\033[32m ========> PUBLISHED $CURRENT_TAG tag of $1 npm module SUCCESSFULLY" "\033[0m"
fi
npmPublish "$1"
}

versions() {
# Get the unstable tag from package.json
CURRENT_TAG=$(grep '"tag":' package.json | cut -d\" -f4)
echo -e "\033[32m ======> Current TAG: $CURRENT_TAG" "\033[0m"
# Get the version from package.json
RELEASE_VERSION=$(grep '"version":' package.json | cut -d\" -f4)
echo -e "\033[32m ======> Current Version: $RELEASE_VERSION" "\033[0m"
npmPublish() {
npm publish --tag "${CURRENT_TAG}" || {
echo -e "\033[31m FAILED to publish ${CURRENT_TAG} of $1 npm module \033[0m"
exit 1
}
echo -e "\033[32m ========> PUBLISHED ${CURRENT_TAG} tag of $1 npm module SUCCESSFULLY \033[0m"
}

# Set NPM_TOKEN from CI configuration
# Please post in #ci-pipeline channel if you observe npm_token issue
npm config set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}"

# Publish node modules
for module in ${NODE_MODULES}; do
moduleDir="${PROJECT_DIR}/${module}"
if [ -d "${moduleDir}" ]; then
echo -e "\033[32m Publishing ${module}" "\033[0m"
(cd "${moduleDir}" && versions && npmPublish "${module}")
fi
done
publishAllPackages

0 comments on commit b4b04ad

Please sign in to comment.