Backend/.gitlab-ci.yml

85 lines
3.5 KiB
YAML
Raw Permalink Normal View History

2021-02-11 13:25:32 -05:00
variables:
2021-02-12 22:26:32 -05:00
CI_REGISTRY_IMAGE_GRADLE: "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME-gradle"
CI_REGISTRY_IMAGE_BACKEND: "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"
2021-02-11 13:25:32 -05:00
2021-02-11 22:53:28 -05:00
before_script:
2021-02-11 13:25:32 -05:00
- docker info
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
2021-02-11 13:25:32 -05:00
stages:
- build
- test
- package
2021-02-11 14:51:58 -05:00
- deploy
2021-02-12 23:59:03 -05:00
.only-master:
only:
- master
2021-02-11 13:25:32 -05:00
2021-02-12 11:15:53 -05:00
build:
2021-02-11 13:25:32 -05:00
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-12 11:15:53 -05:00
test:
2021-02-11 13:25:32 -05:00
stage: test
2021-02-12 11:15:53 -05:00
needs: [ 'build' ]
2021-02-11 13:25:32 -05:00
variables:
2021-02-12 22:26:32 -05:00
TEST_CONTAINER_NAME: "cre_backend_gradle_tests-$CI_COMMIT_REF_NAME"
2021-02-11 13:25:32 -05:00
script:
2021-03-13 11:13:08 -05:00
- docker rm $TEST_CONTAINER_NAME || true
- docker run --name $TEST_CONTAINER_NAME $CI_REGISTRY_IMAGE_GRADLE gradle test
after_script:
- mkdir test-results && docker cp $TEST_CONTAINER_NAME:/usr/src/cre/build/test-results/test/ test-results
- docker rm $TEST_CONTAINER_NAME || true
artifacts:
when: always
reports:
junit: test-results/test/TEST-*.xml
2021-02-11 13:25:32 -05:00
2021-02-12 11:15:53 -05:00
package:
2021-02-11 13:25:32 -05:00
stage: package
2021-02-12 11:15:53 -05:00
needs: [ 'test' ]
2021-02-12 23:59:03 -05:00
extends: .only-master
2021-02-11 13:25:32 -05:00
variables:
2021-02-12 22:26:32 -05:00
PACKAGE_CONTAINER_NAME: "cre_backend_package-$CI_COMMIT_REF_NAME"
2021-02-12 11:15:53 -05:00
ARTIFACT_NAME: "ColorRecipesExplorer-backend-$CI_PIPELINE_IID"
2021-02-11 13:25:32 -05:00
script:
2021-03-13 11:13:08 -05:00
- docker rm $PACKAGE_CONTAINER_NAME || true
2021-05-28 18:18:01 -04:00
- docker run --name $PACKAGE_CONTAINER_NAME $CI_REGISTRY_IMAGE_GRADLE gradle bootJar -Pversion=$CI_PIPELINE_IID
2021-05-28 19:58:06 -04:00
- docker cp $PACKAGE_CONTAINER_NAME:/usr/src/cre/build/libs/ColorRecipesExplorer-$CI_PIPELINE_IID.jar $ARTIFACT_NAME.jar
- docker build -t $CI_REGISTRY_IMAGE_BACKEND --build-arg JDK_VERSION=$JDK_VERSION --build-arg PORT=$PORT --build-arg ARTIFACT_NAME=$ARTIFACT_NAME .
- 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
- docker image remove $CI_REGISTRY_IMAGE_GRADLE || true # Remove the gradle image from the runner so it doesn't pollute the runner's image registry
2021-02-11 13:25:32 -05:00
artifacts:
paths:
2021-02-12 11:15:53 -05:00
- $ARTIFACT_NAME.jar
2021-02-11 13:25:32 -05:00
expire_in: 1 week
2021-02-12 11:15:53 -05:00
deploy:
stage: deploy
image: alpine:latest
needs: [ 'package' ]
2021-02-12 23:59:03 -05:00
extends: .only-master
2021-02-11 19:38:11 -05:00
variables:
2021-02-12 22:26:32 -05:00
DEPLOYED_CONTAINER_NAME: "cre_backend-$CI_COMMIT_REF_NAME"
2021-02-11 14:51:58 -05:00
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
script:
2021-02-12 11:15:53 -05:00
- 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_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY && docker pull $CI_REGISTRY_IMAGE_BACKEND"
- ssh -p $DEPLOYMENT_SERVER_SSH_PORT $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER "docker run -d -p $PORT:$PORT --name=$DEPLOYED_CONTAINER_NAME -v $DATA_VOLUME:/usr/bin/cre/data -v $CONFIG_VOLUME:/usr/bin/cre/config -e spring_profiles_active=$SPRING_PROFILES $CI_REGISTRY_IMAGE_BACKEND"