11 Commits

2 changed files with 67 additions and 40 deletions

58
Jenkinsfile vendored
View File

@ -1,19 +1,22 @@
pipeline { pipeline {
agent any agent {
kubernetes {
label 'kubernetes-agent'
yamlFile 'podTemplate.yaml'
}
}
environment { environment {
ACR_NAME = 'marcin00'
CLIENT_ID = 'c302726f-fafb-4143-94c1-67a70975574a'
DOCKER_REGISTRY_URL = 'marcin00.azurecr.io' DOCKER_REGISTRY_URL = 'marcin00.azurecr.io'
DOCKER_IMAGE = "${DOCKER_REGISTRY_URL}/user-microservice:${GIT_COMMIT}" DOCKER_IMAGE = "${DOCKER_REGISTRY_URL}/user-microservice:${GIT_COMMIT}"
ACR_NAME = 'marcin00'
} }
stages { stages {
stage('Checkout') { stage('Code Tests') {
steps { steps {
checkout scm container('python') {
}
}
stage('Test python app') {
steps {
script {
dir('api') { dir('api') {
sh ''' sh '''
python3 -m venv env python3 -m venv env
@ -30,43 +33,18 @@ pipeline {
} }
} }
} }
stage('Build & test docker image') {
steps {
script {
appImage = docker.build("${DOCKER_IMAGE}")
sh label: 'Install dgoss', script: ''' stage('Build & Push Docker') {
curl -s -L https://github.com/aelsabbahy/goss/releases/latest/download/goss-linux-amd64 -o goss
curl -s -L https://github.com/aelsabbahy/goss/releases/latest/download/dgoss -o dgoss
chmod +rx *goss
'''
withEnv(['GOSS_OPTS=-f junit', 'GOSS_PATH=./goss', 'GOSS_SLEEP=3', 'SQLALCHEMY_DATABASE_URI=sqlite:///:memory:']) {
sh label: 'run image tests', script: './dgoss run -e SQLALCHEMY_DATABASE_URI=sqlite:///:memory: ${DOCKER_IMAGE} > goss_junit.xml'
}
}
}
post {
always {
junit testResults: '**/*goss_junit.xml'
}
}
}
stage('Deploy') {
steps { steps {
script { container('docker') {
sh ''' sh '''
az login --identity docker build -t ${DOCKER_IMAGE} .
az acr login --name ${ACR_NAME} az login --identity --client-id ${CLIENT_ID}
docker push ${DOCKER_IMAGE} az acr login --name ${ACR_NAME}
docker push ${DOCKER_IMAGE}
''' '''
} }
} }
} }
} }
post {
cleanup {
script { cleanWs() }
}
}
} }

49
podTemplate.yaml Normal file
View File

@ -0,0 +1,49 @@
apiVersion: v1
kind: Pod
metadata:
labels:
jenkins: "slave"
jenkins/label: "kubernetes-agent"
spec:
containers:
- name: jnlp
image: jenkins/inbound-agent:alpine
tty: false
workingDir: /home/jenkins/agent
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
env:
- name: JENKINS_WEB_SOCKET
value: "true"
- name: REMOTING_OPTS
value: "-noReconnectAfter 1d"
- name: python
image: python:3.11.7-alpine
command:
- cat
tty: true
workingDir: /home/jenkins/agent
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
- name: docker
image: marcin00.azurecr.io/azure-cli-docker:slim-bookworm
tty: true
workingDir: /home/jenkins/agent
securityContext:
privileged: true
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
nodeSelector:
kubernetes.io/os: linux
restartPolicy: Never
volumes:
- name: workspace-volume
emptyDir: {}