Building an image with Git submodules
scutifer
PROOP

a year ago

Is there a recommended way to build images with Git submodules? Since your .dockerignore likely excludes .git, I cannot do a git submodule init && git submodule update --recursive and expect it to work.

Should I do a multi-stage build instead i.e git clone using alpine/git do submodule update, then copy that into a node:22 image for builds?

Solved

6 Replies

scutifer
PROOP

a year ago

A two stage build worked. But can we please get a pre-build command in Railway? Its useful in quite a few places.


a year ago

Hello,

I'm glad you solved the problem!

If you would like to request that as a feature, please open a thread here - https://station.railway.com/feedback

Best,

Brody


Status changed to Awaiting User Response Railway about 1 year ago


scutifer

A two stage build worked. But can we please get a pre-build command in Railway? Its useful in quite a few places.

a year ago

Hey, can you please share with me what you did here?


Status changed to Awaiting Railway Response Railway about 1 year ago


ritvik

Hey, can you please share with me what you did here?

chandrika
EMPLOYEE

a year ago

Hey Ritvik, will let them answer but also Railway has pre-deploy commands in case they're helpful for you https://docs.railway.com/guides/pre-deploy-command


Status changed to Awaiting User Response Railway about 1 year ago


a year ago

Pre-deploy commands execute between building and deploying your application

Not helpful, because I need to run git submodule update --init --recursive before the build step. Even if I could, running this inside a container is pointless as inside it there is no .git folder


Status changed to Awaiting Railway Response Railway about 1 year ago


scutifer
PROOP

a year ago

A pre-build, not a pre-deploy command is what's needed here. See https://station.railway.com/feedback/new-feature-request-pre-build-command-h-03c341f7

Here's a sample 2-stage build.

# Stage 1: Clone repository with submodules
FROM alpine/git:latest as repo

ARG GITHUB_TOKEN
ENV GITHUB_TOKEN=${GITHUB_TOKEN}

ARG RAILWAY_GIT_COMMIT_SHA
ENV RAILWAY_GIT_COMMIT_SHA=${RAILWAY_GIT_COMMIT_SHA}

RUN echo "Cache bust: ${RAILWAY_GIT_COMMIT_SHA}"

WORKDIR /repo

RUN git config --global url."https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/"
RUN git clone --depth=1 --single-branch --branch main ${GIT_REPO_URL} .
RUN git submodule init && git submodule update --depth=1

# Stage 2: Build the application
FROM node:22

## ... cache bust again if you need to ...

WORKDIR /app

# Copy repository files from git stage (including submodules)
COPY --from=repo /repo .

## ... your usual dockerfile ...

Status changed to Awaiting User Response chandrika about 1 year ago


Status changed to Solved scutifer about 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...