diff --git a/.yamllint b/.yamllint index d48c2d3..573e22b 100644 --- a/.yamllint +++ b/.yamllint @@ -2,5 +2,5 @@ extends: relaxed rules: line-length: - max: 200 + max: 260 allow-non-breakable-inline-mappings: true diff --git a/src/commands/upload_source_maps_to_instana.yml b/src/commands/upload_source_maps_to_instana.yml new file mode 100644 index 0000000..40deac6 --- /dev/null +++ b/src/commands/upload_source_maps_to_instana.yml @@ -0,0 +1,82 @@ +description: > + Upload JS source maps to Instana APM + +parameters: + instana_endpoint: + default: "$INSTANA_ENDPOINT" + type: string + description: Instana API endpoint host + instana_api_token: + default: "$INSTANA_API_TOKEN" + type: string + description: Instana API token + website_id: + description: "Website ID in Instana" + type: string + upload_config_name: + description: "Upload configuration name to be created and cleaned up before every upload" + type: string + assets_path: + default: "dist/assets" + description: "Local path to the folder containing JS files" + type: string + maps_path: + default: "source-maps" + description: "Local path to the folder containing JS source maps" + type: string + js_url: + description: "Remote path to the folder containing JS files" + type: string +steps: + - run: + name: Upload JS source maps to Instana + command: | + # get all file upload configurations for the website + GET_RESPONSE=$(curl -s -X GET "https://<< parameters.instana_endpoint >>/api/website-monitoring/config/<< parameters.website_id >>/sourcemap-upload" -H "Accept: application/json" -H "Authorization: apiToken << parameters.instana_api_token >>") + echo "File Upload configurations for website ID << parameters.website_id >> : $GET_RESPONSE" + UPLOAD_CONFIG_ID=$(echo "$GET_RESPONSE" | jq -r '.configs[] | select(.description == "<< parameters.upload_config_name >>") | .id') + # create config if not found + if [ -z "$UPLOAD_CONFIG_ID" ]; then + echo "No file upload configuration << parameters.upload_config_name >> was found, so creating a new one..." + CREATION_ENDPOINT="https://<< parameters.instana_endpoint >>/api/website-monitoring/config/<< parameters.website_id >>/sourcemap-upload" + # echo "CREATION_ENDPOINT=$CREATION_ENDPOINT" + CREATION_RESPONSE=$(curl -s -X POST $CREATION_ENDPOINT -H "Content-Type: application/json" -H "Accept: application/json" -d '{"description": "<< parameters.upload_config_name >>"}' -H "authorization: apiToken << parameters.instana_api_token >>") + echo "CREATION_RESPONSE=$CREATION_RESPONSE" + UPLOAD_CONFIG_ID=$(echo "$CREATION_RESPONSE" | jq -r '.id') + fi + if [ -z "$UPLOAD_CONFIG_ID" ]; then + echo "Error: UPLOAD_CONFIG_ID is still empty!" + exit 1 + else + echo "UPLOAD_CONFIG_ID=$UPLOAD_CONFIG_ID" + fi + # clear uploads + CLEAR_ENDPOINT="https://<< parameters.instana_endpoint >>/api/website-monitoring/config/<< parameters.website_id >>/sourcemap-upload/$UPLOAD_CONFIG_ID/clear" + # echo "CLEAR_ENDPOINT=$CLEAR_ENDPOINT" + CLEAR_RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}\n" -X PUT $CLEAR_ENDPOINT -H "authorization: apiToken << parameters.instana_api_token >>") + echo "CLEAR_RESPONSE_CODE=$CLEAR_RESPONSE_CODE" + if [ "$CLEAR_RESPONSE_CODE" -eq 204 ]; then + echo "Request to clear source map files for << parameters.upload_config_name >> was successful." + else + echo "Request to clear source map files for << parameters.upload_config_name >> failed with status code: $CLEAR_RESPONSE_CODE" + exit 1 + fi + # upload files + echo "########### JS files ##########" + ls -al << parameters.assets_path >>/*.js + if ls << parameters.assets_path >>/*.js.map >/dev/null 2>&1; then + echo "Error: << parameters.assets_path >>/*.js.map files still exist! Move them to the << parameters.maps_path >>/ directory on the same level like your local path to sync." + exit 1 + fi + echo "########### JS source map files ##########" + ls -al << parameters.maps_path >>/*.js.map + UPLOAD_ENDPOINT="https://<< parameters.instana_endpoint >>/api/website-monitoring/config/<< parameters.website_id >>/sourcemap-upload/$UPLOAD_CONFIG_ID/form" + # echo "UPLOAD_ENDPOINT=$UPLOAD_ENDPOINT" + for file in << parameters.assets_path >>/*.js; do + filename=$(basename "$file") + MAP_LOCATION="<< parameters.maps_path >>/${filename}.map" + JS_URL="<< parameters.js_url >>/$filename" + echo "JS_URL=$JS_URL \nMAP_LOCATION=$MAP_LOCATION" + UPLOAD_RESULT=$(curl -s -X PUT "$UPLOAD_ENDPOINT" -H "authorization: apiToken << parameters.instana_api_token >>" -F "url=$JS_URL" -F "sourceMap=@$MAP_LOCATION" -H "ContentType: multipart/form-data") + echo "UPLOAD_RESULT=$UPLOAD_RESULT" + done diff --git a/src/jobs/sync_and_invalidate_with_source_maps_upload_to_instana.yml b/src/jobs/sync_and_invalidate_with_source_maps_upload_to_instana.yml new file mode 100644 index 0000000..d35a86c --- /dev/null +++ b/src/jobs/sync_and_invalidate_with_source_maps_upload_to_instana.yml @@ -0,0 +1,129 @@ +description: > + Upload JS source maps to Instana APM + +executor: default + +parameters: + distribution_id: + description: ID of cloudfront distribution + type: string + pattern: + description: Pattern to purge + type: string + default: "/*" + bucket_name: + description: Name of the bucket to sync files to + type: string + local: + description: Local path to sync + type: string + default: "dist/" + remote: + description: Remote path in s3 to sync + type: string + default: "" + additional_args: + default: "" + description: Additional arguments to pass to AWS cli + type: string + attach_workspace: + default: true + description: > + Boolean for whether or not to attach to an existing workspace. + type: boolean + checkout: + default: true + description: > + Boolean for whether or not to checkout sources + type: boolean + workspace_root: + default: . + description: > + Workspace root path that is either an absolute path or a path relative to the working directory. + type: string + aws_access_key_id: + type: env_var_name + description: aws access key id override + default: AWS_ACCESS_KEY_ID + aws_secret_access_key: + type: env_var_name + description: aws secret access key override + default: AWS_SECRET_ACCESS_KEY + aws_iam_web_identity_role_arn: + type: string + description: IAM role of web identity enabled role to access aws resources + default: "none" + aws_region: + type: env_var_name + description: aws region override + default: AWS_REGION + # Instana source maps upload + bucket_key: + description: Name of the bucket key to distinguish between assets to be uploaded to the same bucket + type: string + default: "" + instana_endpoint: + default: "$INSTANA_ENDPOINT" + type: string + description: Instana API endpoint host + instana_api_token: + default: "$INSTANA_API_TOKEN" + type: string + description: Instana API token + # tenant: + # default: "trustedshops" + # description: "Tenant part in the Instana host name" + # type: string + # unit: + # description: "Unit part in the Instana host name, must match stage (dev, test or prod)" + # type: string + website_id: + description: "Website ID in Instana" + type: string + assets_path: + default: "dist/assets" + description: "Local path to the older containing JS files and its source maps" + type: string + javascript_url: + description: "Remote path to the folder containing JS files and its source maps" + type: string +steps: + - when: + condition: <> + steps: + - checkout + - unless: + condition: + and: + - equal: [ <>, "none" ] + steps: + - aws-cli/install + - aws-cli/assume_role_with_web_identity: + role_arn: <> + - when: + condition: + and: + - equal: [ <>, "none" ] + steps: + - aws-cli/setup: + aws_access_key_id: <> + aws_secret_access_key: <> + region: <> + - s3_sync: + bucket_name: <> + local: <> + remote: <> + attach_workspace: <> + workspace_root: <> + additional_args: <> + - upload_source_maps_to_instana: + instana_endpoint: << parameters.instana_endpoint >> + instana_api_token: << parameters.instana_api_token >> + website_id: << parameters.website_id >> + # use bucket_name as upload_config_name + upload_config_name: "<< parameters.bucket_name >><< parameters.bucket_key>>" + assets_path: << parameters.assets_path >> + js_url: << parameters.javascript_url >> + - cloudfront_invalidate: + distribution_id: <> + pattern: <>