github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/contrib/nightly/upload.sh (about)

     1  #!/bin/bash
     2  #
     3  # This would build the binaries and docker image, and upload them to Github nightly build and Docker
     4  # nightly tag.
     5  # This also does the release, if run from the release branch on Travis.
     6  
     7  set -e
     8  
     9  # TODO (pawan) - This file declares a lot of redundant variables. Simplify it.
    10  # This script is run when
    11  # 1. A cronjob is run on master which happens everyday and updates the nightly tag.
    12  # 2. A new tag is pushed i.e. when we make a new release.
    13  # 3. A release is updated.
    14  
    15  # TODO - Remove logic for step which updates the binaries for a release.
    16  run_upload_script() {
    17    # So that script can run locally too.
    18    if [[ "$TRAVIS" != true ]]; then
    19      TRAVIS_BRANCH="master"
    20      return 0
    21    fi
    22  
    23    if [[ $TRAVIS_TAG == "nightly" ]]; then
    24  		# We create nightly tag using the script so we don't want to run this script
    25  		# when the nightly build is triggered on updating where the commit points too.
    26  		echo "Nightly tag. Skipping script"
    27  		return 1
    28  	fi
    29  
    30  	# We run a cron job daily on Travis which will update the nightly binaries.
    31  	if [ $TRAVIS_EVENT_TYPE == "cron" ]; then
    32  		if [ "$TRAVIS_BRANCH"  != "master" ];then
    33  			echo "Cron job can only be run on master branch"
    34  			return 1
    35  		fi
    36  		echo "Running nightly script for cron job."
    37  		return 0
    38  	fi
    39  
    40  	branch=$TRAVIS_BRANCH
    41  	release_reg="^release/(v.+)"
    42  	if [[ $branch =~ $release_reg ]]; then
    43  		tag=${BASH_REMATCH[1]}
    44  		# If its the release branch and the tag already exists, then we want to update
    45  		# the assets.
    46  		echo $tag
    47  		if git rev-parse $tag >/dev/null 2>&1
    48  		then
    49  			return 0
    50  		else
    51  			echo "On release branch, but tag doesn't exist. Skipping"
    52  			return 1
    53  		fi
    54  	fi
    55  
    56  	if [[ $TRAVIS_TAG =~ v.+ ]]; then
    57  		echo "A new tag was pushed, running nightly script"
    58  		return 0
    59  	fi
    60  
    61  	return 1
    62  }
    63  
    64  get_tag() {
    65  	branch=$TRAVIS_BRANCH
    66  	release_reg="^release/(v.+)"
    67  	if [[ $branch =~ $release_reg ]]; then
    68  		echo ${BASH_REMATCH[1]}
    69  		return
    70  	fi
    71  
    72  	version="^v.+"
    73  	if [[ $TRAVIS_TAG =~ $version ]]; then
    74  		echo $TRAVIS_TAG
    75  		return
    76  	fi
    77  
    78  	echo "nightly"
    79  }
    80  
    81  # Can either be of the type v0.x.y or be nightly.
    82  BUILD_TAG=$(get_tag)
    83  ASSET_SUFFIX=""
    84  
    85  if [[ $BUILD_TAG == "nightly" ]]; then
    86  	ASSET_SUFFIX="-dev"
    87  fi
    88  
    89  TRAVIS_EVENT_TYPE=${TRAVIS_EVENT_TYPE:-cron}
    90  if ! run_upload_script; then
    91  	echo "Skipping running the nightly script"
    92  	exit 0
    93  else
    94   declare -r SSH_FILE="$(mktemp -u $HOME/.ssh/XXXXX)"
    95  
    96  
    97    if [[ "$TRAVIS" == true ]]; then
    98      openssl aes-256-cbc \
    99        -K $encrypted_b471ec07d33f_key \
   100        -iv $encrypted_b471ec07d33f_iv \
   101        -in ".travis/ratel.enc" \
   102        -out "$SSH_FILE" -d
   103  
   104       chmod 600 "$SSH_FILE" \
   105        && printf "%s\n" \
   106          "Host github.com" \
   107          "  IdentityFile $SSH_FILE" \
   108          "  LogLevel ERROR" >> ~/.ssh/config
   109    fi
   110  
   111    go get -u github.com/jteeuwen/go-bindata/...
   112    pushd $GOPATH/src/github.com/dgraph-io
   113    if [[ ! -d ratel ]]; then
   114      git clone git@github.com:dgraph-io/ratel.git
   115    fi
   116  
   117    pushd ratel
   118    source ~/.nvm/nvm.sh
   119    nvm install --lts
   120    ./scripts/build.prod.sh
   121    popd
   122    popd
   123  
   124  	echo "Running nightly script"
   125  fi
   126  
   127  OS="linux"
   128  
   129  DGRAPH=$GOPATH/src/github.com/dgraph-io/dgraph
   130  BUILD_DIR=$DGRAPH/contrib/nightly
   131  source ${BUILD_DIR}/github.sh
   132  
   133  DGRAPH_REPO="dgraph-io/dgraph"
   134  DGRAPH_VERSION=$(git describe --abbrev=0)
   135  LATEST_TAG=$(curl -s https://api.github.com/repos/dgraph-io/dgraph/releases/latest \
   136   | grep "tag_name" | awk '{print $2}' | tr -dc '[:alnum:]-.\n\r' | head -n1)
   137  DGRAPH_COMMIT=$(git rev-parse HEAD)
   138  TAR_FILE="dgraph-${OS}-amd64.tar.gz"
   139  NIGHTLY_FILE="${GOPATH}/src/github.com/dgraph-io/dgraph/${TAR_FILE}"
   140  OSX_NIGHTLY_FILE="${GOPATH}/src/github.com/dgraph-io/dgraph/dgraph-darwin-amd64.tar.gz"
   141  SHA_FILE_NAME="dgraph-checksum-${OS}-amd64.sha256"
   142  SHA_FILE="${GOPATH}/src/github.com/dgraph-io/dgraph/${SHA_FILE_NAME}"
   143  OSX_SHA_FILE="${GOPATH}/src/github.com/dgraph-io/dgraph/dgraph-checksum-darwin-amd64.sha256"
   144  CURRENT_BRANCH=$TRAVIS_BRANCH
   145  CURRENT_DIR=$(pwd)
   146  
   147  WINDOWS_TAR_NAME="dgraph-windows-amd64.tar.gz"
   148  NIGHTLY_WINDOWS_FILE="${GOPATH}/src/github.com/dgraph-io/dgraph/$WINDOWS_TAR_NAME"
   149  
   150  update_or_create_asset() {
   151  	local release_id=$1
   152  	local asset=$2
   153  	local asset_file=$3
   154  	local asset_id=$(send_gh_api_request repos/${DGRAPH_REPO}/releases/${release_id}/assets \
   155  		| jq -r -c ".[] | select(.name == \"${asset}\").id")
   156  
   157  	if [[ -n "${asset_id}" ]]; then
   158  		echo "Found asset file for ${asset}. Deleting"
   159  		send_gh_api_request repos/${DGRAPH_REPO}/releases/assets/${asset_id} \
   160  			DELETE
   161  	fi
   162  	echo "Uplading asset ${asset}, loc: ${asset_file}"
   163  	upload_release_asset ${asset_file} "$asset" \
   164  		${DGRAPH_REPO} ${release_id} \
   165  		> /dev/null
   166  }
   167  
   168  get_nightly_release_body() {
   169  	echo '
   170  	Dgraph development (pre-release) build which is updated every night.
   171  	You can automatically install the nightly binaries along with the assets by running
   172  	`curl https://get.dgraph.io -sSf | bash -s nightly`.
   173  	'
   174  }
   175  
   176  upload_assets() {
   177  	local release_id
   178  	# We check if a release with tag nightly already exists, else we create it.
   179  	read release_id < <( \
   180  		send_gh_api_request repos/${DGRAPH_REPO}/releases \
   181  		| jq -r -c "(.[] | select(.tag_name == \"${BUILD_TAG}\").id), \"\"") \
   182  		|| exit
   183  
   184  	if [[ -z "${release_id}" ]]; then
   185  		echo "Creating release for tag ${BUILD_TAG}."
   186  		# For actual releases add draft true and for nightly release prerelease true.
   187  		if [[ $BUILD_TAG == "nightly" ]]; then
   188  			read release_id < <( \
   189  				send_gh_api_data_request repos/${DGRAPH_REPO}/releases POST \
   190  				"{ \"name\": \"${DGRAPH_VERSION}${ASSET_SUFFIX}\", \"tag_name\": \"${BUILD_TAG}\", \
   191  				\"prerelease\": true }" \
   192  				| jq -r -c '.id') \
   193  				|| exit
   194  		else
   195  			read release_id < <( \
   196  				send_gh_api_data_request repos/${DGRAPH_REPO}/releases POST \
   197  				"{ \"name\": \"${DGRAPH_VERSION}\", \"tag_name\": \"${BUILD_TAG}\", \
   198  				\"draft\": true }" \
   199  				| jq -r -c '.id') \
   200  				|| exit
   201  		fi
   202  	fi
   203  
   204  	# We upload the tar binary.
   205  	local name="dgraph-${OS}-amd64.tar.gz"
   206  	update_or_create_asset $release_id $name ${NIGHTLY_FILE}
   207  
   208  	local name="dgraph-darwin-amd64.tar.gz"
   209  	update_or_create_asset $release_id $name ${OSX_NIGHTLY_FILE}
   210  
   211  	local sha_name="dgraph-checksum-${OS}-amd64.sha256"
   212  	update_or_create_asset $release_id $sha_name ${SHA_FILE}
   213  
   214  	local sha_name="dgraph-checksum-darwin-amd64.sha256"
   215  	update_or_create_asset $release_id $sha_name ${OSX_SHA_FILE}
   216  
   217  	# As asset would be the same on both platforms, we only upload it from linux.
   218  	update_or_create_asset $release_id $WINDOWS_TAR_NAME ${NIGHTLY_WINDOWS_FILE}
   219  
   220  	# We dont want to update description programatically for version releases and commit apart from
   221  	# nightly.
   222  	if [[ $BUILD_TAG == "nightly" ]]; then
   223  		echo 'Updating release description.'
   224  		# TODO(pawan) - This fails, investigate and fix.
   225  		# 			send_gh_api_data_request repos/${DGRAPH_REPO}/releases/${release_id} PATCH \
   226  		# 				"{ \"force\": true, \"body\": $(get_nightly_release_body) | jq -s -c -R '.') }" \
   227  		# 				> /dev/null
   228  		#
   229  		echo "Updating ${BUILD_TAG} tag to point to ${DGRAPH_COMMIT}."
   230  		send_gh_api_data_request repos/${DGRAPH_REPO}/git/refs/tags/${BUILD_TAG} PATCH \
   231  			"{ \"force\": true, \"sha\": \"${DGRAPH_COMMIT}\" }" \
   232  			> /dev/null
   233  	fi
   234  }
   235  
   236  DOCKER_TAG=""
   237  docker_tag() {
   238  	if [[ $BUILD_TAG == "nightly" ]]; then
   239  		DOCKER_TAG="master"
   240  	else
   241  		DOCKER_TAG=$DGRAPH_VERSION
   242  	fi
   243  }
   244  
   245  docker_tag
   246  
   247  build_docker_image() {
   248  	pushd $DGRAPH/contrib/nightly > /dev/null
   249  	# Extract dgraph binary from the tar into dgraph-build folder.
   250  	if [ ! -d dgraph-build ]; then
   251      mkdir dgraph-build
   252    fi
   253  	tar -xzf ${NIGHTLY_FILE} -C dgraph-build
   254  	echo -e "Building the docker image with tag: $DOCKER_TAG."
   255  	docker build -t dgraph/dgraph:$DOCKER_TAG -f $DGRAPH/contrib/nightly/Dockerfile .
   256    # This script only runs on Travis for master or when a new tag is pushed. When a new tag is pushed
   257    # we must tag the docker image with latest tag as well.
   258    if [[ $DOCKER_TAG != "master" ]]; then
   259  		echo "Tagging docker image with latest tag"
   260  		docker tag dgraph/dgraph:$DOCKER_TAG dgraph/dgraph:latest
   261  	fi
   262    rm -rf dgraph
   263  }
   264  
   265  upload_docker_image() {
   266  	echo "Logging into Docker."
   267  	docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
   268  	echo "Pushing the image"
   269  	echo -e "Pushing image with tag $DOCKER_TAG"
   270  	docker push dgraph/dgraph:$DOCKER_TAG
   271  	if [[ $DOCKER_TAG != "master" ]]; then
   272  		echo -e "Pushing latest image"
   273  		docker push dgraph/dgraph:latest
   274  	fi
   275  	popd > /dev/null
   276  }
   277  
   278  pushd $DGRAPH > /dev/null
   279  
   280  $BUILD_DIR/build-cross-platform.sh "windows" $ASSET_SUFFIX
   281  $BUILD_DIR/build-cross-platform.sh "darwin" $ASSET_SUFFIX
   282  $BUILD_DIR/build.sh $ASSET_SUFFIX
   283  
   284  if [[ $DOCKER_TAG == "" ]]; then
   285    echo -e "No docker tag found. Exiting the script"
   286    exit 0
   287  fi
   288  
   289  build_docker_image
   290  
   291  if [ "$TRAVIS" = true ]; then
   292  	upload_assets
   293  	upload_docker_image
   294  fi
   295  
   296  if [ "$DGRAPH" != "$CURRENT_DIR" ]; then
   297  	mv $NIGHTLY_FILE $SHA_FILE $CURRENT_DIR
   298  fi
   299  
   300  # Lets rename the binaries before they are uploaded to S3.
   301  mv $TRAVIS_BUILD_DIR/dgraph/dgraph $TRAVIS_BUILD_DIR/dgraph/dgraph-$TRAVIS_OS_NAME-${TRAVIS_COMMIT:0:7}
   302  
   303  popd > /dev/null