Skip to content

Commit

Permalink
ED-4000 feat: Github actions instead of Jenkins-test
Browse files Browse the repository at this point in the history
  • Loading branch information
princegupta1131 committed Jun 11, 2024
1 parent e53f653 commit 1fcfe64
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 80 deletions.
107 changes: 102 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,106 @@
# Dockerfile for the player setup
FROM node:18.20.2-slim
RUN useradd -u 1001 -md /home/sunbird sunbird
# Use a base image suitable for building the client and server
FROM node:20.13.1-bookworm-slim AS builder

# Arguments for CDN URL and buildCdnAssets
ARG cdnUrl=""
ENV cdnUrl=${cdnUrl}
ARG buildCdnAssets=""
ENV buildCdnAssets=${buildCdnAssets}

# Set the working directory for the client build
WORKDIR /usr/src/app/client

# Copy the client code into the Docker container
COPY src/app/client ./
RUN yarn cache clean

# Install client dependencies
RUN yarn install --no-progress --frozen-lockfile --production=true


# Build the client
RUN npm run build

# Build the client for CDN and inject CDN fallback
# Conditional build logic for CDN assets
RUN if [ "$buildCdnAssets" = "true" ]; then \
echo "Building client CDN assets..."; \
npm run build-cdn --deploy-url $cdnUrl && \
export sunbird_portal_cdn_url=$cdnUrl && \
npm run inject-cdn-fallback && \
echo "Completed client CDN prod build."; \
else \
echo "Skipping client CDN assets build."; \
fi

# Validate and copy CDN assets if they exist
RUN if [ "$buildCdnAssets" = "true" ]; then \
echo "Validating and copying CDN assets..."; \
if [ -d "src/app/dist-cdn" ] && [ "$(ls -A src/app/dist-cdn)" ]; then \
mkdir -p /usr/src/app/cdn_assets && \
cp -r src/app/dist-cdn/* /usr/src/app/cdn_assets/ && \
echo "CDN assets copied successfully."; \
else \
echo "Directory src/app/dist-cdn does not exist or is empty. Skipping copy."; \
fi; \
else \
echo "Skipping validation and copying of CDN assets."; \
fi
# Set the working directory for server build
WORKDIR /usr/src/app

# Copy package.json and yarn.lock for server
COPY src/app/package.json src/app/yarn.lock ./app_dist/

# Copy server-related files into the app_dist directory before installing dependencies
COPY src/app/libs ./app_dist/libs
COPY src/app/helpers ./app_dist/helpers
COPY src/app/proxy ./app_dist/proxy
COPY src/app/resourcebundles ./app_dist/resourcebundles
COPY src/app/framework.config.js ./app_dist/
COPY src/app/sunbird-plugins ./app_dist/sunbird-plugins
COPY src/app/routes ./app_dist/routes
COPY src/app/constants ./app_dist/constants
COPY src/app/controllers ./app_dist/controllers
COPY src/app/server.js ./app_dist/

# Install server dependencies in the app_dist directory
WORKDIR /usr/src/app/app_dist
RUN yarn install --no-progress --frozen-lockfile --ignore-engines --production=true

# Start a new stage for the final image
FROM node:20.13.1-bookworm-slim

# Set the commit hash as a build argument and environment variable
ARG commit_hash=""
ENV commit_hash=${commit_hash}

# Create a non-root user and group with specific UID and GID
RUN groupadd -g 1001 sunbird && \
useradd -u 1001 -g sunbird -m -d /home/sunbird sunbird

# Set the working directory and copy the built files
WORKDIR /home/sunbird
COPY --chown=sunbird . /home/sunbird/app_dist/
COPY --chown=sunbird:sunbird --from=builder /usr/src/app /home/sunbird

# Switch to the non-root user
USER sunbird

# Rename the index.html file to index.ejs
WORKDIR /home/sunbird/app_dist
RUN mv dist/index.html dist/index.ejs

# Print the commit hash
RUN echo "Commit Hash: ${commit_hash}"

# Add the build hash to package.json
RUN sed -i "/version/a\ \"buildHash\": \"${commit_hash}\"," package.json

# Run the build script to perform additional tasks (e.g., phraseAppPull)
RUN node helpers/resourceBundles/build.js -task="phraseAppPull"

# Expose the port used by the server
EXPOSE 3000
CMD ["node", "server.js", "&"]

# Start the server
CMD ["node", "server.js"]
80 changes: 5 additions & 75 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,93 +13,23 @@ node=$2
org=$3
buildDockerImage=$4
buildCdnAssests=$5
cdnUrl=""
echo "buildDockerImage: " $buildDockerImage
echo "buildCdnAssests: " $buildCdnAssests
## docker test start
if [ $buildCdnAssests == true ]
then
cdnUrl=$6
echo "cdnUrl: " $cdnUrl
fi

commit_hash=$(git rev-parse --short HEAD)
nvm install $NODE_VERSION # same is used in client and server

cd src/app
mkdir -p app_dist/ # this folder should be created prior server and client build
rm -rf dist-cdn # remove cdn dist folder

# function to run client build for docker image
build_client_docker(){
echo "starting client local prod build"
npm run build # Angular prod build
echo "completed client local prod build"
cd ..
mv app_dist/dist/index.html app_dist/dist/index.ejs # rename index file
}
# function to run client build for cdn
build_client_cdn(){
echo "starting client cdn prod build"
npm run build-cdn -- --deployUrl $cdnUrl # prod command
export sunbird_portal_cdn_url=$cdnUrl # required for inject-cdn-fallback task
npm run inject-cdn-fallback
echo "completed client cdn prod build"
}
# function to run client build
build_client(){
echo "Building client in background"
nvm use $NODE_VERSION
npm rebuild canvas
cd client
echo "starting client yarn install"
yarn install --no-progress --production=true
echo "completed client yarn install"
if [ $buildDockerImage == true ]
then
build_client_docker & # run client local build in background
fi
if [ $buildCdnAssests == true ]
then
build_client_cdn & # run client local build in background
fi
wait # wait for both build to complete
echo "completed client post_build"
}

# function to run server build
build_server(){
echo "Building server in background"
echo "copying requied files to app_dist"
cp -R libs helpers proxy resourcebundles package.json framework.config.js sunbird-plugins routes constants controllers server.js ./../../Dockerfile app_dist
cd app_dist
nvm use $NODE_VERSION
echo "starting server yarn install"
yarn install --ignore-engines --no-progress --production=true
echo "completed server yarn install"
node helpers/resourceBundles/build.js -task="phraseAppPull"
}

build_client & # run client build in background
if [ $buildDockerImage == true ]
then
build_server & # run client build in background
fi

## wait for both build to complete
wait

BUILD_ENDTIME=$(date +%s)
echo "Client and Server Build complete Took $[$BUILD_ENDTIME - $STARTTIME] seconds to complete."

if [ $buildDockerImage == true ]
then
cd app_dist
sed -i "/version/a\ \"buildHash\": \"${commit_hash}\"," package.json
echo "starting docker build"
docker build --no-cache --label commitHash=$(git rev-parse --short HEAD) -t ${org}/${name}:${build_tag} .
echo commit_hash $commit_hash
docker build --no-cache --build-arg buildCdnAssets=$buildCdnAssests --build-arg cdnUrl=$cdnUrl --build-arg commit_hash=$(git rev-parse --short HEAD) -t ${org}/${name}:${build_tag} .

echo "completed docker build"
cd ../../..
echo {\"image_name\" : \"${name}\", \"image_tag\" : \"${build_tag}\",\"commit_hash\" : \"${commit_hash}\", \"node_name\" : \"$node\"} > metadata.json
fi

ENDTIME=$(date +%s)
echo "build completed. Took $[$ENDTIME - $STARTTIME] seconds."

0 comments on commit 1fcfe64

Please sign in to comment.