Skip to content

Commit 5cf387e

Browse files
committed
test
1 parent d3f8a82 commit 5cf387e

8 files changed

+369
-0
lines changed

.cirrus/analyze

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
git fetch --unshallow || true
5+
6+
if [ -n "${GITHUB_BASE_BRANCH:-}" ]; then
7+
echo "Fetching base branch: $GITHUB_BASE_BRANCH"
8+
git fetch origin "${GITHUB_BASE_BRANCH}"
9+
fi
10+
11+
if [ -z "${PIPELINE_ID:-}" ]; then
12+
PIPELINE_ID=$BUILD_NUMBER
13+
fi
14+
15+
# Runs the SonarQube scanner with default and additional parameters.
16+
# Usage: run_sonar_scanner [additional_parameters...]
17+
run_sonar_scanner() {
18+
19+
local additional_params=("$@")
20+
21+
# echo "Running SonarQube scanner..."
22+
if [ -z "${SONAR_HOST_URL:-}" ]; then
23+
echo "SONAR_HOST_URL is required for SonarQube scanner"
24+
exit 1
25+
fi
26+
27+
if [ -z "${SONAR_TOKEN:-}" ]; then
28+
echo "SONAR_TOKEN is required for SonarQube scanner"
29+
exit 1
30+
fi
31+
32+
npx sonarqube-scanner -X \
33+
-Dsonar.host.url="$SONAR_HOST_URL" \
34+
-Dsonar.token="$SONAR_TOKEN" \
35+
-Dsonar.analysis.buildNumber="$BUILD_NUMBER" \
36+
-Dsonar.analysis.pipeline="$PIPELINE_ID" \
37+
-Dsonar.analysis.sha1="$GIT_SHA1" \
38+
-Dsonar.analysis.repository="$GITHUB_REPO" \
39+
"${additional_params[@]}"
40+
echo "SonarQube scanner finished"
41+
}

.cirrus/includes/cirrus_error_advices

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
# This script is responsible to provide functions to give advices to end users.
3+
# If during the execution of a re-ci-images base scripts some errors occurs, then some advices might be given
4+
# in order to assist the user to resolve them (when possible).
5+
#
6+
# WARN: Please do not use this script out of re-ci-images base bash scripts.
7+
# (using it in another context might break in future releases)
8+
#
9+
# Usage: source includes/cirrus_error_advices
10+
11+
# Requires the environment variables:
12+
# none are required yet the functions are able to autofix
13+
14+
function is_using_cirrus(){
15+
[ "${CIRRUS_CI:-false}" == "true" ]
16+
}
17+
18+
# CIRRUS_REPO_NAME is provided out of the box by Cirrus CI
19+
function print_invalid_github_token_for_cirrus_advice(){
20+
REPOSITORY_SHORTNAME=${CIRRUS_REPO_NAME:-"REPOSITORY_NAME"}
21+
echo "Tips:"
22+
cat <<EOF
23+
24+
1) Check the Hashicorp Vault policy for this repository in
25+
26+
https://github.com/SonarSource/re-terraform-aws-vault/tree/master/orders
27+
28+
The policy of this repository should contains the following declarations:
29+
30+
$REPOSITORY_SHORTNAME
31+
secrets:
32+
github:
33+
presets: [default]
34+
customs:
35+
- <<: *github_promotion
36+
repositories: [$REPOSITORY_SHORTNAME]
37+
38+
and
39+
40+
github_promotion: &github_promotion
41+
organization: SonarSource
42+
suffix: promotion
43+
description: add a github check containing the build version to the current commit (required by cirrus_promote_maven)
44+
permissions: {statuses: write}
45+
46+
2) Check the .cirrus.yml file:
47+
48+
The token should be declared this way:
49+
50+
GITHUB_TOKEN: VAULT[development/github/token/\${CIRRUS_REPO_OWNER}-\${CIRRUS_REPO_NAME}-promotion token]
51+
"
52+
EOF
53+
}

.cirrus/includes/git_utils

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
# Git-related utility functions
3+
4+
set -euo pipefail
5+
6+
fetch_git_history() {
7+
git fetch --unshallow || true
8+
}
9+
10+
fetch_pr_references() {
11+
if [ -n "${GITHUB_BASE_BRANCH:-}" ]; then
12+
git fetch origin "${GITHUB_BASE_BRANCH}"
13+
fi
14+
}
15+
16+
is_master_branch() {
17+
[[ "${GITHUB_BRANCH}" == "master" ]]
18+
}
19+
20+
is_maintenance_branch() {
21+
[[ "${GITHUB_BRANCH}" == "branch-"* ]]
22+
}
23+
24+
is_pull_request() {
25+
[[ "${PULL_REQUEST:-}" != "false" ]]
26+
}
27+
28+
is_dogfood_branch() {
29+
[[ "${GITHUB_BRANCH}" == "dogfood-on-"* ]]
30+
}
31+
32+
is_long_lived_feature_branch() {
33+
[[ "${GITHUB_BRANCH}" == "feature/long/"* ]]
34+
}

.cirrus/includes/jfrog_utils.sh

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
function extract_module_names() {
5+
artifact=$1
6+
module=$(echo "$artifact" | sed -E "s,^([^/]+/[^/]+/([^/]+))/([^/]+)/(([0-9].)+[0-9]+)/.*$,\1:\3:\4," | sed "s,/,.,g")
7+
echo "$module"
8+
}
9+
10+
function extract_artifacts() {
11+
public_artifacts=()
12+
private_artifacts=()
13+
artifacts=$(grep Installing | sed 's,.*\.m2/repository/,,')
14+
while read -r artifact; do
15+
if [[ $artifact == "org/"* ]]; then
16+
public_artifacts+=("$artifact")
17+
elif [[ $artifact == "com/"* ]]; then
18+
private_artifacts+=("$artifact")
19+
fi
20+
done <<<"$artifacts"
21+
}
22+
23+
function upload_artifacts() {
24+
jfrog config add test --artifactory-url "$ARTIFACTORY_URL" --access-token "$ARTIFACTORY_DEPLOY_PASSWORD"
25+
pushd "${CIRRUS_WORKING_DIR}/.m2/repository/"
26+
for artifact in "${public_artifacts[@]}"; do
27+
echo "Deploying public artifact: $artifact"
28+
module=$(extract_module_names "$artifact")
29+
jfrog rt u --module "$module" --build-name "${CIRRUS_REPO_NAME}" --build-number "${BUILD_ID}" "$artifact" "${ARTIFACTORY_DEPLOY_REPO}"
30+
done
31+
32+
jfrog config edit test --artifactory-url "$ARTIFACTORY_URL" --access-token "$ARTIFACTORY_PRIVATE_DEPLOY_PASSWORD"
33+
for artifact in "${private_artifacts[@]}"; do
34+
echo "Deploying private artifact: $artifact"
35+
module=$(extract_module_names "$artifact")
36+
jfrog rt u --module "$module" --build-name "${CIRRUS_REPO_NAME}" --build-number "${BUILD_ID}" "$artifact" "${ARTIFACTORY_PRIVATE_DEPLOY_REPO}"
37+
done
38+
popd
39+
}

.cirrus/includes/version_util

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
# This script is responsible to provide functions to verify that the specified version follows the Sonar way proposed
3+
# by the RE Team.
4+
#
5+
# Usage: source includes/version_util
6+
7+
# Requires the environment variables:
8+
# none are required yet the functions are able to autofix
9+
10+
11+
# Verify that the version declared in pom.xml or in gradle.properties
12+
# use the following pattern: x.x.x.x (<major>.<minor>.<patch>.<buildNumber>) and warn if not.
13+
# Args:
14+
# $1 The version string to check
15+
function check_version_format(){
16+
local version=$1
17+
local extracted_points="${version//[^.]}"
18+
local point_count=${#extracted_points}
19+
if [[ "$point_count" != 3 ]]; then
20+
echo "WARN: This version $version does not match the standardized format used commonly across the organization: '<MAJOR>.<MINOR>.<PATCH>.<BUILD NUMBER>'."
21+
fi
22+
}

.cirrus/npm_deploy

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Required environment variables
6+
: "${ARTIFACTORY_URL?Environment variable missing}"
7+
: "${ARTIFACTORY_DEPLOY_ACCESS_TOKEN?Environment variable missing}"
8+
9+
jfrog config remove repox > /dev/null 2>&1 # Do not log if the repox config were not present
10+
jfrog config add repox --artifactory-url "$ARTIFACTORY_URL" --access-token "$ARTIFACTORY_DEPLOY_ACCESS_TOKEN"
11+
12+
jfrog_npm_publish() {
13+
14+
local repo_resolve=${1:-npm}
15+
local repo_deploy=${2:-sonarsource-npm-public-qa}
16+
local build_name=${3:-$CIRRUS_REPO_NAME}
17+
local build_number=${4:-$BUILD_NUMBER}
18+
19+
20+
jfrog npm-config --repo-resolve "$repo_resolve" --repo-deploy "$repo_deploy"
21+
jfrog npm publish --build-name="$build_name" --build-number="$build_number"
22+
jfrog rt build-publish "$build_name" "$build_number"
23+
}

.cirrus/npm_version_utils

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
PACKAGE_JSON="package.json"
6+
7+
function get_current_version() {
8+
CURRENT_VERSION=$(jq -r .version "$PACKAGE_JSON")
9+
if [ -z "$CURRENT_VERSION" ] || [ "$CURRENT_VERSION" == "null" ]; then
10+
echo "Could not get version from $PACKAGE_JSON" >&2
11+
exit 1
12+
fi
13+
14+
echo "$CURRENT_VERSION"
15+
}
16+
17+
function set_npm_version_with_build_id() {
18+
local BUILD_ID=$1
19+
20+
CURRENT_VERSION=$(get_current_version)
21+
22+
RELEASE_VERSION=${CURRENT_VERSION%"-SNAPSHOT"}
23+
24+
# In case of 2 digits, we need to add the 3rd digit (0 obviously)
25+
# Mandatory in order to compare versions (patch VS non patch)
26+
DIGIT_COUNT=$(echo "${RELEASE_VERSION//./ }" | wc -w)
27+
if [ "$DIGIT_COUNT" -lt 3 ]; then
28+
RELEASE_VERSION="$RELEASE_VERSION.0"
29+
fi
30+
NEW_VERSION="$RELEASE_VERSION-$BUILD_ID"
31+
32+
echo "Replacing version $CURRENT_VERSION with $NEW_VERSION"
33+
npm version --no-git-tag-version --allow-same-version "$NEW_VERSION"
34+
export PROJECT_VERSION=$NEW_VERSION
35+
}

.cirrus/yarn_build_deploy_analyze

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/bin/bash
2+
# shellcheck source=/dev/null
3+
4+
set -euo pipefail
5+
6+
source "$(dirname "$0")"/includes/git_utils
7+
source "$(dirname "$0")"/includes/version_util
8+
source "$(dirname "$0")"/analyze
9+
source "$(dirname "$0")"/npm_deploy
10+
source "$(dirname "$0")"/npm_version_utils
11+
12+
13+
if is_master_branch && ! is_pull_request; then
14+
echo "On master branch and not a pull request."
15+
16+
git fetch origin "${GITHUB_BRANCH}"
17+
echo "Fetched origin ${GITHUB_BRANCH}."
18+
19+
CURRENT_VERSION=$(get_current_version)
20+
echo "Current version: $CURRENT_VERSION"
21+
22+
set_npm_version_with_build_id "$BUILD_NUMBER"
23+
echo "Set yarn version with build ID: $BUILD_NUMBER."
24+
25+
check_version_format "$PROJECT_VERSION"
26+
echo "Checked version format: $PROJECT_VERSION."
27+
28+
echo "Installing yarn dependencies..."
29+
yarn install --immutable
30+
31+
echo "Running tests..."
32+
yarn test
33+
34+
echo "Running Sonar Scanner..."
35+
run_sonar_scanner \
36+
-Dsonar.projectVersion="$CURRENT_VERSION"
37+
38+
echo "Building project..."
39+
yarn build
40+
41+
echo "Publishing to JFrog..."
42+
jfrog_npm_publish "$@"
43+
44+
elif is_maintenance_branch && not_pull_request; then
45+
git fetch origin "${GITHUB_BRANCH}"
46+
47+
CURRENT_VERSION=$(get_current_version)
48+
49+
if [[ $CURRENT_VERSION =~ "-SNAPSHOT" ]]; then
50+
echo "======= Found SNAPSHOT version ======="
51+
# Do not deploy a SNAPSHOT version but the release version related to this build
52+
set_npm_version_with_build_id "$BUILD_NUMBER"
53+
check_version_format "$CURRENT_VERSION"
54+
else
55+
echo "======= Found RELEASE version ======="
56+
echo "======= Deploy $CURRENT_VERSION ======="
57+
check_version_format "$CURRENT_VERSION"
58+
fi
59+
60+
yarn install --immutable
61+
yarn test
62+
run_sonar_scanner \
63+
-Dsonar.branch.name="$GITHUB_BRANCH"
64+
65+
yarn build
66+
jfrog_npm_publish "$@"
67+
68+
elif is_pull_request; then
69+
# Do not deploy a SNAPSHOT version but the release version related to this build and PR
70+
set_npm_version_with_build_id "$BUILD_NUMBER"
71+
72+
if [ "${DEPLOY_PULL_REQUEST:-}" == "true" ]; then
73+
echo '======= with deploy ======='
74+
75+
check_version_format "$PROJECT_VERSION"
76+
77+
yarn install --immutable
78+
79+
yarn test
80+
run_sonar_scanner \
81+
-Dsonar.analysis.prNumber="$PULL_REQUEST"
82+
83+
yarn build
84+
jfrog_npm_publish "$@"
85+
86+
else
87+
echo '======= no deploy ======='
88+
89+
yarn install --immutable
90+
91+
run_sonar_scanner \
92+
-Dsonar.analysis.prNumber="$PULL_REQUEST"
93+
94+
yarn build
95+
fi
96+
97+
elif is_dogfood_branch && not_pull_request; then
98+
echo '======= Build dogfood branch ======='
99+
100+
CURRENT_VERSION=$(get_current_version)
101+
102+
set_npm_version_with_build_id "$BUILD_NUMBER"
103+
check_version_format "$PROJECT_VERSION"
104+
105+
yarn install --immutable
106+
yarn build
107+
jfrog_npm_publish "$@"
108+
109+
elif is_long_lived_feature_branch && not_pull_request; then
110+
yarn install --immutable
111+
112+
yarn test
113+
run_sonar_scanner \
114+
-Dsonar.branch.name="$GITHUB_BRANCH"
115+
116+
yarn build
117+
118+
else
119+
yarn install --immutable
120+
yarn test
121+
yarn build
122+
fi

0 commit comments

Comments
 (0)