How to deploy React Django app automatically on AWS EC2 using Jenkins - jenkins

I am new to Jenkins. I have a project that runs Django and React on the same port which is running on AWS EC2 machine. My Database runs on RDS. Now I am trying to implement pipeline and automatic deployment using Jenkins. All the tutorials I was reading or watching suggest using AWS CodeDeploy service.
This is my sample Jenkinsfile now that tests my React code. As now the main intention is to do automatic code deployment so I am not thinking about testing python here. I just want whatever my code to be deployed on EC2 automatically
pipeline {
agent any
stages {
stage('Install dependency') {
steps {
sh "yarn install"
}
}
stage('Test project') {
steps {
sh "yarn test"
}
}
stage('Build project') {
steps {
sh "yarn build"
}
}
}
}
I know about AWS CodeDeploy plugin on Jenkins. But it uses S3 and AWS Code Deploy. What I am really trying to achieve is, Can I do something like this
I will build my project in Jenkins and it will send my project automatically to the ec2 machine and make it live.
OR
Can I connect to my ec2 instance and do the same that we do manually. Like - Login to instance, fetch the code from git, merge it, build it, restart the service

Related

How to get web access when running DDEV in a Droplet using Jenkins/Jenkinsfile?

I provisioned a droplet with Jenkins/Docker/DDEV, the idea is to have Jenkins to run tests and provide a staging environment for multiple web projects. Each web project is already using DDEV to be used as a local dev environment. Ideally what I want to build is, having Jenkins pull from the repo, and start DDEV on the droplet, providing the staging environment.
So far, I have accomplished all of the above, however, I am a bit stack on the network configuration to allow people visiting my droplet through a web browser to access each Jenkins build.
Below is my Jenkinsfile so far, ideally if I could setup the network in the Jenkinsfile would be brilliant.
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: 'jenkinsSSH', url: 'https://myrepo.git']])
}
}
stage('Example') {
steps {
echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}"
sh 'cd $WORKSPACE && ddev start'
}
}
stage('Release') {
steps {
echo 'Ready to release etc.'
}
}
}
}
I had a look extensively in Google in case someone else has tried a similar endeavour. Also, looked if Jenkins has plugins for this task. I can't access the DDEV through the web browser.
If you want to access a running DDEV project via the web browser, you'll have to open it up with ddev config global --router-bind-all-ports, and then deal with handling name resolution. There are full details about this class of thing in https://ddev.readthedocs.io/en/latest/users/topics/sharing/; you may also be interested in the full sharing solution in https://ddev.readthedocs.io/en/latest/users/topics/hosting/
Also note that there is a GitHub action for this kind of testing that may help you, https://github.com/marketplace/actions/setup-ddev

Playwright Docker Image as Jenkins agent

I am trying to use Playwright docker image in Jenkins. In the official documentation, they give an example of how to use Docker plugin:
pipeline {
agent { docker { image 'mcr.microsoft.com/playwright:v1.25.0-focal' } }
stages {
stage('e2e-tests') {
steps {
// Depends on your language / test framework
sh 'npm install'
sh 'npm run test'
}
}
}
}
However, it is not a possibility for me to use the Docker plugin and I have to use pod templates instead. Here is the setting that I am using:
With this setting, I can see the pod is running by running commands in the pod terminal, however, I get this messages in the logs in Jenkins and it eventually timeout and the agents gets suspended.
Waiting for agent to connect (30/100):
What do I need to change in pod/container template config?

Use existing service in Kubernetes via Jenkins

I have a Jenkins pipeline which runs an application in cloud using Kubernetes plugin.
So far, I have a simple yaml file which configures a pod. The Jenkins pipeline creates a pod and it does some operations (It's parsing some data).
I've created a service (with 1 replica) which I deployed and I want to use that in Jenkins instead of creating the same pod everytime I run.
Can someone please advise how to do that?
Currently this is how I am running the pipeline:
stage('Parse logs') {
agent {
kubernetes {
cloud 'sandbox'
label 'log-parser'
yamlFile 'jenkins/logparser.yaml'
}
}
when {
beforeAgent true
expression { params.parse_logs }
}
steps {
container('log-parser'') {
sh '/usr/local/openjdk-11/bin/java -jar /opt/log-parser/log-parser.jar --year=$year --month=$month --day=$day --hour=$hour
}
}
}
Can you please advise how to use the created service 'log-parser' instead of creating a pod everytime I run the pipeline?

Jenkins deploy artifact on same server

I'm trying to create a Jenkins Pipeline or group of itens to help me create a custom CI/CD for my projects and right now i'm stuck at the deploy part, i want to deploy on the same server that my jenkins is running (Windows Server/IIS). I would also like to know how to deploy to another server (Windows Server/IIS), this second one would be my production env.
I have managed to clone, build and archive using two approaches with Jenkins:
Pipelines
I have managed to create a pipeline that will clone my project, execute my build and then archive the artifacts from my build. The problem is, how do i deploy the artifact now?
This is my pipeline script
node {
stage('git clone') {
// Get some code from a GitHub repository
git 'my-git-url'
}
stage('npm install') {
bat label: 'npm install',
script: '''cd app
npm install'''
}
stage('gulp install') {
bat label: 'gulp install',
script: '''cd app
npm i gulp'''
}
stage('gulp production') {
bat label: 'gulp production',
script: '''cd app
gulp production'''
}
stage('create artifact') {
archiveArtifacts artifacts: 'app/dist/**',
onlyIfSuccessful: true
}
}
Freestyle projects
I have managed to create a project that will build and then archive the artifact using Execute shell build step and the Archive the artifacts post-build actions. How can i deploy the artifact using this approach? On this case i'm trying to trigger a second freestyle project to execute the deploy.
According to your question : "I want to deploy on the same server that my jenkins is running (Windows Server/IIS)" .. and comments I will suggest some approaches.
Windows
Use windows as operative system for production environments is not recommended. Linux is the only and the best choice.
IIS
I don't recommed IIS to deploys static assets. You need something more light and scalable. You could use :
nodejs with pm2 (https://expressjs.com/en/starter/static-files.html)
nginx (https://medium.com/#jgefroh/a-guide-to-using-nginx-for-static-websites-d96a9d034940)
apache (http://book.seaside.st/book/advanced/deployment/deployment-apache/serving-files)
docker
Deploy on IIS
Deploy static assets on IIS is just copy and paste the files on some folder and point IIS configurations to that folder:
https://www.atlantic.net/hipaa-compliant-hosting/how-to-build-static-website-iis/
Basic deploy on IIS using Jenkins
After your build commands, you just need to copy the build results (css.js.html.etc) and paste to some folder like c://webapps/my-app (pre-configured in IIS).
You can do this using a simple shell execution in free style project or pipeline script like https://stackoverflow.com/a/53159387/3957754
You could use this approach to deploy your static assets on the same server that your jenkins is running.
Advanced deploy on IIS using Jenkins
Microsoft has a tool called MSDeploy. Basically is a command line tool to deploy apps on remote IIS:
msdeploy.exe -verb:sync -source:contentPath="" -dest:contentPath=""
More details here:
https://stackoverflow.com/a/12032030/3957754
Note: You can't run MS deploy commands that talk to the MSDeploy service on the same machine
Jenkins Agent
Jenkins agent is an application that runs on a remote server, not where jenkins master node runs.
https://wiki.jenkins.io/display/JENKINS/Step+by+step+guide+to+set+up+master+and+agent+machines+on+Windows
Your master jenkins could use an agent in the remote or localhost IIS and execute jenkins jobs with copy and paste approach.

Deploy application using Jenkinsfile and AWS Code deploy

I am migrating from Jenkins 1.x to Jenkins 2. I want to build and deploy application using Jenkinsfile.
I am able to build gradle application, but I am confused about deploying application via AWS Codedeploy using Jenkinsfile.
Here is my jenkinsfile
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
// Get some code from a GitHub repository
git branch: 'master',
credentialsId: 'xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxx',
url: 'https://github.com/somerepo/someapplication.git'
// Mark the code build 'stage'....
stage 'Build'
// Run the gradle build
sh '/usr/share/gradle/bin/gradle build -x test -q buildZip -Pmule_env=aws-dev -Pmule_config=server'
stage 'Deploy via Codedeploy'
//Run using codedeploy agent
}
I have searched many tutorial but they're using AWS Code deploy plugin instead.
Could you help me deploying application via AWS Codedeploy using Jenkinsfile?
Thank you.
Alternatively you can use AWS CLI commands to do code deployment. This involves two steps.
Step 1 - Push the deployment bundle to S3 bucket. See the following command:
aws --profile {profile_name} deploy push --application-name {code_deploy_application_name} --s3-location s3://<s3_file_path>.zip
Where:
profile_name = name of AWS profile (if using multiple accounts)
code_deploy_application_name = name of AWS code deployment application.
s3_file_path = S3 file path for deployment bundle zip file.
Step 2 - Initiate code deployment
The second command is the used to trigger code deployment. See the following command:
aws --profile {profile} deploy create-deployment --application-name {code_deploy_application_name} --deployment-group-name {code_deploy_group_name} --s3-location bucket={s3_bucket_name},bundleType=zip,key={s3_bucket_zip_file_path}
Where:
profile = name of your AWS profile (if using multiple accounts)
code_deploy_application_name = same as step 1.
code_deploy_group_name = name of code deployment group. This is associated with your code deploy application.
s3_bucket_name = name of S3 bucket which will store your deployment artefacts. (Make sure that your role that performs code deploy has permissions to s3 bucket.)
s3_bucket_zip_file_path = same as step 1.

Resources