Deploy Psychz Dedicated Server Using Jenkins Pipeline Scripts
Publisher: Psychz Networks, March 21,2023Jenkins is an open-source continuous integration tool that helps automate the software build, testing, and deployments involved in software development. It is written in Java, so it will require Java to be installed on our system.
Here, we will see how to install Jenkins on Rocky Linux 8 / AlmaLinux 8.
Installing Java
Step 1: Install OpenJDK – JAVA
As Jenkins is written in Java, thus we need a Java development kit installed on our AlmaLinux or Rocky. Here will install the OpenJDK version that is available in the official repo of our Linux.
Step 2: Add Jenkins RPM repo on AlmaLinux/Rocky.
The next thing is to add the source from where we will receive the Jenkins packages and updates to install. For that, we need to add the official RPM repo of Jenkins on our Linux using the below commands:
First, import and add the GPG key:
Next, add and create the repository.
[jenkins]
name=Jenkins-stable
baseurl=http://pkg.jenkins.io/redhat-stable
gpgcheck=1
EOF
Press Enter key and provide your user password, and the repo will be created.
Step 3: Refresh system repos
Refresh the repository cache to integrate the newly added repository into the system.
Step 4: Command to install Jenkins on Almalinux or Rocky Linux
Finally, run the command to install and set up the Jenkins server on our Redhat-based Linux operating system.
Important: If you don't want to use the repo, download the RPM package directly from Github.
To install the RPM package, switch to the directory where you have downloaded the file and run the below command
Step 5: Start & enable Jenkins Service
To start the Jenkins service on Almalinux or Rocky and also enable the same so that it can start automatically with system boot, follow the given command-
Step 6: Allow Jenkins port 8080 to access from a remote system
Although after installing and running this automation server services, we can access its web interface from a locally installed browser; however, if you are using a CLI server, we have to use a remote system browser to access it. For that, the ports must be opened in the AlmaLinux firewall.
Step 7: Note Admin password
With the installation, Jenkins will create a password for the Admin user. To find that, run the below command and then copy the same.
A password will be displayed, copy and save it somewhere.
Step 8: Access Jenkins web interface
Now, open a browser on your local system or any other system to access the IP address of the system where the Jenkins server has been installed.
In the browser URL address bar type- http://server-ip-address:8080
Enter the Username and Password and sign-in
Step 9: Install Plugins
You will have two options to install Jenkins plugins to extend its features. It is recommended to go for the "Install Suggested Plugins" option. We will also install or additional plugins later from the Jenkins web interface.
Select "Install suggested plugins"
Step 10: Create First Admin User
Once you have configured the Plugins, create an Admin user account that will be used to login to the Jenkins web interface later.
Enter the required details and ensure that you save or memorize the password you have used to create the Admin login.
After that, the setup will provide a page for Instance, and Configuration, showing the current URL to access Jenkins. Just save and continue.
Step 11: Jenkins dashboard
Finally, you have successfully run and configured Jenkins on AlmaLinux. Now you can Create new pipelines to start testing applications.
List of Jenkins Plugins
Jenkins, by default, will install a set of plugins required to run it. Apart from the installed plugins, you will also need a few additional plugins to create and run your pipeline successfully builds.
To download and install the plugins, go to the main dashboard and click on "Manage Jenkins" with the gear icon on the left-side navigation menu bar. Now, under "System Configuration," click on "Manage Plugins." On the left-hand side, click on "Available Plugins" to search the required plugins as mentioned below, one by one.
Select the checkbox next to the plugin name and hit the "Download now and install after restart" button at the bottom of the page.
- Credentials Plugin
- Credentials Binding Plugin
- commons-lang3 v3.x Jenkins API Plugin Version
- Checks API plugin
- Caffeine API Plugin
- Apache HttpComponents Client 4.x API Plugin
- Branch API Plugin
- Maven Integration plugin
- HTTP Request Plugin
- Pipeline: Groovy Libraries
- Pipeline: REST API Plugin
- Plain Credentials Plugin
- Plugin Utilities API Plugin
- SSH Build Agents plugin
Adding credentials
Jenkins offer a module that lets you save all the login credentials safely which you can easily call via a code in your pipeline script. Itis highly advisable to add your login credentials like security token and username or password in Jenkins "Credential" module.
Follow the steps given below
Step 1 : To access "Credentials" feature, click on "Manage Jenkins" with the gear icon on the left-side navigation menu bar. Now, under "Security," click on "Credentials." Navigate to "System" > "Global credentials (unrestricted)."
Step 2: Click on "Add Credentials."
Step 3: Select "Username with password" from the "Kind" dropdown.
Step 4: Enter your Psychz API access token as the "Username" and your access username as the "Password".
Step 5: Assign a unique ID to this credential, e.g., "psychz-api-credentials".
Step 6: Click "OK" to save the credentials.
Create Jenkins Pipeline
Create a new Jenkins pipeline job and configure it to use a Jenkinsfile or the pipeline script directly. To create a pipeline, click on "New Item" with the + icon on the dasboard and select "Pipeline" module from the list. Before moving forward, name the pipeline that you wish to create.
Scroll down to the "Pipeline" section with the text area (under Script) and copy the below pipeline script and paste it in the text area.
import groovy.json.JsonSlurper
pipeline {
agent any
stages {
stage('Deploy dedicated server') {
steps {
script {
withCredentials([usernamePassword(
credentialsId: 'psychz-api-credentials',
usernameVariable: 'ACCESS_TOKEN',
passwordVariable: 'ACCESS_USERNAME'
)]) {
def apiUrl = 'https://api.psychz.net/v1/order_express'
def payload = [
access_token: ACCESS_TOKEN,
access_username: ACCESS_USERNAME,
plan_id: 138,
order_quantity: 1,
payment_mode: 1,
os_cat: 1,
os_id: 7366,
disk_partition_id: 9,
software_raid: 1,
hostname: 'testserver1',
password: 'test1234567890',
partner_id: 0,
enforce_password_change: 0
]
def response = httpRequest(
url: apiUrl,
httpMode: 'POST',
contentType: 'APPLICATION_JSON',
requestBody: groovy.json.JsonOutput.toJson(payload),
validResponseCodes: '200:404'
)
def jsonResponse = new JsonSlurper().parseText(response.content)
echo "Order ID: ${jsonResponse.data.order_id}"
return jsonResponse.data.order_id
}
}
}
}
}
}
Save the Jenkinsfile and commit it to your source control repository if necessary.
Execute Pipeline
You can use the "Build Now" feature to test run your pipeline or Trigger the Jenkins pipeline job automatically (e.g., through webhooks).
Once the pipeline is executed successfully (indicated in green), you can check the console output to see the fetched results.
The above pipeline script sends a POST request to the Psychz API to order the specified dedicated servers, using the stored credentials. The response from the API will be displayed in the Jenkins job console output. By using the withCredentials function from the Credentials Binding plugin, you're securely binding the stored credentials to environment variables that can be used in the pipeline script without exposing the sensitive data.
IMPORTANT NOTE: Please ensure to whitelist your IP Address before you attempt to build the pipeline. To know how to whitelist an IP address, please refer to the following article Setup API Access Key