Chore: Upgrade Jenkins-job-builder to 6.3.0
[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 # shellcheck disable=SC1090
22 . ~/lf-env.sh
23
24 lf-activate-venv --python python3 yq
25
26 tag=""
27 if [[ $CONTAINER_TAG_METHOD == "latest" ]]; then
28     tag="latest"
29 elif [[ $CONTAINER_TAG_METHOD == "stream" ]]; then
30     tag="$STREAM"
31 elif [[ $CONTAINER_TAG_METHOD == "git-describe" ]]; then
32     tag=$(git describe)
33 elif [[ $CONTAINER_TAG_METHOD == "yaml-file" ]]; then
34     dir=${CONTAINER_TAG_YAML_DIR:-$DOCKER_ROOT}
35     tag_file=$dir/container-tag.yaml
36     if [[ -f $tag_file ]]; then
37         # pip installs yq to $HOME/.local/bin
38         tag=$(yq -r .tag "$tag_file")
39     else
40         echo "File $tag_file not found."
41     fi
42 else
43     echo "Method $CONTAINER_TAG_METHOD not implemented (yet)"
44 fi
45 if [[ -z $tag ]]; then
46     echo "Failed to get a container tag using method $CONTAINER_TAG_METHOD"
47     exit 1
48 fi
49 echo "---> Docker image tag found: $tag"
50 # Write DOCKER_IMAGE_TAG information to a file so it can be
51 # injected into the environment for following steps
52 echo "DOCKER_IMAGE_TAG=$tag" >> "$WORKSPACE/env_docker_inject.txt"