#!/bin/bash -eu
#
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# A simple utility that copies the content of the groovy library to the test
# jenkins docker container to test modification to the lib.

: "${CONTAINER_NAME:=$(docker ps | grep "bazel:jenkins-test" | cut -f 1 -d " ")}"

LIB_DIR="$(cd "$(dirname "$0")" && pwd -P)/lib"

if [ -z "$CONTAINER_NAME" ]; then
    echo "Could not find container running bazel:jenkins-test, is the test container running?" >&2
    exit 1
fi

docker exec "$CONTAINER_NAME" bash -c 'rm -fr /opt/lib/*'
for i in "$LIB_DIR/"*; do
    docker cp "$i" "$CONTAINER_NAME:/opt/lib/"
done
docker exec "$CONTAINER_NAME" chown -R jenkins /opt/lib
docker exec "$CONTAINER_NAME" su jenkins bash -c 'cd /opt/lib; git add .; git commit -m sync'

Add simple shell script to transfer the lib folder to a running container
Add simple shell script to transfer the lib folder to a running container

This enable fast testing of the groovy code (let say for rules_closure):
  1. Run `bazel run //jenkins:test`
  2. Add a jenkins pipeline job that depends on the library
  3. Edit the pipeline job and run it
  4. Run jenkins/transfer-lib.sh and it will copy the library at the good place
  5. Go back to step 3

Change-Id: I74c966ebc81de45234178d497b82cd08534a5062
