DEV Community

Cover image for Building a Custom Ubuntu Image with SDKMAN and Using It in a GitLab CI/CD Pipeline
Tandap Noel Bansikah
Tandap Noel Bansikah

Posted on

Building a Custom Ubuntu Image with SDKMAN and Using It in a GitLab CI/CD Pipeline

In this article, we’ll walk through the process of creating a custom Ubuntu Docker image with SDKMAN installed. SDKMAN is a tool for managing parallel versions of multiple Software Development Kits (SDKs) like Java, Scala, and sbt. We’ll also demonstrate how to use this custom image in a GitLab CI/CD pipeline.

Step 1: Creating the Custom Ubuntu Image

Below is a Dockerfile that sets up an Ubuntu 20.04 base image, installs necessary dependencies, and configures SDKMAN to install specific versions of Java, Scala, and sbt.
Dockerfile:

# Dockerfile
FROM ubuntu:20.04

# Install dependencies
RUN apt-get update && apt-get install -y \
    curl \
    unzip \
    zip \
    bash \
    git \
    vim \
    wget \
    jq \
    tmux 

# Install SDKMAN!
RUN curl -s "https://get.sdkman.io" | bash

# Set the shell to bash and ensure SDKMAN is sourced
SHELL ["/bin/bash", "-c"]

# Ensure SDKMAN is sourced and install specific SDKs
RUN source "$HOME/.sdkman/bin/sdkman-init.sh" && \
    sdk install java 21.0.2-open && \
    sdk install sbt 1.8.2 && \
    sdk install scala 2.13.10

# Set the working directory
WORKDIR /app
Enter fullscreen mode Exit fullscreen mode

Explanation:
Base Image: We start with ubuntu:20.04 as the base image.

Dependencies: Essential tools like curl, unzip, git, and wget are installed.

SDKMAN Installation: SDKMAN is installed using its installation script.

SDK Installation: Specific versions of Java, Scala, and sbt are installed using SDKMAN.

Working Directory: The working directory is set to /app.

Building the Image:

To build the Docker image, run the following command:

docker build -t bansikah/gitlab-ci:v1.0.1 .
Enter fullscreen mode Exit fullscreen mode

Build Image

Push image to Registry(your personal dockerhub registry)

Also we will exec into the container to check if the tools we actually installed are there like below
Push image & and exec into it to check sdkman version

Docker Push

Step 2: Using the Custom Image in a GitLab CI/CD Pipeline

Once the Docker image is built and pushed to a container registry (e.g., Docker Hub), you can use it in a GitLab CI/CD pipeline. Below is an example .gitlab-ci.yml file that uses the custom image and verifies the SDKMAN installation.
.gitlab-ci.yml

# .gitlab-ci.yml
image: bansikah/gitlab-ci:v1.0.1  

stages:
  - test

test-job:
  stage: test
  script:
    - echo "Checking SDKMAN version..."
    - source "$HOME/.sdkman/bin/sdkman-init.sh"  
    - sdk version
    - echo "Checking installed SDKs..."
    #- sdk list java
    #- sdk list scala
    #- sdk list sbt
Enter fullscreen mode Exit fullscreen mode

Explanation:
Image: The custom Docker image (bansikah/gitlab-ci:v1.0.1) is specified as the base image for the pipeline.

Stages: A single stage (test) is defined.

Script:

  • The source command ensures SDKMAN is initialized.

  • The sdk version command checks the installed SDKMAN version.

  • The sdk list commands display the installed versions of Java, Scala, and sbt.

Step 3: Running the Pipeline

When you push this .gitlab-ci.yml file to your GitLab repository, the pipeline will automatically run. The test-job will:

  • Use the custom Docker image.

  • Verify the SDKMAN installation.

  • List the installed SDKs (Java, Scala, and sbt).

Test Results

Conclusion

By creating a custom Ubuntu image with SDKMAN pre-installed, you can streamline your development and CI/CD workflows. This approach ensures consistency across environments and reduces setup time for your pipelines. You can extend this setup by adding more SDKs or tools as needed.

Ref:
SDKMAN Official Documentation
Docker Documentation
GitLab CI/CD Documentation

Happy coding! 🚀

Top comments (0)