Development Tools: Linking GitHub’s Private Repo to Dockerized Jenkins

Bayu Adi Wibowo
3 min readDec 24, 2023

--

Build, test, and deploy are processes that are used in software development. There will be a lot of effort if those processes are done manually. Developers usually use automation tools to automate the whole process like using Jenkins to do the deployment process. But, how do we develop and test the deployment script? One of the options is to dockerized all necessary tools to the local computer. Ready to dive in? Let’s explore the steps to make it happen.

Jenkins in Docker Mode

Environment Used

  • Host: MacOS Monterey 12.6.5
  • Docker version 24.0.7
  • jenkins/jenkins:2.426.2-jdk17 image

Spin up Jenkins

Prepare the docker network for the Jenkins.

docker network create jenkins

Create a Dockerfile to build the image.

FROM jenkins/jenkins:2.426.2-jdk17
USER root
RUN apt-get update && apt-get install -y lsb-release
RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc https://download.docker.com/linux/debian/gpg
RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.asc] https://download.docker.com/linux/debian $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
RUN apt-get update && apt-get install -y docker-ce-cli
USER jenkins
RUN jenkins-plugin-cli --plugins "blueocean docker-workflow"

Run the build command to build the Jenkins image.

docker build -t myjenkins-blueocean:2.426.2-1 .

If everything is fine, the image will created with myjenkins-blueocean:2.426.2–1 name. Then we can start to spin up the Jenkins container.

docker run \
--name local_jenkins \
--restart=on-failure \
--detach \
--network jenkins \
--env DOCKER_HOST=tcp://docker:2376 \
--env DOCKER_CERT_PATH=/certs/client \
--env DOCKER_TLS_VERIFY=1 \
--publish 8080:8080 \
--publish 50000:50000 \
--volume jenkins-data:/var/jenkins_home \
--volume jenkins-docker-certs:/certs/client:ro \
myjenkins-blueocean:2.426.2-1

Some highlights:

  • Will publish 8080 and 50000 ports to the host.
  • Create jenkins-data and jenkins-docker-certs, so when the container is restarted it will retain all data changes of the container.

Jenkins is served in 8080 port, open http://localhost:8080/ to start using Jenkins. After a fresh installation, Jenkins will have a wizard to create an admin user and initial plugin installation. It will need a temporary password before user creation, the token can be found at /var/jenkins_home/secrets/initialAdminPassword. Just follow the instructions until the Jenkins Dashboard opens.

Sample GitHub Private Repo Pipeline

GitHub Personal Access Token (PAT) will be used for Jenkins connection. The PAT will provide access to the GitHub API.

Create GitHub PAT

  • Go to your GitHub personal settings.
  • Find the Personal Access Token setting from the Developer Settings tab.
  • Create the token with the classic option.
  • Fill note, access expiration, and necessary permission for the token.
  • Submit and copy the token to a safe place.

Register PAT to Jenkins Credentials

Every Jenkins pipeline sourced from GitHub will need credentials to access the repository. So, before creating a Jenkins pipeline it’s better to prepare the credentials first.

  • Go to the Manage Jenkins section.
  • Then navigate to Credentials > Global domain.
  • Add the credentials and choose Username with password.
  • Fill Username with the GitHub username and fill in the password with PAT created earlier.
  • Put ID fields with value as credentials identifier.
  • And submit the credentials.

Create Jenkins Pipeline

Finally, after all prerequisites are done a Jenkins pipeline can be created. We will create a simple Freestyle Project to check the connection between Jenkins and GitHub.

  • Add a new item from the Jenkins dashboard.
  • Choose Freestyle Project.
  • Use git as Source Code Management.
  • Go to the private repo and find the HTTPS Clone link under the code button.
  • Put the GitHub link to the Repository URL.
  • On the credentials, choose the ID that is created earlier.
  • Change the necessary config like Branches, etc if needed.
  • Under the Build Steps tab, create a new dummy Execute shell steps and fill with this command
echo 'Hello, world!'
  • Save the pipeline and try to trigger it with the Build Now button.

Sample success run result.

That’s it!!! 🏁🏁🏁

Now, we can start to develop the deployment scripts to automate the deployment process using our local computer. We can easily debug and test the script locally.

Stay tuned for the real use cases in the next article.

Thank you!!! 🍻

--

--