I created 2 example setups of multibranch pipeline in 2 languages.
For this app, i first created an ec2 instance using terraform. This will be the public site. The new-instance script does all the handling of calling terraform, extracting ip and associating dns for it. Then, i created a sample app that you can find on: https://github.com/cheseo/node-example This example uses only node, and no dependencies. There's also a test.js file that is used to test the app.
I simply used node docker image to test the app on each push. If the test passes, it uses a special curl call to update the production app.
pipeline {
agent { docker { image 'node:24-bookworm' } }
stages {
stage('build'){
steps {
sh 'node test.js'
}
}
stage('deploy') {
steps {
sh 'curl -H "x-terminate: true" https://node.ec2.ashwink.com.np'
}
}
}
}
The agent, which usually means 'how to run this pipeline' is docker. The stages are basically self-explanatory. It uses the shell to execute simple commands. Note that more complex options, like controlling EC2 instances through jenkins is possible. You can automitally delete EC2 instances, create new ones, update, or just about anything.
The steps for the python app is pretty much the same as for node app. The only difference being that it uses python and flask to do the work. The jenkinsfile is different, it uses a custom docker file:
pipeline {
agent { dockerfile true }
stages {
stage('test'){
steps {
sh './test.sh'
}
}
stage('deploy') {
steps {
sh 'curl -H "x-terminate: true" https://python.ec2.ashwink.com.np'
}
}
}
}
The dockerfile used is:
FROM python:3.9-slim # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app RUN apt update RUN apt install -y curl RUN pip3 install flask CMD ["flask", "--app", "main", "run"]
All of these can be found on: https://github.com/cheseo/python-jenkins-example