stages: - test - build - deploy_to_test - deploy_to_production workflow: rules: - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH variables: DOCKER_BUILD_IMAGE_TAG: ":stable" DOCKER_IMAGE_TAG: ":latest" - if: $CI_COMMIT_REF_NAME != $CI_DEFAULT_BRANCH variables: DOCKER_BUILD_IMAGE_TAG: ":latest" DOCKER_IMAGE_TAG: ":$CI_COMMIT_REF_NAME" test-code: image: python:3.10 stage: test services: - postgres:11.0 variables: POSTGRES_DB: ldv-spec-db POSTGRES_USER: postgres POSTGRES_PASSWORD: "atdb123" script: - cd ldvspec - pip install -r requirements/dev.txt - python manage.py migrate --settings ldvspec.settings.ci - python manage.py test --settings ldvspec.settings.ci docker-test-build: variables: if: main # Official docker image. image: docker$DOCKER_BUILD_IMAGE_TAG before_script: - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY stage: build services: - docker:dind script: - docker build --pull -t "$CI_REGISTRY_IMAGE$DOCKER_IMAGE_TAG" ldvspec - docker push $CI_REGISTRY_IMAGE$DOCKER_IMAGE_TAG docker-build-master: # Official docker image. image: docker$DOCKER_BUILD_IMAGE_TAG stage: build services: - docker:dind before_script: - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY script: - docker build --pull -t "$CI_REGISTRY_IMAGE" ldvspec - docker push "$CI_REGISTRY_IMAGE" only: - main # deploy test/dev version on 'sdc-dev.astron.nl' docker-deploy-main-test: stage: deploy_to_test before_script: ## ## Install ssh-agent if not already installed, it is required by Docker. ## (change apt-get to yum if you use an RPM-based image) ## - 'command -v ssh-agent >/dev/null || ( apt-get update && apt-get install openssh-client rsync )' ## ## Run ssh-agent (inside the build environment) ## - eval $(ssh-agent -s) ## ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store ## We're using tr to fix line endings which makes ed25519 keys work ## without extra base64 encoding. ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556 ## ## ## Create the SSH directory and give it the right permissions ## - echo "$SSH_PRIVATE_KEY_USER_SDC" | tr -d '\r' | ssh-add - - mkdir -p ~/.ssh - chmod 700 ~/.ssh - ssh-keyscan dop814.astron.nl >> ~/.ssh/known_hosts - ssh-keyscan sdc-dev.astron.nl >> ~/.ssh/known_hosts - chmod 644 ~/.ssh/known_hosts script: ## deploy the docker-compose file and use it to spin up the containers - scp -O -o StrictHostKeyChecking=no ldvspec/docker/docker-compose-dev-cd.yml sdc@dop814.astron.nl:/docker_compose/ldvspec/docker-compose-dev-cd.yml - ssh -o StrictHostKeyChecking=no sdc@dop814.astron.nl "echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY" - ssh -o StrictHostKeyChecking=no sdc@dop814.astron.nl "docker pull "$CI_REGISTRY_IMAGE"" - ssh -o StrictHostKeyChecking=no sdc@dop814.astron.nl "docker-compose -p ldvspec -f /docker_compose/ldvspec/docker-compose-dev-cd.yml up -d" - echo "Application deployed" when: manual only: - main docker-deploy-main-production: # image: docker:latest stage: deploy_to_production before_script: ## ## Install ssh-agent if not already installed, it is required by Docker. ## (change apt-get to yum if you use an RPM-based image) ## - 'command -v ssh-agent >/dev/null || ( apt-get update && apt-get install openssh-client rsync )' ## ## Run ssh-agent (inside the build environment) ## - eval $(ssh-agent -s) ## ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store ## We're using tr to fix line endings which makes ed25519 keys work ## without extra base64 encoding. ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556 ## ## ## Create the SSH directory and give it the right permissions ## - echo "$SSH_PRIVATE_KEY_USER_SDC" | tr -d '\r' | ssh-add - - mkdir -p ~/.ssh - chmod 700 ~/.ssh - ssh-keyscan dop821.astron.nl >> ~/.ssh/known_hosts - ssh-keyscan sdc.astron.nl >> ~/.ssh/known_hosts - chmod 644 ~/.ssh/known_hosts script: ## deploy the docker-compose file and use it to spin up the containers - scp -O -o StrictHostKeyChecking=no ldvspec/docker/docker-compose-production-cd.yml sdco@dop821.astron.nl:/opt/dockercompose/ldvspec/docker-compose-production-cd.yml - ssh -o StrictHostKeyChecking=no sdco@dop821.astron.nl "echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY" - ssh -o StrictHostKeyChecking=no sdco@dop821.astron.nl "docker pull "$CI_REGISTRY_IMAGE"" - ssh -o StrictHostKeyChecking=no sdco@dop821.astron.nl "docker-compose -p ldvspec -f /opt/dockercompose/ldvspec/docker-compose-production-cd.yml up -d" - echo "Application deployed" when: manual only: - main