Skip to content

Instantly share code, notes, and snippets.

@JCotton1123
Last active February 22, 2024 09:11
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 26 You must be signed in to fork a gist
  • Save JCotton1123/a9746529b88c4a07e422cb4b59cbe329 to your computer and use it in GitHub Desktop.
Save JCotton1123/a9746529b88c4a07e422cb4b59cbe329 to your computer and use it in GitHub Desktop.
Sample Jenkinsfile for Python project
pipeline {
options {
buildDiscarder(logRotator(numToKeepStr: '10')) // Retain history on the last 10 builds
ansiColor('xterm') // Enable colors in terminal
timestamps() // Append timestamps to each line
timeout(time: 20, unit: 'MINUTES') // Set a timeout on the total execution time of the job
}
agent {
// Run this job within a Docker container built using Dockerfile.build
// contained within your projects repository. This image should include
// the core runtimes and dependencies required to run the job,
// for example Python 3.x and NPM.
dockerfile { filename 'Dockerfile.build' }
}
stages { // Define the individual processes, or stages, of your CI pipeline
stage('Checkout') { // Checkout (git clone ...) the projects repository
steps {
checkout scm
}
}
stage('Setup') { // Install any dependencies you need to perform testing
steps {
script {
sh """
pip install -r requirements.txt
"""
}
}
}
stage('Linting') { // Run pylint against your code
steps {
script {
sh """
pylint **/*.py
"""
}
}
}
stage('Unit Testing') { // Perform unit testing
steps {
script {
sh """
python -m unittest discover -s tests/unit
"""
}
}
}
stage('Integration Testing') { //Perform integration testing
steps {
script {
sh """
# You have the option to stand up a temporary environment to perform
# these tests and/or run the tests against an existing environment. The
# advantage to the former is you can ensure the environment is clean
# and in a desired initial state. The easiest way to stand up a temporary
# environment is to use Docker and a wrapper script to orchestrate the
# process. This script will handle standing up supporting services like
# MySQL & Redis, running DB migrations, starting the web server, etc.
# You can utilize your existing automation, your custom scripts and Make.
./standup_testing_environment.sh # Name this whatever you'd like
python -m unittest discover -s tests/integration
"""
}
}
}
post {
failure {
script {
msg = "Build error for ${env.JOB_NAME} ${env.BUILD_NUMBER} (${env.BUILD_URL})"
slackSend message: msg, channel: env.SLACK_CHANNEL
}
}
}
pipeline {
options {
buildDiscarder(logRotator(numToKeepStr: '10')) // Retain history on the last 10 builds
ansiColor('xterm') // Enable colors in terminal
timestamps() // Append timestamps to each line
timeout(time: 20, unit: 'MINUTES') // Set a timeout on the total execution time of the job
}
agent any
parameters {
// Select the application or service you want updated. Even if you only have a single
// application or service now its good to plan for the future.
string(name: 'DEPLOY_APP', default: 'force-web', description: 'Deploy this application or service')
// Select the version you want deployed. This could be a semantic version or a
// git reference like a tag, branch, or SHA.
string(name: 'DEPLOY_VER', description: 'Deploy this version of the application or service')
// Select the environment you want updated. Even if you only have a single
// environment now its good to plan for the future.
string(name: 'DEPLOY_ENV', default: 'dev', description: 'Deploy to this environment')
}
stages {
stage('Checkout') { // Checkout the repository containing your deploy automation
steps {
checkout scm
}
}
}
stage('Deploy') {
steps {
// Ansible example
// - Ansible should be preinstalled on the Jenkins servers
// - need to store SSH key in Jenkins that can be used for deployments
withCredentials([sshUserPrivateKey(credentialsId: "deploy-ssh-key", keyFileVariable: 'deploy.pem')]) {
sh """
ansible-playbook \
--key-file ./deploy.pem \
--extra-vars "environment=#{params.DEPLOY_ENV} version=#{params.DEPLOY_VER}" \
deploy_#{params.DEPLOY_APP}.yml
"""
}
}
}
}
post {
success {
msg = "Deploy succeeded for #{params.DEPLOY_APP} #{params.DEPLOY_VER} " +
"to #{params.DEPLOY_ENV} #{ (${env.BUILD_URL})"
slackSend message: msg, channel: env.SLACK_CHANNEL
}
failure {
script {
msg = "Deploy failed for #{params.DEPLOY_APP} #{params.DEPLOY_VER} " +
"to #{params.DEPLOY_ENV} #{ (${env.BUILD_URL})"
slackSend message: msg, channel: env.SLACK_CHANNEL
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment