f3beff0e093498d85ace5d35bd82df49ad2f9654
[releng/global-jjb.git] / shell / docker-get-container-tag.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11
12 echo "---> docker-get-container-tag.sh"
13
14 # Gets the container tag per $CONTAINER_TAG_METHOD
15 # For YAML file use directory $CONTAINER_TAG_YAML_DIR
16 # if a value is provided, else fall back to $DOCKER_ROOT
17 # use login flag to get $HOME/.local/bin in PATH
18
19 set -feu -o pipefail
20
21 tag=""
22 if [[ $CONTAINER_TAG_METHOD == "latest" ]]; then
23     tag="latest"
24 elif [[ $CONTAINER_TAG_METHOD == "stream" ]]; then
25     tag="$STREAM"
26 elif [[ $CONTAINER_TAG_METHOD == "git-describe" ]]; then
27     tag=$(git describe)
28 elif [[ $CONTAINER_TAG_METHOD == "yaml-file" ]]; then
29     dir=${CONTAINER_TAG_YAML_DIR:-$DOCKER_ROOT}
30     tag_file=$dir/container-tag.yaml
31     if [[ -f $tag_file ]]; then
32         # pip installs yq to $HOME/.local/bin
33         tag=$(yq -r .tag "$tag_file")
34     else
35         echo "File $tag_file not found."
36     fi
37 else
38     echo "Method $CONTAINER_TAG_METHOD not implemented (yet)"
39 fi
40 if [[ -z $tag ]]; then
41     echo "Failed to get a container tag using method $CONTAINER_TAG_METHOD"
42     exit 1
43 fi
44 echo "---> Docker image tag found: $tag"
45 # Write DOCKER_IMAGE_TAG information to a file so it can be
46 # injected into the environment for following steps
47 echo "DOCKER_IMAGE_TAG=$tag" >> "$WORKSPACE/env_docker_inject.txt"