This commit is contained in:
FyloZ 2021-02-12 11:15:53 -05:00
parent 43c38a0eba
commit b55887a989
1 changed files with 21 additions and 67 deletions

View File

@ -1,8 +1,6 @@
variables:
CI_REGISTRY_IMAGE_GRADLE: "$CI_REGISTRY_IMAGE:latest-backend-gradle"
CI_REGISTRY_IMAGE_NG: "$CI_REGISTRY_IMAGE:latest-frontend-ng"
CI_REGISTRY_IMAGE_BACKEND: "$CI_REGISTRY_IMAGE:latest-backend"
CI_REGISTRY_IMAGE_FRONTEND: "$CI_REGISTRY_IMAGE:latest-frontend"
CI_REGISTRY_IMAGE_GRADLE: "$CI_REGISTRY_IMAGE:latest-gradle"
CI_REGISTRY_IMAGE: "$CI_REGISTRY_IMAGE:latest"
before_script:
- docker info
@ -14,72 +12,47 @@ stages:
- package
- deploy
build-backend:
build:
stage: build
script:
- docker pull $CI_REGISTRY_IMAGE_GRADLE || true
- 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 .
- docker push $CI_REGISTRY_IMAGE_GRADLE
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
test-backend:
test:
stage: test
needs: ['build-backend']
needs: [ 'build' ]
variables:
TEST_CONTAINER_NAME: "cre_backend_gradle_tests"
script:
- docker run --name $TEST_CONTAINER_NAME --rm $CI_REGISTRY_IMAGE_GRADLE gradle test
package-backend:
package:
stage: package
needs: ['test-backend']
needs: [ 'test' ]
variables:
PACKAGE_CONTAINER_NAME: "cre_backend_package"
ARTIFACT_NAME: "ColorRecipesExplorer-$CI_PIPELINE_IID"
ARTIFACT_NAME: "ColorRecipesExplorer-backend-$CI_PIPELINE_IID"
script:
- docker run --name $PACKAGE_CONTAINER_NAME $CI_REGISTRY_IMAGE_GRADLE gradle buildBackend
- docker cp $PACKAGE_CONTAINER_NAME:/usr/src/cre/dist/backend/ColorRecipesExplorer.jar $ARTIFACT_NAME.jar
- docker run --name $PACKAGE_CONTAINER_NAME $CI_REGISTRY_IMAGE_GRADLE gradle bootJar
- docker cp $PACKAGE_CONTAINER_NAME:/usr/src/cre/build/libs/ColorRecipesExplorer.jar $ARTIFACT_NAME.jar
- docker rm $PACKAGE_CONTAINER_NAME
- docker build -t $CI_REGISTRY_IMAGE_BACKEND -f Dockerfile --build-arg PORT=$BACKEND_PORT --build-arg ARTIFACT_NAME=$ARTIFACT_NAME .
- docker push $CI_REGISTRY_IMAGE_BACKEND
- docker build -t $CI_REGISTRY_IMAGE --build-arg JDK_VERSION=$JDK_VERSION --build-arg PORT=$PORT --build-arg ARTIFACT_NAME=$ARTIFACT_NAME .
- docker push $CI_REGISTRY_IMAGE
after_script:
- docker stop $PACKAGE_CONTAINER_NAME || true
- docker rm $PACKAGE_CONTAINER_NAME || true
artifacts:
paths:
- ColorRecipesExplorer-$CI_PIPELINE_IID.jar
- $ARTIFACT_NAME.jar
expire_in: 1 week
package-frontend:
stage: package
needs: ['build-frontend']
deploy:
stage: deploy
image: alpine:latest
needs: [ 'package' ]
variables:
PACKAGE_CONTAINER_NAME: "cre_frontend_package"
ARTIFACT_NAME: "ColorRecipesExplorer-$CI_PIPELINE_IID-ng"
script:
- apk update
- apk add --no-cache zip
- mkdir dist
- docker run --name $PACKAGE_CONTAINER_NAME $CI_REGISTRY_IMAGE_NG gradle buildFrontend -PfrontendConfiguration=$FRONTEND_CONFIGURATION
- docker cp $PACKAGE_CONTAINER_NAME:/usr/src/cre/dist/frontend/ dist/
- zip -r $ARTIFACT_NAME.zip dist/
- docker build -t $CI_REGISTRY_IMAGE_FRONTEND -f frontend.Dockerfile --build-arg ARTIFACT_NAME=$ARTIFACT_NAME .
- docker push $CI_REGISTRY_IMAGE_FRONTEND
after_script:
- docker stop $PACKAGE_CONTAINER_NAME || true
- docker rm $PACKAGE_CONTAINER_NAME || true
artifacts:
paths:
- $ARTIFACT_NAME.zip
expire_in: 1 week
.before_deploy: &before_deploy
DEPLOYED_CONTAINER_NAME: "cre_backend"
before_script:
- apk update
- apk add --no-cache openssh-client
@ -90,26 +63,7 @@ package-frontend:
- ssh-add ~/.ssh/id_rsa
- 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'
script:
- ssh -p $DEPLOYMENT_SERVER_SSH_PORT $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER "docker stop $DEPLOYED_CONTAINER_NAME || true && docker rm $DEPLOYED_CONTAINER_NAME || true"
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 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"
deploy-frontend:
stage: deploy
image: alpine:latest
needs: ['package-frontend']
variables:
DEPLOYED_CONTAINER_NAME: "cre_frontend"
<<: *before_deploy
script:
- 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_FRONTEND"
- ssh -p $DEPLOYMENT_SERVER_SSH_PORT $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER "docker run -d -p $FRONTEND_PORT:80 --name=$DEPLOYED_CONTAINER_NAME $CI_REGISTRY_IMAGE_FRONTEND"
- 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"
- ssh -p $DEPLOYMENT_SERVER_SSH_PORT $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER "docker run -d -p $PORT:$PORT --name=$DEPLOYED_CONTAINER_NAME -e spring_profiles_active=$SPRING_PROFILES -e spring_datasource_username=$DB_USERNAME -e spring_datasource_password=$DB_PASSWORD -e spring_datasource_url=$DB_URL $CI_REGISTRY_IMAGE"