Skip to content

Commit 5602744

Browse files
authored
test/smarini/BUILD 6984 modularScripts (#59)
1 parent f71d7b6 commit 5602744

15 files changed

+2527
-10
lines changed

.cirrus.star

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("github.com/SonarSource/cirrus-modules@b1f898b04170567791e1fd75f691f0266aab60af", "load_features") # 3.0.4
1+
load("github.com/SonarSource/cirrus-modules@5cd6425fdb78665f07284f2c12d495618a7bbc0a", "load_features") # 3.1.0
22

33
def main(ctx):
44
return load_features(ctx, only_if=dict())

.cirrus.yml

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ env:
66
CIRRUS_SHELL: bash
77

88
eks_container_definition: &CONTAINER_DEFINITION
9-
image: ${CIRRUS_AWS_ACCOUNT}.dkr.ecr.eu-central-1.amazonaws.com/base:latest
9+
image: ${CIRRUS_AWS_ACCOUNT}.dkr.ecr.eu-central-1.amazonaws.com/base:node20-yarn2.4.3
1010
cluster_name: ${CIRRUS_CLUSTER_NAME}
1111
region: eu-central-1
1212
namespace: default
13-
docker_arguments:
14-
CIRRUS_AWS_ACCOUNT: ${CIRRUS_AWS_ACCOUNT}
1513

1614
build_task:
1715
auto_cancellation: $CIRRUS_BRANCH != 'master' && $CIRRUS_BRANCH != 'stable'
1816
eks_container:
1917
<<: *CONTAINER_DEFINITION
20-
cpu: 2
21-
memory: 2G
18+
builder_role: cirrus-builder
19+
builder_image: docker-builder-v*
20+
builder_instance_type: t2.small
21+
dockerfile: .cirrus/Dockerfile
22+
docker_arguments:
23+
CIRRUS_AWS_ACCOUNT: ${CIRRUS_AWS_ACCOUNT}
2224
env:
2325
ARTIFACTORY_DEPLOY_ACCESS_TOKEN: VAULT[development/artifactory/token/${CIRRUS_REPO_OWNER}-${CIRRUS_REPO_NAME}-qa-deployer access_token]
2426
SONAR_HOST_URL: VAULT[development/kv/data/sonarcloud data.url]
@@ -38,7 +40,7 @@ build_task:
3840

3941
build_script:
4042
- source cirrus-env BUILD
41-
- regular_npm_build_deploy_analyze
43+
- ./.cirrus/npm_build_deploy_analyze
4244

4345
promote_task:
4446
depends_on:

.cirrus/Dockerfile

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
ARG CIRRUS_AWS_ACCOUNT
2+
FROM ${CIRRUS_AWS_ACCOUNT}.dkr.ecr.eu-central-1.amazonaws.com/base:j17-latest
3+
4+
USER root
5+
6+
# Update package list
7+
RUN apt-get update -y
8+
9+
# Install some tools
10+
RUN apt-get install -y jq
11+
12+
# Install vim
13+
RUN apt-get install -y vim
14+
15+
# Install Node.js
16+
ARG NODE_VERSION=20.*
17+
RUN apt-get install -y nodejs=${NODE_VERSION}
18+
19+
# Install Yarn
20+
RUN npm install -g yarn
21+
22+
# Install sonar-scanner-npm
23+
RUN npm install -g sonarqube-scanner
24+
25+
# Clean up cache after packages installation
26+
RUN apt-get clean
27+
28+
USER sonarsource

.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-env

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
3+
set +o verbose
4+
set -euo pipefail
5+
6+
# generic environment variables used by Gradle build
7+
export GIT_SHA1=$CIRRUS_CHANGE_IN_REPO
8+
export GITHUB_BASE_BRANCH=${CIRRUS_BASE_BRANCH:-}
9+
export GITHUB_BRANCH=$CIRRUS_BRANCH
10+
export GITHUB_REPO=${CIRRUS_REPO_FULL_NAME}
11+
export PROJECT=${CIRRUS_REPO_NAME}
12+
export PULL_REQUEST=${CIRRUS_PR:-false}
13+
export PULL_REQUEST_SHA=${CIRRUS_BASE_SHA:-}
14+
export PIPELINE_ID=${CIRRUS_BUILD_ID}
15+
16+
export LANG=C.UTF-8
17+
18+
TASK_TYPE=$1
19+
if [[ "$TASK_TYPE" != "BUILD"* ]]; then
20+
export SONARSOURCE_QA=true
21+
fi
22+
23+
: "${CI_BUILD_NUMBER?missing parameter. See https://xtranet-sonarsource.atlassian.net/wiki/spaces/RE/pages/2992210934}"
24+
BUILD_NUMBER=${CI_BUILD_NUMBER}
25+
BUILD_ID=${CI_BUILD_NUMBER}
26+
echo "BUILD_NUMBER: $BUILD_NUMBER"
27+
export BUILD_NUMBER BUILD_ID
28+
29+
if [[ "${GITHUB_TOKEN:-}" == "ENCRYPTED"* ]]; then
30+
echo "Unsetting GITHUB_TOKEN"
31+
unset GITHUB_TOKEN
32+
fi
33+
34+
M2_SOURCE=~/.m2
35+
if [[ -f "/c/buildTools-docker/mvn/settings-public-auth.xml" ]]; then
36+
# /c/buildTools-docker/mvn is populated during Windows image creation
37+
M2_SOURCE=/c/buildTools-docker/mvn
38+
fi
39+
40+
[[ ! -d ~/.m2 ]] && mkdir ~/.m2
41+
if [[ "${TASK_TYPE}" == *"-PRIVATE" || "${TASK_TYPE}" == "QA"* || "${TASK_TYPE}" == "PROMOTE"* ]]; then
42+
cp "${M2_SOURCE}/settings-private.xml" ~/.m2/settings.xml
43+
else
44+
if [[ -n "${ARTIFACTORY_ACCESS_TOKEN:-}" ]]; then
45+
cp "${M2_SOURCE}/settings-public-auth.xml" ~/.m2/settings.xml
46+
else
47+
echo "No ARTIFACTORY_ACCESS_TOKEN found, Repox will not be accessible".
48+
fi
49+
fi
50+
51+
case $BUILD_ID in
52+
''|*[!0-9]*) echo "$BUILD_ID is not a number" && exit 1 ;;
53+
esac
54+
set -o verbose

.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+
}

0 commit comments

Comments
 (0)