Skip to content
Snippets Groups Projects
Commit e8421ced authored by Sergio Gonzalez Diaz's avatar Sergio Gonzalez Diaz
Browse files

gitlab-ci.yml_generator.py

parent b68c04f3
No related branches found
No related tags found
1 merge request!54Release 2.0.0
......@@ -37,7 +37,11 @@ build {microservice}:
- docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile ./src/
- docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
- docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
after_script:
- docker rmi $(docker images --quiet --filter=dangling=true)
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"'
- changes:
- src/$IMAGE_NAME/*.{{py,in,yml}}
- src/$IMAGE_NAME/Dockerfile
......@@ -46,8 +50,8 @@ build {microservice}:
- manifests/$IMAGE_NAME.yaml
- .gitlab-ci.yml
# test if the Docker image can be pulled from the gitlab registry
test {microservice} pull:
# apply unit test to the {microservice} component
unit test {microservice}:
variables:
IMAGE_NAME: '{microservice}' # name of the microservice
IMAGE_TAG: '{tag}' # tag of the container image (production, development, etc)
......@@ -56,55 +60,22 @@ test {microservice} pull:
- build {microservice}
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
- if docker network list | grep teraflowbridge; then echo "teraflowbridge is already created"; else docker network create -d bridge teraflowbridge; fi
- if docker container ls | grep influxdb; then docker rm -f influxdb; else echo "influxdb image is not in the system"; fi
- if docker container ls | grep $IMAGE_NAME; then docker rm -f $IMAGE_NAME; else echo "$IMAGE_NAME image is not in the system"; fi
script:
- docker pull "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
rules:
- changes:
- src/$IMAGE_NAME/*.{{py,in,yml}}
- src/$IMAGE_NAME/Dockerfile
- src/$IMAGE_NAME/tests/*.py
- src/$IMAGE_NAME/tests/Dockerfile
- manifests/$IMAGE_NAME.yaml
- .gitlab-ci.yml
# test if the Docker image can be executed
test {microservice} run:
variables:
IMAGE_NAME: '{microservice}' # name of the microservice
IMAGE_TAG: '{tag}' # tag of the container image (production, development, etc)
stage: test
needs:
- build {microservice}
before_script:
- if docker network list | grep teraflowbridge; then echo "teraflowbridge is already created"; else docker network create -d bridge teraflowbridge; fi
script:
- docker run -d -p 7070:7070 --name $IMAGE_NAME --network=teraflowbridge --rm "$IMAGE_NAME:$IMAGE_TAG"
- docker ps
- docker run --name influxdb -d -p 8086:8086 -e INFLUXDB_DB=$INFLUXDB_DATABASE -e INFLUXDB_ADMIN_USER=$INFLUXDB_USER -e INFLUXDB_ADMIN_PASSWORD=$INFLUXDB_PASSWORD -e INXLUXDB_HTTP_AUTH_ENABLED=True --network=teraflowbridge --rm influxdb:1.8
- docker run --name $IMAGE_NAME -d -p 7070:7070 --env INFLUXDB_USER=$INFLUXDB_USER --env INFLUXDB_PASSWORD=$INFLUXDB_PASSWORD --env INFLUXDB_DATABASE=$INFLUXDB_DATABASE --env INFLUXDB_HOSTNAME=influxdb -v "$PWD/src/$IMAGE_NAME/tests:/opt/results" --network=teraflowbridge --rm $CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG
- docker ps -a
- docker exec -i $IMAGE_NAME bash -c "pytest --junitxml=/opt/results/report.xml"
after_script:
- docker stop {microservice}
- docker rm -f $IMAGE_NAME
- docker rm -f influxdb
- docker network rm teraflowbridge
rules:
- changes:
- src/$IMAGE_NAME/*.{{py,in,yml}}
- src/$IMAGE_NAME/Dockerfile
- src/$IMAGE_NAME/tests/*.py
- src/$IMAGE_NAME/tests/Dockerfile
- manifests/$IMAGE_NAME.yaml
- .gitlab-ci.yml
# apply unit test to the {microservice} component
test {microservice} pytest:
variables:
IMAGE_NAME: '{microservice}' # name of the microservice
IMAGE_NAME_TEST: '{microservice}-test' # name of the microservice
IMAGE_TAG: '{tag}' # tag of the container image (production, development, etc)
stage: test
needs:
- build {microservice}
script:
- docker build -t "$IMAGE_NAME_TEST:$IMAGE_TAG" -f ./src/$IMAGE_NAME/tests/Dockerfile ./src/
- docker run -v "$PWD/src/$IMAGE_NAME/tests:/opt/results" $IMAGE_NAME_TEST:$IMAGE_TAG
rules:
- changes:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"'
- src/$IMAGE_NAME/*.{{py,in,yml}}
- src/$IMAGE_NAME/Dockerfile
- src/$IMAGE_NAME/tests/*.py
......@@ -116,65 +87,32 @@ test {microservice} pytest:
reports:
junit: src/$IMAGE_NAME/tests/report.xml
# Deployment of the {microservice} service in development Kubernetes Cluster
deploy {microservice} development:
# Deployment of the {microservice} service in Kubernetes Cluster
deploy {microservice}:
variables:
IMAGE_NAME: '{microservice}' # name of the microservice
IMAGE_TAG: '{tag}' # tag of the container image (production, development, etc)
stage: deploy
needs:
- build {microservice}
- test {microservice} run
- unit test {microservice}
# - dependencies all
# - integ_test execute
script:
- 'sed -i "s/$IMAGE_NAME:.*/$IMAGE_NAME:$IMAGE_TAG/" manifests/$IMAGE_NAME.yaml'
- kubectl version
- kubectl get all
- kubectl apply -f "manifests/$IMAGE_NAME.yaml"
- kubectl get all
# environment:
# name: development
# name: test
# url: https://example.com
# kubernetes:
# namespace: development
# namespace: test
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop"'
when: manual
changes:
- src/$IMAGE_NAME/*.{{py,in,yml}}
- src/$IMAGE_NAME/Dockerfile
- src/$IMAGE_NAME/tests/*.py
- src/$IMAGE_NAME/tests/Dockerfile
- manifests/$IMAGE_NAME.yaml
- .gitlab-ci.yml
# Deployment of the {microservice} service in production Kubernetes Cluster
deploy {microservice} development:
variables:
IMAGE_NAME: '{microservice}' # name of the microservice
IMAGE_TAG: '{tag}' # tag of the container image (production, development, etc)
stage: deploy
needs:
- build {microservice}
- test {microservice} run
script:
- kubectl version
- kubectl get all
- kubectl apply -f "manifests/$IMAGE_NAME.yaml"
- kubectl get all
# environment:
# name: production
# url: https://example.com
# kubernetes:
# namespace: production
rules:
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"'
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH'
when: manual
changes:
- src/$IMAGE_NAME/*.{{py,in,yml}}
- src/$IMAGE_NAME/Dockerfile
- src/$IMAGE_NAME/tests/*.py
- src/$IMAGE_NAME/tests/Dockerfile
- manifests/$IMAGE_NAME.yaml
- .gitlab-ci.yml
"""
f.write(yml_template.format(microservice = args.microservice, tag=args.tag))
print("File created in the following path: {file}".format(file=file))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment