This is web auto compile and build Gitlab CI, when CI is launched, this CI will pick up “web” GitLab runner and perform build static file, build docker image and deploy docker image to dev environment.
The runner is using shell as its executor.
variables:
DEVOPS_ROBOT_ACCOUNT: "devops@gosysops.com"
DEVOPS_DEVHUB_ACCOUNT: "robot$$gosysops"
DOCKER_IMAGE_REGISTRY: "devops.gosysops.com/artifactory/"
PROJECT_NAME: "melt-web"
SSH_PORT: 22
SSH_IP: s101.vpn.gosysops.com
stages:
- compile
- build
- deploy
Build Static file:
stage: compile
tags:
- "web"
cache:
paths:
- node_modules
- dist
- default.conf
script:
- echo "clean cache ..."
- echo "Building dist files ..."
- cnpm install
- npm run build
- ls -l
retry: 2
Build Docker Image:
stage: build
tags:
- "web"
cache:
paths:
- dist
- default.conf
- node_modules
policy: pull
script:
- ls -l
- echo "Printing Image name and tag"
- echo $DOCKER_TARGET_IMAGE_NAME_AND_VERSION_TAG
- echo "$DOCKER_TARGET_IMAGE_NAME_AND_VERSION_TAG" >> ./version.html
- echo "Building docker image ..."
- build_and_push_docker_image
- auto_change_version .version-x113 g.gosysops.com/voicecloud/practical-platform/melt-web.git
retry: 2
Deploy to dev environment:
stage: deploy
tags:
- web
script:
- git fetch origin $CI_COMMIT_REF_NAME
- git checkout $CI_COMMIT_REF_NAME
- git reset --hard origin/$CI_COMMIT_REF_NAME
- git branch --all
- git tag --list
- export COMMITVERSION=$(cat .version)
- echo "Project name is" $PROJECT_NAME
- echo "Project dir is" $DEVELOPER
- echo "Version is" $CI_COMMIT_REF_NAME-$COMMITVERSION-$CI_COMMIT_SHORT_SHA
- ssh -o StrictHostKeyChecking=no -i /home/gitlab-runner/id_rsa devops@192.168.0.101 "$DEVELOPER/upgrade.sh $PROJECT_NAME $CI_COMMIT_REF_NAME-$COMMITVERSION-$CI_COMMIT_SHORT_SHA"
- auto_change_version .version g.gosysops.com/station-ng/auth-server.git
retry: 2
when: manual
# ---------------------------------------------------------------------------
.auto_devops: &auto_devops |
# DevOps variables and functions
export CI_BUILD_NUMBER=$CI_COMMIT_SHA
export COMMITVERSION=$(cat .version)
auto_change_version(){
echo "version file is: ${1}"
echo "repo is: ${2}"
export CI_BUILD_VERSION=${COMMITVERSION%.*}.$(echo ${COMMITVERSION##*.}+1|bc)
echo "commit version is ${CI_BUILD_VERSION}"
git config --global user.name "DevOps Robot"
git config --global user.email "devops@gosysops.com"
git fetch origin $CI_COMMIT_REF_NAME
git checkout $CI_COMMIT_REF_NAME
git reset --hard origin/$CI_COMMIT_REF_NAME
git branch --all
git tag --list
sed -i 's#'${COMMITVERSION}'#'${CI_BUILD_VERSION}'#g' ${1}
cat ${1}
echo "commit and push"
git add -v ${1}
git commit -m "[skip ci] auto commit version is ${CI_BUILD_VERSION}"
git push http://devops:${DEVOPS_ROBOT_LOGIN_PASSWORD}@${2}
}
echo_ci_vriables() {
echo "Variable CI_COMMIT_SHA is" $CI_COMMIT_SHA;
echo "Variable CI_COMMIT_SHORT_SHA is" $CI_COMMIT_SHORT_SHA;
echo "Variable CI_COMMIT_TAG is" $CI_COMMIT_TAG;
echo "Variable CI_COMMIT_REF_NAME is" $CI_COMMIT_REF_NAME;
# Since $CI_COMMIT_REF_NAME resolves to the branch or tag name, and your branch-name can contain forward slashes (e.g., feature/my-feature), it is safer to use $CI_COMMIT_REF_SLUG as the image tag.
echo "Variable CI_COMMIT_REF_SLUG is" $CI_COMMIT_REF_SLUG;
echo "Variable CI_ENVIRONMENT_SLUG is" $CI_ENVIRONMENT_SLUG;
}
echo_docker_variables() {
#echo "Docker image registry is" $CI_REGISTRY;
echo "Docker image registry is" $DOCKER_IMAGE_REGISTRY;
echo "Target image 'name:tag' is" $DOCKER_TARGET_IMAGE_NAME_AND_VERSION_TAG;
}
build_and_push_docker_image() {
docker login -u "${DEVOPS_DEVHUB_ACCOUNT}" -p $DEVOPS_DEVHUB_PASSWORD $DOCKER_IMAGE_REGISTRY;
echo $DOCKER_TARGET_IMAGE_NAME_AND_VERSION_TAG > ./version.html
docker build --build-arg REVISION=$CI_COMMIT_SHA -t $DOCKER_TARGET_IMAGE_NAME_AND_VERSION_TAG .;
echo "starting printing DOCKER_TARGET_IMAGE_NAME_AND_VERSION_TAG"
echo $DOCKER_TARGET_IMAGE_NAME_AND_VERSION_TAG
docker push $DOCKER_TARGET_IMAGE_NAME_AND_VERSION_TAG;
}
before_script:
- *auto_devops
- DOCKER_TARGET_IMAGE_NAME_AND_LATEST_TAG=$DOCKER_IMAGE_REGISTRY/$CI_PROJECT_NAME:latest
- DOCKER_TARGET_IMAGE_NAME_AND_VERSION_TAG=$DOCKER_IMAGE_REGISTRY/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-$COMMITVERSION-$CI_COMMIT_SHORT_SHA
- echo_ci_vriables
- echo_docker_variables