From 2b33015277f8c48a711f4dcc1d0b815d1aeb4551 Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Fri, 21 Feb 2025 12:16:38 +0300 Subject: [PATCH 1/6] Added Jenkinsfile --- Jenkinsfile | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..2fe1337 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,83 @@ +pipeline { + agent any + environment { + REGISTRY_NAME = 'registry.entcor/trust-module' + IMAGE_NAME = "prometheus-exporter" + GITEA_REPOSITORY_URL = "http://git.entcor/api/v1/repos/" + } + + stages { + stage('Init variables') { + when { + expression(env.CHANGE_BRANCH?.startsWith('rc')) + } + steps { + script { + env.IMAGE_TAG = sh(script: "git describe --tags --abbrev=0", returnStdout: true).trim() + } + } + } + stage('Build tagged image and run tests') { + when { + expression(env.CHANGE_BRANCH?.startsWith('rc')) + } + steps { + script { + def image = docker.build("${env.IMAGE_NAME}:${env.IMAGE_TAG}") + + try { + sh "docker run --rm ${env.IMAGE_NAME}:${env.IMAGE_TAG} cargo test" + sh "docker tag ${env.IMAGE_NAME}:${env.IMAGE_TAG} ${env.REGISTRY_NAME}/${env.IMAGE_NAME}:${env.IMAGE_TAG}" + } catch (Exception e) { + error("Tests failed: ${e.message}") + } + } + } + } + stage ('Push docker image to registry') { + when { + expression { env.CHANGE_BRANCH?.startsWith('rc') } + } + steps { + script { + docker.withRegistry('https://registry.entcor/harbor/', 'harbor-credentials-id') { + docker.image("${env.REGISTRY_NAME}/${env.IMAGE_NAME}:${env.IMAGE_TAG}").push() + } + } + } + } + } + post { + always { + script { + echo "Cleaning up workspace..." + sh "rm -rf ${env.WORKSPACE}/package/ || true" + sh "rm -rf ${env.WORKSPACE}/rc/ || true" + } + } + success { + script { + if (env.CHANGE_BRANCH?.startsWith('rc')) { + echo "Attempting to merge PR ${env.CHANGE_ID} into master..." + withCredentials([usernamePassword(credentialsId: 'gitea_creds', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) { + def prId = env.CHANGE_ID + sh """ + curl -X POST \ + -u "${GITEA_USER}:${GITEA_PASS}" \ + -H "Content-Type: application/json" \ + -d '{"do":"merge"}' \ + http://git.entcor/api/v1/repos/deployer3000/trust-module-frontend/pulls/${prId}/merge + """ + echo "PR ${prId} merged successfully into master!" + } + } + } + } + failure { + echo "Pipeline failed. Check the logs for details." + } + aborted { + echo "Pipeline was aborted." + } + } +} \ No newline at end of file From 358e6cf50970f2a7787ed3b7bfa222f75153093c Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Fri, 21 Feb 2025 12:16:57 +0300 Subject: [PATCH 2/6] Changed owner to repo --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 2fe1337..af348cd 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -66,7 +66,7 @@ pipeline { -u "${GITEA_USER}:${GITEA_PASS}" \ -H "Content-Type: application/json" \ -d '{"do":"merge"}' \ - http://git.entcor/api/v1/repos/deployer3000/trust-module-frontend/pulls/${prId}/merge + http://git.entcor/api/v1/repos/deployer3000/prometheus-exporter/pulls/${prId}/merge """ echo "PR ${prId} merged successfully into master!" } From beecc698f0a2eb6542bc6547fce59fedf13395e1 Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Fri, 21 Feb 2025 12:18:18 +0300 Subject: [PATCH 3/6] Deleted removing package/ from post pipeline --- Jenkinsfile | 1 - 1 file changed, 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index af348cd..0a17d6b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -51,7 +51,6 @@ pipeline { always { script { echo "Cleaning up workspace..." - sh "rm -rf ${env.WORKSPACE}/package/ || true" sh "rm -rf ${env.WORKSPACE}/rc/ || true" } } From 6ad1b23359a689ec3bcf7f6a514b00421a080f3f Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Fri, 21 Feb 2025 14:27:24 +0300 Subject: [PATCH 4/6] Some changes in dockerfile and jenkinsfile --- Dockerfile | 3 ++- Jenkinsfile | 4 +--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4d634c6..4ddd137 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ RUN apt update && apt install -y musl-tools RUN rustup target add x86_64-unknown-linux-musl COPY . . +RUN cargo test RUN cargo build --release --target=x86_64-unknown-linux-musl FROM alpine:latest @@ -14,4 +15,4 @@ COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/exporter /app/ RUN apk add --no-cache ca-certificates -CMD ["/app/exporter"] +ENTRYPOINT ["/app/exporter"] diff --git a/Jenkinsfile b/Jenkinsfile index 0a17d6b..19c0ce6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -23,10 +23,8 @@ pipeline { } steps { script { - def image = docker.build("${env.IMAGE_NAME}:${env.IMAGE_TAG}") - try { - sh "docker run --rm ${env.IMAGE_NAME}:${env.IMAGE_TAG} cargo test" + def image = docker.build("${env.IMAGE_NAME}:${env.IMAGE_TAG}") sh "docker tag ${env.IMAGE_NAME}:${env.IMAGE_TAG} ${env.REGISTRY_NAME}/${env.IMAGE_NAME}:${env.IMAGE_TAG}" } catch (Exception e) { error("Tests failed: ${e.message}") From df8b603a01935b5351b1016e4de9b87d37acdb78 Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Fri, 21 Feb 2025 14:32:44 +0300 Subject: [PATCH 5/6] Fixed brackets in jenkinsfile --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 19c0ce6..3416175 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -9,7 +9,7 @@ pipeline { stages { stage('Init variables') { when { - expression(env.CHANGE_BRANCH?.startsWith('rc')) + expression{env.CHANGE_BRANCH?.startsWith('rc')} } steps { script { From 1efe122edfc12a7985f25f2d91d46b66129b64a9 Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Fri, 21 Feb 2025 14:35:54 +0300 Subject: [PATCH 6/6] Fixed brackets in jenkinsfile 2 --- Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 3416175..796dc26 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -9,7 +9,7 @@ pipeline { stages { stage('Init variables') { when { - expression{env.CHANGE_BRANCH?.startsWith('rc')} + expression { env.CHANGE_BRANCH?.startsWith('rc') } } steps { script { @@ -19,7 +19,7 @@ pipeline { } stage('Build tagged image and run tests') { when { - expression(env.CHANGE_BRANCH?.startsWith('rc')) + expression { env.CHANGE_BRANCH?.startsWith('rc') } } steps { script {