Backend/.gitlab-ci.yml

120 lines
4.6 KiB
YAML
Raw Normal View History

2021-02-11 13:25:32 -05:00
variables:
CI_REGISTRY_IMAGE_GRADLE: "$CI_REGISTRY_IMAGE:latest-backend-gradle"
2021-02-11 19:38:11 -05:00
CI_REGISTRY_IMAGE_NG: "$CI_REGISTRY_IMAGE:latest-frontend-ng"
2021-02-11 13:25:32 -05:00
CI_REGISTRY_IMAGE_BACKEND: "$CI_REGISTRY_IMAGE:latest-backend"
2021-02-11 19:38:11 -05:00
CI_REGISTRY_IMAGE_FRONTEND: "$CI_REGISTRY_IMAGE:latest-frontend"
2021-02-11 13:25:32 -05:00
before_script:
- docker info
- docker login -u $CI_USERNAME -p $CI_ACCESS_TOKEN $CI_REGISTRY
stages:
- build
- test
- package
2021-02-11 14:51:58 -05:00
- deploy
2021-02-11 13:25:32 -05:00
build-backend:
stage: build
script:
- docker pull $CI_REGISTRY_IMAGE_GRADLE || true
2021-02-11 19:38:11 -05:00
- docker build --cache-from $CI_REGISTRY_IMAGE_GRADLE -f gradle.Dockerfile -t $CI_REGISTRY_IMAGE_GRADLE --build-arg JDK_VERSION=$JDK_VERSION --build-arg GRADLE_VERSION=$GRADLE_VERSION .
2021-02-11 13:25:32 -05:00
- docker push $CI_REGISTRY_IMAGE_GRADLE
2021-02-11 19:38:11 -05:00
build-frontend:
stage: build
script:
- docker pull $CI_REGISTRY_IMAGE_NG || true
- docker build --cache-from $CI_REGISTRY_IMAGE_NG -f ng.Dockerfile -t $CI_REGISTRY_IMAGE_NG --build-arg JDK_VERSION=$JDK_VERSION --build-arg GRADLE_VERSION=$GRADLE_VERSION .
- docker push $CI_REGISTRY_IMAGE_NG
compile-kotlin-backend:
stage: test
needs: ['build-backend']
variables:
COMPILE_KOTLIN_CONTAINER_NAME: "cre_backend_gradle_compile_kotlin"
script:
- docker run --name $COMPILE_KOTLIN_CONTAINER_NAME --rm $CI_REGISTRY_IMAGE_GRADLE gradle compileKotlin
compile-java-backend:
stage: test
needs: ['build-backend']
variables:
COMPILE_JAVA_CONTAINER_NAME: "cre_backend_gradle_compile_java"
script:
- docker run --name $COMPILE_JAVA_CONTAINER_NAME --rm $CI_REGISTRY_IMAGE_JAVA gradle compileJava
2021-02-11 13:25:32 -05:00
test-backend:
stage: test
needs: ['build-backend']
variables:
TEST_CONTAINER_NAME: "cre_backend_gradle_tests"
script:
- docker run --name $TEST_CONTAINER_NAME --rm $CI_REGISTRY_IMAGE_GRADLE gradle test
package-backend:
stage: package
2021-02-11 19:40:52 -05:00
needs: ['test-backend']
2021-02-11 13:25:32 -05:00
variables:
2021-02-11 19:38:11 -05:00
PACKAGE_CONTAINER_NAME: "cre_backend_package"
2021-02-11 15:14:15 -05:00
ARTIFACT_NAME: "ColorRecipesExplorer-$CI_PIPELINE_IID"
2021-02-11 13:25:32 -05:00
script:
- docker run --name $PACKAGE_CONTAINER_NAME $CI_REGISTRY_IMAGE_GRADLE gradle buildBackend
2021-02-11 15:14:15 -05:00
- docker cp $PACKAGE_CONTAINER_NAME:/usr/src/cre/dist/backend/ColorRecipesExplorer.jar $ARTIFACT_NAME.jar
2021-02-11 13:25:32 -05:00
- docker rm $PACKAGE_CONTAINER_NAME
2021-02-11 15:18:34 -05:00
- docker build -t $CI_REGISTRY_IMAGE_BACKEND -f backend.Dockerfile --build-arg PORT=$BACKEND_PORT --build-arg ARTIFACT_NAME=$ARTIFACT_NAME .
2021-02-11 14:51:58 -05:00
- docker push $CI_REGISTRY_IMAGE_BACKEND
2021-02-11 19:38:11 -05:00
after_script:
- docker stop $PACKAGE_CONTAINER_NAME || true
2021-02-11 19:46:08 -05:00
- docker rm $PACKAGE_CONTAINER_NAME || true
2021-02-11 13:25:32 -05:00
artifacts:
paths:
- ColorRecipesExplorer-$CI_PIPELINE_IID.jar
expire_in: 1 week
2021-02-11 19:38:11 -05:00
package-frontend:
stage: package
2021-02-11 19:40:52 -05:00
needs: ['build-frontend']
2021-02-11 19:38:11 -05:00
variables:
PACKAGE_CONTAINER_NAME: "cre_frontend_package"
ARTIFACT_NAME: "ColorRecipesExplorer-$CI_PIPELINE_IID-ng"
before_script:
- apk update
- apk add --no-cache zip
- mkdir dist
script:
- docker run --name $PACKAGE_CONTAINER_NAME $CI_REGISTRY_IMAGE_NG gradle buildFrontend
- docker cp $PACKAGE_CONTAINER_NAME:/usr/src/mcd/dist/frontend/ dist/
- zip -r $ARTIFACT_NAME.zip dist/
after_script:
- docker stop $PACKAGE_CONTAINER_NAME || true
2021-02-11 19:46:08 -05:00
- docker rm $PACKAGE_CONTAINER_NAME || true
2021-02-11 19:38:11 -05:00
artifacts:
paths:
- $ARTIFACT_NAME.zip
expire_in: 1 week
2021-02-11 14:51:58 -05:00
.before_deploy: &before_deploy
before_script:
- apk update
- apk add --no-cache openssh-client
- mkdir -p ~/.ssh
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
- chmod 700 ~/.ssh/id_rsa
- eval $(ssh-agent -s)
2021-02-11 15:23:48 -05:00
- ssh-add ~/.ssh/id_rsa
2021-02-11 14:51:58 -05:00
- ssh-keyscan -p $DEPLOYMENT_SERVER_SSH_PORT -H $DEPLOYMENT_SERVER >> ~/.ssh/known_hosts
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
2021-02-11 14:53:32 -05:00
deploy-backend:
stage: deploy
image: alpine:latest
needs: ['package-backend']
variables:
DEPLOYED_CONTAINER_NAME: "cre_backend"
<<: *before_deploy
script:
- ssh -p $DEPLOYMENT_SERVER_SSH_PORT $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER "docker stop $DEPLOYED_CONTAINER_NAME || true && docker rm $DEPLOYED_CONTAINER_NAME || true"
- ssh -p $DEPLOYMENT_SERVER_SSH_PORT $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER "docker login -u $CI_USERNAME -p $CI_ACCESS_TOKEN $CI_REGISTRY && docker pull $CI_REGISTRY_IMAGE_BACKEND"
- ssh -p $DEPLOYMENT_SERVER_SSH_PORT $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER "docker run -d -p $BACKEND_PORT:$BACKEND_PORT --name=$DEPLOYED_CONTAINER_NAME -e spring_profiles_active=$BACKEND_PROFILES -e spring_datasource_username=$DB_USERNAME -e spring_datasource_password=$DB_PASSWORD -e spring_datasource_url=$DB_URL $CI_REGISTRY_IMAGE_BACKEND"