From bf1c2e76f8683a73bafb217053b5d3e919899095 Mon Sep 17 00:00:00 2001 From: Ricardo Rosales Date: Thu, 23 Jul 2020 15:44:15 -0500 Subject: [PATCH] Adding 2 different environment setups (#2) * Adding 2 different environment setups * docker now can change working_directory * Adding simple_docker_tag * Adding github_org to DOCKER_PREFIX when using hub.docker.com * Now you can override the name of the container * simple_docker_tag is now boolean * DOCKER_PREFIX and not Docker_PREFIX * evaluate DOCKER_REGISTRY_HOST --- src/orb.yml | 290 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 262 insertions(+), 28 deletions(-) diff --git a/src/orb.yml b/src/orb.yml index 0459708..04b6b03 100644 --- a/src/orb.yml +++ b/src/orb.yml @@ -69,10 +69,6 @@ references: echo "nexusUsername=${NEXUS_USER} nexusPassword=${NEXUS_PASS}" > ~/.gradle/gradle.properties - # Update Google Chrome and heroku keys - wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add - - wget -qO- https://cli-assets.heroku.com/install-ubuntu.sh | sh - # Docker if [ ! -f /etc/docker/daemon.json ]; then sudo mkdir /etc/docker || true @@ -85,6 +81,31 @@ references: sudo cp ~/.docker/config.json "${ROOT_HOME}"/.docker/config.json sudo chown root: "${ROOT_HOME}"/.docker/config.json + setup_public_registries: &setup_public_registries + run: + name: Set up all public registries + command: | + # Print environment + echo "Environment configuration ====================" + env | egrep -v '.*_PASS|.*_CONFIG' | awk -F'=' '{st = index($0,"="); printf("\033[0;35m%-50s\033[0m= \"%s\"\n", $1, substr($0,st+1))}' + echo "==============================================" + + # Docker + DOCKER_AUTH=$(echo -n "${DOCKERHUB_USER}:${DOCKERHUB_PASS}" | base64) + if [ "${DOCKER_REGISTRY_HOST}" == 'hub.docker.com' ]; then + DOCKER_REGISTRY_HOST='https://index.docker.io/v1/' + fi + + if [ ! -f /etc/docker/daemon.json ]; then + sudo mkdir /etc/docker || true + echo '{}' | sudo tee /etc/docker/daemon.json + fi + mkdir ~/.docker || true + sudo mkdir ${ROOT_HOME}/.docker || true + echo "{\"auths\": { \"${DOCKER_REGISTRY_HOST}\": {\"auth\": \"${DOCKER_AUTH}\",\"email\": \"${CI_EMAIL}\"}}}" > ~/.docker/config.json + sudo cp ~/.docker/config.json "${ROOT_HOME}"/.docker/config.json + sudo chown root: "${ROOT_HOME}"/.docker/config.json + # OpenVPN commands - required for Docker machines setup_vpn: &setup_vpn run: @@ -198,6 +219,10 @@ references: description: Directory where the coverage report in xml is saved to type: string default: 'target/surefire-reports' + use_nexus: + description: Set True if you are using your own nexus host + type: boolean + default: false # Long npm-related commands npm_params: &npm_params @@ -216,6 +241,10 @@ references: description: Directory where the coverage report in xml is saved to type: string default: 'coverage/junit' + use_nexus: + description: Set True if you are using your own nexus host + type: boolean + default: false # Long pipenv-related commands pipenv_params: &pipenv_params @@ -234,6 +263,10 @@ references: description: Directory where the coverage report in xml is saved to type: string default: 'tmp/test-output' + use_nexus: + description: Set True if you are using your own nexus host + type: boolean + default: false pipenv_envsetup: &pipenv_envsetup run: @@ -283,6 +316,31 @@ references: pipenv run python setup.py sdist bdist_wheel # Docker goodies + docker_params: &docker_params + parameters: + working_directory: + description: Directory containing Dockerfile + type: string + default: '' + after_checkout_commands: + type: string + description: commands to run right after checkout + default: | + echo "No custom commands were configure to run" + echo " configured to run after checkout " + use_nexus: + description: Set True if you are using your own nexus host + type: boolean + default: false + simple_docker_tag: + type: boolean + description: use simple docker tag + default: false + container_name: + description: Name for this container + type: string + default: 'notset' + start_docker: &start_docker run: name: Start Docker daemon and add CircleCI to allowed users @@ -293,12 +351,25 @@ references: docker_build: &docker_build run: name: Build Docker image + <<: *common_step_params # TODO: break if more than one HEAD command: | if [ -z "${CIRCLE_BRANCH}" ]; then TAG_OR_BRANCH="${CIRCLE_TAG}"; else TAG_OR_BRANCH="${CIRCLE_BRANCH}"; fi SANITIZED_CIRCLE_BRANCH=$(printf ${TAG_OR_BRANCH} | tr '[/]' '[_]') VERSION=$(git describe --tags || git rev-parse --short HEAD) - DOCKER_TAG="${DOCKER_REGISTRY_HOST}/${CIRCLE_PROJECT_REPONAME}:ci.${SANITIZED_CIRCLE_BRANCH}.${CIRCLE_BUILD_NUM}.${VERSION}" + CONTAINER_NAME="${CIRCLE_PROJECT_REPONAME}" + if [ '<< parameters.container_name >>' != 'notset' ]; then + CONTAINER_NAME='<< parameters.container_name >>' + fi + DOCKER_PREFIX="${DOCKER_REGISTRY_HOST}/${CONTAINER_NAME}" + if [ "${DOCKER_REGISTRY_HOST}" == 'hub.docker.com' ]; then + DOCKER_PREFIX="${CIRCLE_PROJECT_USERNAME}/${CONTAINER_NAME}" + fi + TAG_FORMAT="ci.${SANITIZED_CIRCLE_BRANCH}.${CIRCLE_BUILD_NUM}.${VERSION}" + if [ '<< parameters.simple_docker_tag >>' == 'true' ]; then + TAG_FORMAT="${SANITIZED_CIRCLE_BRANCH}" + fi + DOCKER_TAG="${DOCKER_PREFIX}:${TAG_FORMAT}" echo "${VERSION}" > .VERSION echo "Building ${DOCKER_TAG} ..." docker build -t "${DOCKER_TAG}" . @@ -307,6 +378,7 @@ references: docker_push: &docker_push run: name: Push Docker image + <<: *common_step_params command: | docker push "$(cat .docker_tag)" @@ -314,10 +386,26 @@ references: # Commands - CircleCI build steps that can be referenced by name in a job # commands: - # Must call this for every build to set up the environment! +# CircleCI specific + fix_circleci: + description: Fix circleci image issue - must call this before every build + steps: + - run: + name: commands to workaround circleci issues + command: | + # Update Google Chrome and heroku keys + wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add - + wget -qO- https://cli-assets.heroku.com/install-ubuntu.sh | sh + + load_workspace: + description: Load previously persisted workspace + steps: + - *load_workspace + + # Must call this for every build to set up the environment! # Used for lightweight (docker) executors only - setup_build_env: - description: Set up the build environment + setup_nexus_build_env: + description: Set up the build environment to use a private nexus host steps: - *setup_registries - *setup_vpn_ssh @@ -326,8 +414,8 @@ commands: # Must call this for every machine-type build # Used for building docker containers - setup_docker_build_env: - description: Set up the build environment for Docker images + setup_nexus_docker_build_env: + description: Set up the build environment for Docker images using a private nexus host steps: - *setup_registries - *setup_vpn @@ -335,11 +423,20 @@ commands: - *wait_vpn - *start_docker - # CircleCI specific - load_workspace: - description: Load previously persisted workspace + # Must call this for every build to set up the environment! + # Used for lightweight (docker) executors only + setup_public_build_env: + description: Set up the build environment steps: - - *load_workspace + - *setup_public_registries + + # Must call this for every machine-type build + # Used for building docker containers + setup_public_docker_build_env: + description: Set up the build environment for Docker images + steps: + - *setup_public_registries + - *start_docker # Maven specific mvn_build: @@ -456,10 +553,12 @@ commands: # Docker specific docker_build: description: Build a docker image with the appropriate tags + <<: *docker_params steps: - *docker_build docker_push: description: Publish a docker image to the Nexus Docker repository + <<: *docker_params steps: - *docker_push @@ -477,9 +576,14 @@ commands: task_name: type: string description: task name to update + working_directory: + description: Directory containing Dockerfile + type: string + default: '' steps: - run: name: Deploy built docker image to environment + working_directory: << parameters.working_directory >> command: | DOCKER_TAG="$(cat .docker_tag)" curl \ @@ -562,7 +666,16 @@ jobs: <<: *npm_params executor: nodejs_10 steps: - - setup_build_env + - fix_circleci + - run: echo "use_nexus is << parameters.use_nexus >>" + - when: + condition: << parameters.use_nexus >> + steps: + - setup_nexus_build_env + - unless: + condition: << parameters.use_nexus >> + steps: + - setup_public_build_env - checkout - after_checkout: <<: *after_checkout_workflow_params @@ -576,7 +689,16 @@ jobs: <<: *npm_params executor: nodejs_10 steps: - - setup_build_env + - fix_circleci + - run: echo "use_nexus is << parameters.use_nexus >>" + - when: + condition: << parameters.use_nexus >> + steps: + - setup_nexus_build_env + - unless: + condition: << parameters.use_nexus >> + steps: + - setup_public_build_env - checkout - after_checkout: <<: *after_checkout_workflow_params @@ -588,7 +710,16 @@ jobs: <<: *mvn_params executor: java_8 steps: - - setup_build_env + - fix_circleci + - run: echo "use_nexus is << parameters.use_nexus >>" + - when: + condition: << parameters.use_nexus >> + steps: + - setup_nexus_build_env + - unless: + condition: << parameters.use_nexus >> + steps: + - setup_public_build_env - checkout - after_checkout: <<: *after_checkout_workflow_params @@ -602,7 +733,16 @@ jobs: <<: *mvn_params executor: java_8 steps: - - setup_build_env + - fix_circleci + - run: echo "use_nexus is << parameters.use_nexus >>" + - when: + condition: << parameters.use_nexus >> + steps: + - setup_nexus_build_env + - unless: + condition: << parameters.use_nexus >> + steps: + - setup_public_build_env - checkout - after_checkout: <<: *after_checkout_workflow_params @@ -612,7 +752,16 @@ jobs: <<: *mvn_params executor: java_8 steps: - - setup_build_env + - fix_circleci + - run: echo "use_nexus is << parameters.use_nexus >>" + - when: + condition: << parameters.use_nexus >> + steps: + - setup_nexus_build_env + - unless: + condition: << parameters.use_nexus >> + steps: + - setup_public_build_env - checkout - after_checkout: <<: *after_checkout_workflow_params @@ -624,7 +773,16 @@ jobs: <<: *pipenv_params executor: python_374 steps: - - setup_build_env + - fix_circleci + - run: echo "use_nexus is << parameters.use_nexus >>" + - when: + condition: << parameters.use_nexus >> + steps: + - setup_nexus_build_env + - unless: + condition: << parameters.use_nexus >> + steps: + - setup_public_build_env - checkout - after_checkout: <<: *after_checkout_workflow_params @@ -638,7 +796,16 @@ jobs: <<: *pipenv_params executor: python_374 steps: - - setup_build_env + - fix_circleci + - run: echo "use_nexus is << parameters.use_nexus >>" + - when: + condition: << parameters.use_nexus >> + steps: + - setup_nexus_build_env + - unless: + condition: << parameters.use_nexus >> + steps: + - setup_public_build_env - checkout - after_checkout: <<: *after_checkout_workflow_params @@ -650,7 +817,16 @@ jobs: <<: *pipenv_params executor: python_374 steps: - - setup_build_env + - fix_circleci + - run: echo "use_nexus is << parameters.use_nexus >>" + - when: + condition: << parameters.use_nexus >> + steps: + - setup_nexus_build_env + - unless: + condition: << parameters.use_nexus >> + steps: + - setup_public_build_env - checkout - after_checkout: <<: *after_checkout_workflow_params @@ -659,13 +835,31 @@ jobs: - pipenv_test: <<: *common_step_params build_docker: + <<: *docker_params executor: docker steps: - - setup_docker_build_env + - fix_circleci + - run: + name: show use_nexus value + command: echo "use_nexus is << parameters.use_nexus >>" + - when: + condition: << parameters.use_nexus >> + steps: + - setup_nexus_docker_build_env + - unless: + condition: << parameters.use_nexus >> + steps: + - setup_public_docker_build_env - checkout + - after_checkout: + <<: *after_checkout_workflow_params - load_workspace - - docker_build - - docker_push + - docker_build: + <<: *common_step_params + simple_docker_tag: << parameters.simple_docker_tag >> + container_name: << parameters.container_name >> + - docker_push: + <<: *common_step_params build_docker_and_deploy: parameters: deploy_hook_url: @@ -679,17 +873,57 @@ jobs: deploy_task_name: type: string description: task name to update + working_directory: + description: Directory containing Dockerfile + type: string + default: '' + after_checkout_commands: + type: string + description: commands to run right after checkout + default: | + echo "No custom commands were configure to run" + echo " configured to run after checkout " + use_nexus: + description: Set True if you are using your own nexus host + type: boolean + default: false + simple_docker_tag: + type: boolean + description: use simple docker tag + default: false + container_name: + description: Name for this container + type: string + default: 'notset' executor: docker steps: - - setup_docker_build_env + - fix_circleci + - run: + name: show use_nexus value + command: echo "use_nexus is << parameters.use_nexus >>" + - when: + condition: << parameters.use_nexus >> + steps: + - setup_nexus_docker_build_env + - unless: + condition: << parameters.use_nexus >> + steps: + - setup_public_docker_build_env - checkout + - after_checkout: + <<: *after_checkout_workflow_params - load_workspace - - docker_build - - docker_push + - docker_build: + <<: *common_step_params + simple_docker_tag: << parameters.simple_docker_tag >> + container_name: << parameters.container_name >> + - docker_push: + <<: *common_step_params - docker_deploy: hook_url: << parameters.deploy_hook_url >> path: << parameters.deploy_path >> task_name: << parameters.deploy_task_name >> + working_directory: << parameters.working_directory >> # # User examples showing how to use this Orb