Refactor templates and scripts for container tag
[releng/global-jjb.git] / shell / docker-get-container-tag.sh
1 #!/bin/bash
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 echo "---> docker-get-container-tag.sh"
12 # Gets the container tag per $CONTAINER_TAG_METHOD
13 # For YAML file use directory $CONTAINER_TAG_YAML_DIR
14 # if value is provided, else fall back to $DOCKER_ROOT
15
16 set -feu -o pipefail
17
18 tag=""
19 if [[ $CONTAINER_TAG_METHOD == "latest" ]]; then
20     tag="latest"
21 elif [[ $CONTAINER_TAG_METHOD == "git-describe" ]]; then
22     tag=$(git describe)
23 elif [[ $CONTAINER_TAG_METHOD == "yaml-file" ]]; then
24     # TODO: set default value in JJB?
25     dir=${CONTAINER_TAG_YAML_DIR:-$DOCKER_ROOT}
26     tag_file=$dir/container-tag.yaml
27     if [[ -f $tag_file ]]; then
28         tag=$(yq -r .tag tag_file)
29     else
30         echo "File $tag_file not found."
31     fi
32 else
33     echo "Method $CONTAINER_TAG_METHOD not implemented (yet)"
34 fi
35 if [[ -z $tag ]]; then
36     echo "Failed to get a container tag using method $CONTAINER_TAG_METHOD"
37     exit 1
38 fi
39 echo "---> Docker image tag found: $tag"
40 # Write DOCKER_IMAGE_TAG information to a file so it can be
41 # injected into the environment for following steps
42 echo "DOCKER_IMAGE_TAG=$tag" >> "$WORKSPACE/env_docker_inject.txt"