Last updated on August 29th, 2024 at 11:29 am
We can easily integrate Jenkins to connect to GitHub and pull files from GitHub repository. This is useful to build a CI / CD pipeline. In this tutorial we will first install Jenkins on Amazon Linux 2023 and then setup a connection between Jenkins and GitHub repo using personal access token. Eventually we will try to run a build to make sure that everything is working as expected. I am trying to make this installation as simple as possible, Lets configure Jenkins with GitHub
- Step 1: Install Jenkins
- Step 2: Configure Jenkins
- Step 3: Connect to GitHub
- Step 4: Run a test build
- Common Error
Step 1: Install Jenkins
I am assuming that you already have the EC2 instance with Amazon Linux 2023 launched and ready to login, if you are not familiar with AWS and would like to know more on how to launch an EC2 instance using Amazon Linux 2023 please take a look at this documentation.
Note: Make sure you have enough /tmp free space (more than 500 MB) available. Also increase the EBS volume size if required.
Run these commands to update the packages, as you can see it shows you the release version which is in my case 2023.5.20240819 at the time of writing this tutorial, it could be different for you.
Take a look at this release note page for more details.
We have to use dnf command for updating
Once the upgrade is successful we are ready to install Jenkins package. The latest version available while writing this tutorial was Jenkins version 2.462.1
- First step is to add the Jenkins repo using the following command:
$ sudo wget -O /etc/yum.repos.d/jenkins.repo \ https://pkg.jenkins.io/redhat-stable/jenkins.repo
- Import a key file from Jenkins-CI to enable installation from the package:
$ sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
When you are done with the repo addition, use yum to install Jenkins
Next let us install Java and the JRE packages compatible with version 2.462.1 of Jenkins are Java 11, Java 17, or Java 21. I am installing Java 21 – More details on Jenkins Java Support Policy
Now all we have to do is enable the Jenkins service and start the service. Check the logs if service refuse to start for further investigation.
$sudo systemctl enable jenkins
$sudo systemctl start jenkins
$sudo systemctl status jenkins
Before we go to step 2 the last package to be installed on the server is git (this is important)
$ sudo dnf install git
Step 2: Configure Jenkins
Go to URL http://<YOUR_PUBLIC_IP>:8080/ (making sure the port 8080 is open)
On the server run this command and it will give you the password to Get Started with Jenkins
$ cat /var/lib/jenkins/secrets/initialAdminPassword
Click “Continue” to proceed, this step will install all the dependencies.
Once it is done we will get a “Jenkins is ready” message.
once you click on “Start using Jenkins” we should get this page
Awesome we have successfully installed Jenkins
Step 3: Connect to GitHub
Create a new item and configure the build, http://<YOUR_PUBLIC_IP>:8080/view/all/newJob and then select the type as Freestyle Project and add item name of your choice.
Press OK and the next step is to configure the GitHub credentials. Before go further there is one last step we have to do on the GitHub account,
- In a new browser tab create new personal access token on GitHub , For that you need to go to your Profile > Developer Settings > Tokens or click here https://github.com/settings/tokens/new .
- Make sure you select the scopes according to your requirement.
Once you generate the token, copy that and go back to the Jenkins item set up screen.
Your repository URL format should be https://<personal_access_token>@github.com/USERNAME/repo.git, the name of branch by default is “MASTER” hence I modified the Branch name to what I have. Make sure you provide your branch names accordingly. Hit “Save” once done.
Note: You should not see any error (in red font popping up just above the “Credentials” field ) while putting in the repository URL. If there one make sure to fix the issue.
Step 4: Run a test build
This step will make sure that Jenkins is able to connect to GitHub without any issues.
Click on the “Build Now” and that will create a new build, as you can see for it is the #3 (for you it will be different build number #1 may be. This happened since I tried 2 times building before taking a screenshot). Seeing a green check mark is a good sign.
Click on number 3 and you will see a status window, check the console output to view more details. This window will also show you if there was any errors during the build process.
Common Error
- If you see this “Jenkins build waiting for next available executor” that normally means there is some resource contention, make sure to check the node status. Usually Free Temp Space or Free Disk Space will be the culprit.
- If you see GitException related error similar to the one below in build console output, then it normally means git package is not installed on the server where Jenkins is running
Caused by: hudson.plugins.git.GitException: Error performing git command: git init /var/lib/jenkins/workspace/jenkins-with-github
at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2862)
at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2766)
at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2761)
at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:2051)
at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl$5.execute(CliGitAPIImpl.java:1071)
... 12 more
Caused by: java.io.IOException: Cannot run program "git" (in directory "/var/lib/jenkins/workspace/jenkins-with-github"): error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1170)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1089)
at hudson.Proc$LocalProc.<init>(Proc.java:252)
at hudson.Proc$LocalProc.<init>(Proc.java:221)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:994)
at hudson.Launcher$ProcStarter.start(Launcher.java:506)
at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2839)
... 16 more