When Strapi content is being added to your site, your front-end application needs to be rebuilt to populate the new content.
You can configure Strapi webhooks that call Jenkins to build your project automatically whenever there is an update to your site.
We can also update the content on-demand using Strapi’s “Next Revalidation” option. That process is also explained later in this article.
- You have installed and configured a Strapi application.
- You have a Jenkins server
- You have a Jenkins user that has the job/build privileges.
- Your Jenkins instance must be secured with HTTPS.
Triggering build automatically using Strapi webhooks
Here is how you can configure webhooks in Strapi to call Jenkins builds
Step 1: Generate a build token for your Jenkins job
Go to the Jenkins job > Configure and under the “Build Trigger” section, check the “Trigger builds remotely (e.g., from scripts)” option and provide an arbitrary token. This token is an authentication token that can be used later to trigger builds from Strapi.
Step 2: Generate a token for Jenkins user
A Jenkins user and API token are needed to be supplied when building remotely. You can create an API token for your own user using the following guide.
If you want to use a service user, you can generate an API token for the Jenkins user.
Step 3: Configure Strapi Webhook
Once you have the Jenkins job and user configured, the webhook configuration is extremely simple.
Login to your Strapi admin dashboard, and go to Settings > Webhooks
Click on the “Create new webhook” button and provide a name for the webhook, for example, “Jenkins build”.
The most important part of this process is building the webhook URL. Use the following format:
Here is a breakdown:
- The URL should be protected with HTTPS
- jenkins-user: This is the Jenkins user that has access to the job/build to trigger builds. It can be a service user as well as long as it has the build permissions.
- jenkins-user-token: The user should have an API authentication token that acts as a password to perform Jenkins operations. This token can be created for your user or you can create it for a user as long as you are a Jenkins admin.
- jenkins.example.com: This is your Jenkins domain name.
- The “job/test/job/webhook-demo” is the path to the Jenkins job. You can go to the Job from Jenkins UI and copy the path from the browser.
- build: This refers to the action to be performed on this job, in this case, it’s “build”.
- token: This is the Authentication token we created for Build Triggers in Step 1.
You can leave the “Headers” section empty unless you need to pass them. For this demo, we are not going to use any headers.
The “Event” section can help you to figure out under what conditions a build should be triggered. For example, you may not want to rebuild the application every time an article is updated or a media is added. You can have all of them checked as well and that will trigger builds whenever any kind of update takes place on your contents.
After you fill in all these, you can test it by clicking the “Trigger” button on the top.
If the message is “Success”, the webhook is working properly.
If you get 403 or 401 errors, double-check the Jenkins user and make sure it has the necessary privileges.
Step 4: Test the webhook by adding content to Strapi
Now that we have the webhook configured, let’s test it by adding a piece of content to the Strapi.
Go to “Content Manager” from the Strapi admin dashboard > Articles > Create a new entry. Add the details, click Save, and then Publish.
Go to Jenkins and observe that the build has already started automatically.
Triggering builds on-demand – Next Revalidation
While webhooks are great for automatically triggering the build whenever a change is made to the content, in some cases, it might not be desired to keep rebuilding the Strapi NextJS application for every little change.
The Strapi project allows you to “Build Content” on demand whenever you think it’s worth populating the changes. This option can be accessed from the “Next Revalidation” menu.
To configure this so that whenever you push “Build Content”, your Jenkins build trigger, go to Settings > Next Revalidation > Configuration.
Here you can configure Build information.
Build url: This should be your Jenkins job build URL.
Build token: This is the Build token we specified inside our Jenkins job (Step 1)
Build user: The Jenkins user which should trigger the build
Build password: The Jenkins user password
After configuring this, you can go back to the “Next Revalidation” option on the left navigation menu and click “Build Content”.
This will call your Jenkins job to rebuild your NextJS front-end application. You will see a “Success” message if it works.
When working with Strapi, the content site needs to be rebuilt each time you add or update a piece of content. In this guide, we went through how to automate this process using Jenkins builds and Strapi webhooks. We also learned how to configure Strapi to trigger builds in Jenkins on demand using Next Revalidation.