Sunday, October 20, 2024

github actions use custom docker image as runner

 Below is the Dockerfile I am using.

# Base image: Ubuntu 22.04
FROM ubuntu:22.04

# Arguments to specify versions
ARG OPENJDK_VERSION=17
ARG MAVEN_VERSION=3.8.8
ARG NODE_VERSION=18

# Update package list and install dependencies
RUN apt-get update && apt-get install -y \
    openjdk-${OPENJDK_VERSION}-jdk \
    nano \
    git \
    curl \
    gnupg2 \
    ca-certificates \
    lsb-release \
    software-properties-common \
    apt-transport-https \
    wget

# Install Maven
RUN curl -o apache-maven-${MAVEN_VERSION}-bin.tar.gz "https://dlcdn.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz" && ls -la
RUN tar -xvzf apache-maven-${MAVEN_VERSION}-bin.tar.gz && ls -la
RUN mv apache-maven-${MAVEN_VERSION} /opt/apache-maven-${MAVEN_VERSION} && ln -s /opt/apache-maven-${MAVEN_VERSION}/bin/mvn /usr/bin/mvn

# Install Node.js, npm, and Yarn
RUN curl -sL https://deb.nodesource.com/setup_${NODE_VERSION}.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g npm \
    && npm install -g yarn \
    && apt-get clean

# Install Azure CLI
RUN curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg > /dev/null
RUN AZ_REPO=$(lsb_release -cs) \
    && echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ ${AZ_REPO} main" | tee /etc/apt/sources.list.d/azure-cli.list
RUN apt-get update && apt-get install -y azure-cli

# Install Docker (make sure Docker is installed inside the image)
RUN apt-get update && \
    apt-get install -y \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

# Add Docker’s official GPG key
RUN mkdir -p /etc/apt/keyrings && \
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
    gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Set up the Docker repository
RUN echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker Engine
RUN apt-get update && \
    apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# Set environment variables
ENV JAVA_HOME /usr/lib/jvm/java-${OPENJDK_VERSION}-openjdk-amd64
ENV MAVEN_HOME /opt/apache-maven-${MAVEN_VERSION}
ENV PATH $MAVEN_HOME/bin:$PATH

# Set the default command for the container
CMD ["bash"]

Now, to build this image, I am using below github workflow by adding relevant secrets to repo.


name: Build and Push Docker Image

on:
  push:
    branches:
      - main
      - az
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      # Checkout the repository
      - name: Checkout code
        uses: actions/checkout@v3

      # Set up Docker Buildx (for better multi-platform builds)
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      # Log in to Docker Hub
      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

      # Build the Docker image
      - name: Build Docker image
        run: |
          docker build \
            -t ${{ secrets.DOCKER_HUB_USERNAME }}/ubuntu-openjdk17-maven-node-az:${{ github.run_number }} .

      # Push the Docker image to Docker Hub
      - name: Push Docker image
        run: |
          docker push ${{ secrets.DOCKER_HUB_USERNAME }}/ubuntu-openjdk17-maven-node-az:${{ github.run_number }}
          docker tag  ${{ secrets.DOCKER_HUB_USERNAME }}/ubuntu-openjdk17-maven-node-az:${{ github.run_number }} ${{ secrets.DOCKER_HUB_USERNAME }}/ubuntu-openjdk17-maven-node-az:latest
          docker push ${{ secrets.DOCKER_HUB_USERNAME }}/ubuntu-openjdk17-maven-node-az:latest

      # Show Docker image
      - name: Show Docker image
        run: |
          echo ${{ secrets.DOCKER_HUB_USERNAME }}/ubuntu-openjdk17-maven-node-az:${{ github.run_number }}
          echo ${{ secrets.DOCKER_HUB_USERNAME }}/ubuntu-openjdk17-maven-node-az:latest

Once the above image is pushed to dockerhub. You can reference that docker image in the github workflows.

name: Docker Test Workflow

on:  
  workflow_dispatch:
  push:
    branches: [test*, 12.5*, Cloud*, 14.*]

jobs:
  test:
    name: Test Job in Docker
    runs-on: githubrunner  # This will run on your self-hosted runner
    container:            # Use a Docker container
      image: udayglobuslive/ubuntu-openjdk17-maven-node-az:25
      options: --rm       # Remove the container after the job is complete
    steps:
      - name: Print system info
        run: |
          echo "Running inside a Docker container"
          uname -a  # Prints system information
          hostname
          
      - name: Create and list a directory
        run: |
          mkdir test_dir
          echo "Created test_dir"
          ls -l  # List the contents of the current directory
          
      - name: Cleanup
        run: |
          rm -rf test_dir
          echo "test_dir removed"


Where, everytime it will use the self hosted runner and create a new container and destroy at the end of pipeline.

Instead of self-hosted runner, you can use github provided runner also.
Like, simply replace githubrunner above and keep ubuntu-latest.

name: Docker Test Workflow

on:  
  workflow_dispatch:
  push:
    branches: [test*, 12.5*, Cloud*, 14.*]

jobs:
  test:
    name: Test Job in Docker
    runs-on: ubuntu-latest  # This will run on github provided runner
    container:            # Use a Docker container
      image: udayglobuslive/ubuntu-openjdk17-maven-node-az:25
      options: --rm       # Remove the container after the job is complete
    steps:
      - name: Print system info
        run: |
          echo "Running inside a Docker container"
          uname -a  # Prints system information
          hostname
          
      - name: Create and list a directory
        run: |
          mkdir test_dir
          echo "Created test_dir"
          ls -l  # List the contents of the current directory
          
      - name: Cleanup
        run: |
          rm -rf test_dir
          echo "test_dir removed"

No comments:

Post a Comment