-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvariables.sh
executable file
·114 lines (97 loc) · 3.66 KB
/
variables.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
set -o errexit #abort if any command fails
me=$(basename "$0")
help_message="\
Usage: echo $me <version>
Generates variables based on the provided environment variable GITHUB_CONTEXT
and <version> argument.
GITHUB_CONTEXT: An environment variable containing a JSON string of the GitHub
context object. Typically generated with \${{ toJson(github) }}.
<version>: The version number corresponding to the current Git commit."
initialize() {
github_context_json="$GITHUB_CONTEXT"
version="$1"
if [[ -z "$github_context_json" ]]; then
echo "Missing or empty GITHUB_CONTEXT environment variable." >&2
echo "$help_message"
exit 1
fi
if [[ -z "$version" ]]; then
echo "No version specified." >&2
echo "$help_message"
exit 1
fi
sha=$(echo "$github_context_json" | jq --raw-output .sha)
ref=$(echo "$github_context_json" | jq --raw-output .ref)
run_id=$(echo "$github_context_json" | jq --raw-output .run_id)
run_number=$(echo "$github_context_json" | jq --raw-output .run_number)
if [[ -z "$sha" ]]; then
echo "No 'sha' found in the GitHub context." >&2
echo "$help_message"
exit 1
fi
if [[ -z "$ref" ]]; then
echo "No 'ref' found in the GitHub context." >&2
echo "$help_message"
exit 1
fi
if [[ -z "$run_id" ]]; then
echo "No 'run_id' found in the GitHub context." >&2
echo "$help_message"
exit 1
fi
if [[ -z "$run_number" ]]; then
echo "No 'run_number' found in the GitHub context." >&2
echo "$help_message"
exit 1
fi
}
generate_variables() {
# Replace + in the version number with a dot.
version="${version/+/.}"
sha8=$(echo "${sha}" | cut -c1-8)
date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
ghpr_docker_image_name="docker.pkg.github.com/swedbankpay/jekyll-plantuml-docker/jekyll-plantuml"
docker_hub_image_name="swedbankpay/jekyll-plantuml"
if [[ "$ref" == refs/tags/* ]]; then
# Override GitVersion's version on tags, just to be sure.
version="${ref#refs/tags/}"
docker_image_name="$docker_hub_image_name"
docker_image_tag="$version"
else
docker_image_name="$ghpr_docker_image_name"
docker_image_tag="$sha8"
fi
docker_image_fqn="$docker_image_name:$docker_image_tag"
ghpr_docker_image_fqn="$ghpr_docker_image_name:$sha8"
docker_hub_image_name="$docker_hub_image_name:$version"
deploy_branch="r${run_id}-${run_number}"
build_branch="${ref#refs/tags/}"
build_branch="${build_branch#refs/heads/}"
echo "Ref: $ref"
echo "Sha: $sha"
echo "Sha8: $sha8"
echo "Date: $date"
echo "Build Branch: $build_branch"
echo "Deploy Branch: $deploy_branch"
echo "Version: $version"
echo "Docker Image Name: $docker_image_name"
echo "Docker Image Tag: $docker_image_tag"
echo "Docker Image FQN: $docker_image_fqn"
echo "::set-output name=ref::$ref"
echo "::set-output name=sha::$sha"
echo "::set-output name=sha8::$sha8"
echo "::set-output name=date::$date"
echo "::set-output name=version::$version"
echo "::set-output name=build_branch::$build_branch"
echo "::set-output name=deploy_branch::$deploy_branch"
echo "::set-output name=docker_image_name::$docker_image_name"
echo "::set-output name=docker_image_tag::$docker_image_tag"
echo "::set-output name=docker_image_fqn::$docker_image_fqn"
echo "::set-output name=ghpr_docker_image_fqn::$ghpr_docker_image_fqn"
}
main() {
initialize "$@"
generate_variables
}
main "$@"