Freestyle Job in Jenkins

Freestyle Job in Jenkins

In the last post, we created the Jenkins server in the cloud with a Docker container and installed the necessary plugins. In this post, I am going to create a test job in Jenkins.

There are two kinds of jobs in Jenkins: freestyle and pipeline. In most cases in production, we should use the pipeline job. In this post, I am going to demonstrate a freestyle job.

Prepare Gitlab and Dockerfile

Create a brand new GitLab project

Create the dockerfile

FROM openjdk:8-jre-alpine

EXPOSE 8080

COPY ./target/java-maven-app-*.jar /usr/app/
WORKDIR /usr/app

CMD java -jar java-maven-app-*.jar

Prepare Credentials

Gitlab Credential

I use SSH as the credential to Gitlab. We can configure it in the Credentials section in Jenkins. We should copy and paste our SSH private key to it.

DockerHub Credentials

Then we create the Dockerhub credential. We should input the username and password of Dockerhub into the newly created credential.

Create Freestyle Job

Once we have prepared the necessary components to create our first Jenkins job. We select to create a Freestyle project and give it the name "simple-job".

In the section "Source Code Management", we input our git repository and select the SSH credential we prepared in the last section.

In the section "Build Environment", tick "Use secret text(s) or file(s)". We then bind the username and password of the Dockhub credential into variables.

In the "Build Steps", we build the source code with the Maven plugin we installed in the last post and then we execute the following shell commands.

docker build -t ${docker_hub_user}/my-repo:jma-1.1 .
echo $PASSWORD | docker login -u $USERNAME --password-stdin
docker push ${docker_hub_user}/my-repo:jma-1.1

Run the Job

Now we can run the job we defined in this post.

We then can check the Jenkins log and repository we pushed to the Dockerhub.

Summary

In this post, we created our first Jenkins job which built the source code, built and pushed the docker image to Dockerhub successfully.

Reference

Module 8 of DevOps Bootcamp