Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
how_to_bats/bin/build.sh
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
112 lines (98 sloc)
2.21 KB
This file contains 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
| #!/bin/bash | |
| # This script builds the nodejs image for the application | |
| # with its devDependencies as ${application}-candidate:${environment} | |
| # so that it can be used for testing in later pipeline stages | |
| raise() | |
| { | |
| echo "${1}" >&2 | |
| } | |
| check_required_environment() { | |
| local required_env="CI_COMMIT_REF_SLUG CI_PROJECT_NAME" | |
| for reqvar in $required_env | |
| do | |
| if [ -z ${!reqvar} ] | |
| then | |
| raise "missing ENVIRONMENT ${reqvar}!" | |
| return 1 | |
| fi | |
| done | |
| } | |
| check_required_publish_environment() { | |
| [ ${DO_NOT_PUBLISH} ] && return | |
| local required_publish_env="CI_REGISTRY CI_REGISTRY_USER CI_REGISTRY_PASSWORD CI_REGISTRY_IMAGE" | |
| for reqvar in $required_publish_env | |
| do | |
| if [ -z ${!reqvar} ] | |
| then | |
| raise "missing ENVIRONMENT ${reqvar} REQUIRED TO PUBLISH! | |
| SET DO_NOT_PUBLISH=1 TO SKIP PUBLISHING TO THE GITLAB REGISTRY | |
| " | |
| return 1 | |
| fi | |
| done | |
| } | |
| login() { | |
| [ ${DO_NOT_PUBLISH} ] && return | |
| check_required_publish_environment || return 1 | |
| echo " | |
| logging into ${CI_REGISTRY} | |
| " | |
| dry_run && return | |
| docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY} | |
| } | |
| dry_run() { | |
| [ ${DRY_RUN} ] && raise "skipping for dry run" && return | |
| return 1 | |
| } | |
| build_candidate() { | |
| local candidate_image="${1}-candidate:${2}" | |
| raise " | |
| Building candidate ${candidate_image} | |
| " | |
| if ! dry_run | |
| then | |
| docker build --pull -t "${candidate_image}" . | |
| if [ $? -gt 0 ] | |
| then | |
| raise "Problem in the Build" | |
| return 1 | |
| fi | |
| fi | |
| publish_image "${candidate_image}" | |
| } | |
| publish_image() { | |
| [ ${DO_NOT_PUBLISH} ] && return | |
| local image="${1}" | |
| local publishable_image="${CI_REGISTRY_IMAGE}/${image}" | |
| echo " | |
| tagging ${image} as ${publishable_image} | |
| " | |
| if ! dry_run | |
| then | |
| docker tag "${image}" "${publishable_image}" | |
| if [ $? -gt 0 ] | |
| then | |
| raise "Problem Tagging" | |
| return 1 | |
| fi | |
| fi | |
| echo " | |
| pushing image to gitlab registry | |
| " | |
| dry_run && return | |
| docker push "${publishable_image}" | |
| } | |
| run_main() { | |
| check_required_environment || exit 1 | |
| login || exit 1 | |
| build_candidate "${CI_PROJECT_NAME}" "${CI_COMMIT_REF_SLUG}" || return 1 | |
| raise "ALL COMPLETE" | |
| } | |
| if [[ "${BASH_SOURCE[0]}" == "${0}" ]] | |
| then | |
| run_main | |
| if [ $? -gt 0 ] | |
| then | |
| exit 1 | |
| fi | |
| fi |