Logo

dev-resources.site

for different kinds of informations.

Deploying a Next.js UI App on S3 Using Jenkins🤩

Published at
1/11/2025
Categories
jenkins
cicd
github
nextjs
Author
krishna7852
Categories
4 categories in total
jenkins
open
cicd
open
github
open
nextjs
open
Author
11 person written this
krishna7852
open
Deploying a Next.js UI App on S3 Using Jenkins🤩

Deploying your Next.js application to Amazon S3 using Jenkins can simplify your development workflow and enable efficient continuous delivery.

What is Jenkins?

Jenkins is a popular open-source automation server that facilitates continuous integration and continuous delivery (CI/CD) in software development. It is widely used to automate tasks related to building, testing, and deploying applications, making it a crucial tool in modern DevOps pipelines.

Jenkins is highly extensible through plugins, allowing it to integrate with various tools and technologies. Whether you are working with Java, Node.js, Python, or other programming languages, Jenkins can be configured to work with almost any tech stack.

Key Features of Jenkins

Automation of Build and Deployment: Jenkins automates repetitive tasks such as building code, running tests, packaging applications, and deploying them to various environments. This significantly reduces manual intervention, accelerates delivery times, and minimizes human errors.

Continuous Integration: Jenkins helps developers integrate their code into a shared repository frequently. Each integration is verified by automated builds and tests to detect problems early, ensuring that issues don’t accumulate over time.

Extensibility: Jenkins has a plugin-based architecture. There are thousands of plugins available for a wide variety of tools and technologies, including version control systems (Git, SVN), build tools (Maven, Gradle), testing frameworks (JUnit, Selenium), deployment systems (Docker, Kubernetes), and more.

Distributed Builds: Jenkins supports distributed builds, meaning that you can set up multiple Jenkins agents on different machines to offload tasks and improve build times, particularly in large projects.

Pipeline as Code: Jenkins allows users to define and automate complex workflows using "Jenkinsfiles" written in Groovy or declarative syntax. This lets teams maintain and version control their CI/CD processes, ensuring transparency and reproducibility.

Cross-Platform Support: Jenkins is platform-independent and can run on various operating systems like Linux, Windows, and macOS. It also works with any software that can be scripted.

Integration with Version Control Systems: Jenkins integrates seamlessly with version control systems like Git, SVN, and Mercurial. This enables it to automatically trigger build processes when changes are made to the codebase, further streamlining the CI/CD process.

Why Use Jenkins?

Faster Development: By automating the build, test, and deployment process, Jenkins accelerates software development cycles, allowing teams to release updates more frequently and with higher quality.

Improved Collaboration: Jenkins integrates with version control systems like Git, allowing teams to work together more effectively. Developers can push their code to the repository, and Jenkins will automatically pick up the changes and run tests, ensuring that everything works together seamlessly.

Increased Quality: Automated tests and consistent deployment practices reduce human error, ensuring that only properly tested and vetted code is deployed to production.

Scalability: As your project grows, Jenkins can scale by adding more agents to handle multiple tasks in parallel. This is especially useful for larger projects that require heavy computational resources.

How Jenkins Works

Jenkins operates on a client-server model. The server is responsible for managing and scheduling jobs (tasks) to be executed, while the agents (also known as slaves) are responsible for running those jobs. Here's an overview of how Jenkins works:

Job Creation: A Jenkins job defines a task or workflow. For example, a job could be set up to pull the latest code from a version control system, run tests, and deploy the application.

Triggering a Job: Jobs can be triggered manually or automatically. You can configure Jenkins to start a job whenever new code is pushed to a version control system, on a schedule (e.g., nightly), or based on other events.

Job Execution: When the job is triggered, Jenkins runs the necessary steps (e.g., fetching code, building, testing, deploying). This can involve running shell scripts, invoking external tools, or using Jenkins plugins.

Reporting and Notifications: After the job is executed, Jenkins provides detailed reports and logs. If the build is successful, Jenkins will display a green indicator, and if it fails, a red indicator. Jenkins can also send notifications via email, Slack, or other channels to keep stakeholders informed.

We’ll walk you through the necessary steps to deploy your Next.js application to Amazon S3 using Jenkins.

Prerequisites

Before we dive into the deployment process, ensure that you have the following prerequisites:

  • Next.js Application: A working Next.js app. If you don’t have one, you can create one using the create-next-app command.
  • Amazon S3 Bucket: An S3 bucket where the static assets will be stored.
  • AWS IAM Credentials: An AWS user with appropriate permissions to access and upload to your S3 bucket.
  • Jenkins Server: A Jenkins instance running with the necessary plugins installed (specifically AWS CLI, NodeJS, and Git).
  • AWS CLI Setup: The AWS CLI must be installed and configured on the Jenkins server. Let’s break down the deployment steps.

Step 1: Set Up the Next.js Application for Static Export

Before deploying your Next.js application to S3, you need to build it as a static site.

1.1 Configure next.config.js
Ensure that your Next.js app is configured for static site generation (SSG). In your next.config.js file, enable the static export option:

module.exports = {
  exportTrailingSlash: true,
};
Enter fullscreen mode Exit fullscreen mode

This setting ensures that the Next.js app is exported with URLs that include a trailing slash.

1.2 Export the Next.js App
Next.js provides a built-in export command to generate static files. Run the following command to export the application:

npm run build
npm run export
Enter fullscreen mode Exit fullscreen mode

This will generate the static files in the out directory. You’ll be uploading these files to your S3 bucket later.

Step 2: Set Up AWS S3 Bucket

Create an S3 Bucket:
Go to the AWS S3 console and create a new S3 bucket. Ensure the bucket is public so users can access the files.

Configure Bucket for Static Hosting:

  • After creating the bucket, navigate to the bucket’s settings and enable "Static website hosting."
  • Set the index document as index.html. Set Permissions: Configure the bucket’s permissions to allow public access to the static assets. Add a bucket policy like this:
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "PublicReadGetObject",
      "Effect": "Allow",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::your-bucket-name/*"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

This ensures that your static files can be accessed publicly.

Step 3: Set Up Jenkins for Deployment

3.1 Install Jenkins Plugins
Ensure you have the necessary Jenkins plugins installed:

  • AWS CLI Plugin (for interacting with AWS services)
  • NodeJS Plugin (for building your Next.js app)
  • Git Plugin (for cloning your repository)
    3.2 Create a New Jenkins Pipeline

  • Create a New Job: In Jenkins, create a new Pipeline job.

  • Configure SCM: In the Pipeline configuration, connect your repository (e.g., GitHub, Bitbucket) so Jenkins can pull the code from your repository.
    3.3 Jenkins Pipeline Script
    You can now write a pipeline script that automates the entire process, including building the app and deploying it to S3.

Here’s an example of a Jenkins pipeline that does all of this:

pipeline {
    agent any

    environment {
        AWS_ACCESS_KEY_ID = credentials('aws-access-key-id')  // Jenkins Credentials Store
        AWS_SECRET_ACCESS_KEY = credentials('aws-secret-access-key') // Jenkins Credentials Store
        S3_BUCKET_NAME = 'your-s3-bucket-name'
        REGION = 'us-west-2'
    }

    stages {
        stage('Clone Repository') {
            steps {
                git 'https://github.com/your-username/your-nextjs-repo.git'
            }
        }

        stage('Install Dependencies') {
            steps {
                script {
                    // Set up Node.js
                    def nodeVersion = '16.x'
                    def nodeHome = tool name: 'NodeJS', type: 'ToolLocation'
                    env.PATH = "${nodeHome}/bin:${env.PATH}"

                    // Install dependencies
                    sh 'npm install'
                }
            }
        }

        stage('Build Next.js App') {
            steps {
                sh 'npm run build'
                sh 'npm run export'
            }
        }

        stage('Deploy to S3') {
            steps {
                script {
                    // Sync the generated files to S3
                    sh """
                        aws s3 sync out/ s3://${S3_BUCKET_NAME}/ --delete --region ${REGION}
                    """
                }
            }
        }

        stage('Invalidate CloudFront Cache') {
            steps {
                script {
                    // Invalidate the CloudFront cache if using CloudFront as CDN
                    // If you are using CloudFront for caching, you should invalidate the cache after the deployment
                    sh """
                        aws cloudfront create-invalidation --distribution-id YOUR_DISTRIBUTION_ID --paths "/*" --region ${REGION}
                    """
                }
            }
        }
    }

    post {
        success {
            echo "Deployment Successful!"
        }

        failure {
            echo "Deployment Failed!"
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation of the Pipeline:

  • Clone Repository: This step clones the repository containing your Next.js app from a GitHub or GitLab repository.
  • Install Dependencies: Installs the required dependencies for your Next.js app.
  • Build Next.js App: Runs npm run build and npm run export to generate static files in the out directory.
  • Deploy to S3: Uses AWS CLI’s aws s3 sync command to upload the files from the out directory to your S3 bucket.
  • Invalidate CloudFront Cache: If you’re using CloudFront as a CDN, this step invalidates the cache to ensure that your users see the latest content.

How to access Private Git repository to Jenkins?

To access a private GitHub repository from Jenkins, you need to configure credentials for authentication. This can be done using Jenkins Credentials either via a GitHub token.

Steps to configure GitHub token credentials:
Create a Personal Access Token (PAT) on GitHub:

  • Go to GitHub Settings.
  • Click Generate new token.
  • Give it a name, and select the necessary scopes (e.g., repo for full access to private repositories).
  • Copy the token (you won’t be able to see it again).
    Add the credentials to Jenkins:

  • Go to Jenkins Dashboard > Manage Jenkins > Manage Credentials.

  • Choose the correct domain (or leave it as global).

  • Click Add Credentials.

  • Set Kind to Username with password.

  • In the Username field, put your GitHub username.

  • In the Password field, paste the Personal Access Token you just created.

  • Give the credentials an ID (e.g., github-credentials).

Update your Jenkins Pipeline script to use the credentials:

You can reference the credentials in your pipeline script using the credentialsId field.

pipeline {
    agent any
    tools {
      nodejs 'nodejs' // nodejs is a plugin name
    }
    stages {
        stage('Git Checkout') {
            steps {
                git credentialsId: 'github-credentials', url: "https://github.com/workspace/xyz.git", branch: "main"
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • credentialsId: This matches the ID of the credentials you created in Jenkins.
  • url: The HTTPS URL of your GitHub repository.
  • branch: The branch you want to clone (e.g., main).

By following these steps, you can automate the deployment of your Next.js application to Amazon S3 using Jenkins. This approach not only makes your deployment process more efficient but also integrates well into continuous integration/continuous deployment (CI/CD) pipelines.

With Jenkins handling the build and deployment, you can focus on developing your Next.js app, knowing that each update will be automatically pushed to S3 in a reliable and repeatable manner.

Happy Coding ...!!

github Article's
30 articles in total
Favicon
Cómo gestionar tus proyectos de software con Github
Favicon
How to upload Markdown files to Dev.to from GitHub
Favicon
Implantando um aplicativo Angular com DigitalOcean e GitHub de forma gratuita
Favicon
Survival Manual: How to Create and Manage a Project in Git
Favicon
Top 50 Websites a Backend Developer Must Know 🖥️🔧🚀
Favicon
Unlocking the Power of GitHub Copilot: Your AI Pair Programmer
Favicon
Top 10 Trending GitHub Repositories, Nov 24 2024
Favicon
Improving Port Management Speed: Why I Created `port-client` to Replace `npx kill-port`
Favicon
Unlock Your Coding Potential with the GitHub Copilot Global Bootcamp!
Favicon
Publish Private Nuget Packages on Github
Favicon
Git Merge VS Git Rebase: Which One Should YOU Use?
Favicon
Static sites FTW
Favicon
Hood Ball — Multiplayer web-based game
Favicon
GitHub Makeover: Create a Profile README That Stands Out and Connects! 👨‍💻
Favicon
Deploying a Next.js UI App on S3 Using Jenkins🤩
Favicon
The Global South OSC
Favicon
🎁 20 Open Source Projects You Shouldn't Miss in 2025
Favicon
A Conversation with Docker CTO Justin Cormack and Flux CEO Ron Efrani: The Future of Developer Environments
Favicon
Sample Programs Repo Celebrates 1,000 Code Snippets
Favicon
Learn Machine Learning with these amazing GitHub repositories! 🚀
Favicon
🐈‍⬛ Git and GitHub: A Beginner’s Guide to Version Control 🚀
Favicon
Undo Mistakes in Git: Revert, Reset, and Checkout Simplified
Favicon
My First npm Package!
Favicon
Top 14 GitHub Data Risks: Data Loss Scenarios and How to Prevent Them
Favicon
Mastering Git and GitHub: A Guide for New Team Members
Favicon
Why I Stopped Using Plain Git Pull (And Why You Should Too)
Favicon
Why I Built commit-ai: A Story About Git Security and Team Safety
Favicon
🔄 Automating GitHub PR Notifications with Slack Integration: A Journey
Favicon
How to Link git to GitHub via SSH on Windows
Favicon
Mastering Git Workflows: Beyond Basic Commands

Featured ones: