Created
November 28, 2019 12:37
-
-
Save f4rx/999adcee80da77d8b97352cc2847b5e0 to your computer and use it in GitHub Desktop.
gitlab-ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| variables: | |
| IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_BUILD_REF_NAME | |
| GIT_STRATEGY: FETCH | |
| stages: | |
| - lints | |
| - build | |
| - test_create_infra | |
| - tests | |
| - test_destroy_infra | |
| - deploy | |
| lint1: | |
| stage: lints | |
| script: | |
| - echo lint1 | |
| lint2: | |
| stage: lints | |
| script: | |
| - echo lint2 | |
| build: | |
| only: | |
| refs: | |
| - master | |
| - tags | |
| stage: build | |
| script: | |
| - docker build -t f3ex/guestbookapp:latest -f Dockerfile_app . | |
| # - echo docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY | |
| # - echo docker build -t $IMAGE_TAG . | |
| # - echo docker push $IMAGE_TAG | |
| test_create_infra: | |
| stage: test_create_infra | |
| script: | |
| - if [ ! -f /tmp/gitlab-ci-key ]; then ssh-keygen -b 2048 -t rsa -f /tmp/gitlab-ci-key -C gitlab-ci@gitlab -q -N ""; fi | |
| - cd tests/terraform | |
| - terraform init | |
| - TF_VAR_domain_name=${DOMAIN_NAME} TF_VAR_project_id=${PROJECT_ID} TF_VAR_user_name=${USER_NAME} TF_VAR_user_password=${USER_PASSWORD} TF_VAR_region=${REGION} TF_VAR_az_zone=${AZ} TF_VAR_volume_type=${VOLUME_TYPE} TF_VAR_public_key=$(cat /tmp/gitlab-ci-key.pub) terraform apply -state=/home/gitlab-runner/terraform.tfstate -auto-approve | |
| tests: | |
| stage: tests | |
| script: | |
| - export TEST_SERVER=$(terraform output -state=/home/gitlab-runner/terraform.tfstate server_external_ip) | |
| - echo $TEST_SERVER | |
| - docker image save f3ex/guestbookapp:latest -o guestbookapp_image | |
| - rsync -aP -e "ssh -i /tmp/gitlab-ci-key -o 'UserKnownHostsFile=/dev/null' -o 'StrictHostKeyChecking=no'" ./ root@$TEST_SERVER:~/app/ | |
| - ssh -i /tmp/gitlab-ci-key -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" root@$TEST_SERVER -T "docker image load -i ~/app/guestbookapp_image" | |
| - ssh -i /tmp/gitlab-ci-key -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" root@$TEST_SERVER -T "cd ~/app/tests && docker-compose up -d" | |
| - sleep 5 | |
| - ssh -i /tmp/gitlab-ci-key -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" root@$TEST_SERVER -T "cd ~/app/tests && docker-compose exec -T -e APP_ADDR=127.0.0.1 guestbookapp pytest -v /app" | |
| test_destroy_infra: | |
| stage: test_destroy_infra | |
| script: | |
| - cd tests/terraform | |
| - ls -la | |
| - terraform init | |
| - TF_VAR_domain_name=${DOMAIN_NAME} TF_VAR_project_id=${PROJECT_ID} TF_VAR_user_name=${USER_NAME} TF_VAR_user_password=${USER_PASSWORD} TF_VAR_region=${REGION} TF_VAR_az_zone=${AZ} TF_VAR_volume_type=${VOLUME_TYPE} TF_VAR_public_key=$(cat /tmp/gitlab-ci-key.pub) terraform destroy -state=/home/gitlab-runner/terraform.tfstate -auto-approve | |
| - ls -l | |
| when: always | |
| deploy: | |
| stage: deploy | |
| script: | |
| - echo deploy | |
| when: manual |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment