How to setup Jenkins + AWS
20.01.2024
20 min
Start the instance (here we used Ubuntu, but other OSes should also be suitable);
In the security group, click on Create a new Group and select the access from Anywhere setting
We configure the possibility of joining Jenkins in the instance -> security groups link (see the next screenshot) -> edit inbound rules
Add a new inbound rule using the Add rule button, set port 8080, and source to “anywhere ipv4”
We install Jenkins using this guide, not forgetting to install Java first https://www.jenkins.io/doc/book/installing/linux/
Or rather, a small part of it in which Debian/Ubuntu is written
After running these commands, you can see if Jenkins is running using sudo systemctl status jenkins
p.s. to exit this status you can press ctrl + c
Now, to connect to Jenkins, it is enough to take public IPv4 from the instance (screenshot below) and add IP :8080 to it
And the result should be something like this:
When trying to connect to Jenkins, we see that we need to find a password
Therefore, we go to the instance, follow the indicated path (you can simply enter sudo vim /var/lib/jenkins/secrets/initialAdminPassword and copy this password), copy and paste the password, and go to Jenkins, where we install everything as on the local machine, register;
Go to Manage Jenkins -> Plugins -> Available Plugins and install these 3 plugins
Immediately go to Manage Jenkins -> Security, go to the very bottom, and change this setting to Accept first connection
We go to GitLab, go to the settings, as shown in the next screenshot
Let’s go to Access tokens:
And we create an access token with a tick on the api:
Copy this newly created token and go to Jenkins, where we go to Manage Jenkins -> Credentials -> System -> Global credentials, as shown in the screenshot
Here we click on Add Credentials and create Credentials with the following parameters:
After the previous point, we need to go to Manage Jenkins -> System and here scroll to the Gitlab settings and configure everything like this:
And click on test connection on the bottom right, if it did not work, then check all the previous points related to the connection
Now we need to generate an ssh key
We go to the console and write ssh-keygen, leave everything at the default or whatever is convenient for you go to these files along the path given in the console, and take the ssh keys, go to GitLab, go to ssh keys, they are almost where access tokens are
We create a new key on GitLab, insert what is in the id_rsa.pub file there
Then we go to Jenkins, go to credentials as before
Manage Jenkins -> Credentials -> System -> Global credentials
Add new credentials:
And there it is on the screenshot, at the bottom there is an enter directly field, we copy everything in the id_rsa file there, including lines
—–BEGIN OPENSSH PRIVATE KEY—– and
—–END OPENSSH PRIVATE KEY—–
Create a repository on GitLab in any convenient way
This is how it turned out for us
And also, it is quite important to create a new branch immediately or later before setting up webhooks, because then we will not have the opportunity to make merge requests
We go to Jenkins and create a new job
We take the ssh clone repository
And we add it in the job settings in Jenkins with the corresponding pretzels that we created, as shown in the screenshot
We get to build triggers, where we click on Build when a change is pushed to GitLab and choose anything you need, in my case I choose accepted merge request events
We go to GitLab, to our project -> settings -> webhooks
And here we have 3 points:
In the first point, we enter the current address of our job, for we go to the settings of our job, and build triggers, and there you can see the GitLab webhook URL, but it is not quite correct, the very first IPv4 address is stored there, we need to change it to the current one, as shown in the screenshot:
We insert it in the first point
We go further, we see in the job that we have an advanced sub-item in build triggers, go to it, generate a secret token, save the changes in the job and copy this token, and paste it into the second item on GitLab
In the third point, everything is simple, click on the merge request events checkbox and everything is ready
Save this webhook and test it, you should see a message like this:
You may also need sudo rights to execute some commands, for this we go to the instance and register sudo visudo and write this line at the very bottom of this file jenkins ALL=(ALL) NOPASSWD: ALL
Now we go to the job settings again and configure all the logic we need, in our case, it is to build the project and copy the jar to the /tmp/test/ folder, which must be created before the job build
And now we can make a second job in which you simply add the program launch logic:
Now, all you have to do is merge the branches and everything will automatically build and start
Similar post
All post10 min
Read more