]> git.lizzy.rs Git - rust.git/blob - src/ci/shared.sh
Use standard formatting for "rust-call" ABI message
[rust.git] / src / ci / shared.sh
1 #!/bin/false
2 # shellcheck shell=bash
3
4 # This file is intended to be sourced with `. shared.sh` or
5 # `source shared.sh`, hence the invalid shebang and not being
6 # marked as an executable file in git.
7
8 export MIRRORS_BASE="https://ci-mirrors.rust-lang.org/rustc"
9
10 # See http://unix.stackexchange.com/questions/82598
11 # Duplicated in docker/dist-various-2/shared.sh
12 function retry {
13   echo "Attempting with retry:" "$@"
14   local n=1
15   local max=5
16   while true; do
17     "$@" && break || {
18       if [[ $n -lt $max ]]; then
19         sleep $n  # don't retry immediately
20         ((n++))
21         echo "Command failed. Attempt $n/$max:"
22       else
23         echo "The command has failed after $n attempts."
24         return 1
25       fi
26     }
27   done
28 }
29
30 function isCI {
31     [[ "${CI-false}" = "true" ]] || isAzurePipelines || isGitHubActions
32 }
33
34 function isAzurePipelines {
35     [[ "${TF_BUILD-False}" = "True" ]]
36 }
37
38 function isGitHubActions {
39     [[ "${GITHUB_ACTIONS-false}" = "true" ]]
40 }
41
42
43 function isSelfHostedGitHubActions {
44     [[ "${RUST_GHA_SELF_HOSTED-false}" = "true" ]]
45 }
46
47 function isMacOS {
48     [[ "${OSTYPE}" = "darwin"* ]]
49 }
50
51 function isWindows {
52     [[ "${OSTYPE}" = "cygwin" ]] || [[ "${OSTYPE}" = "msys" ]]
53 }
54
55 function isLinux {
56     [[ "${OSTYPE}" = "linux-gnu" ]]
57 }
58
59 function isCiBranch {
60     if [[ $# -ne 1 ]]; then
61         echo "usage: $0 <branch-name>"
62         exit 1
63     fi
64     name="$1"
65
66     if isAzurePipelines; then
67         [[ "${BUILD_SOURCEBRANCHNAME}" = "${name}" ]]
68     elif isGitHubActions; then
69         [[ "${GITHUB_REF}" = "refs/heads/${name}" ]]
70     else
71         echo "isCiBranch only works inside CI!"
72         exit 1
73     fi
74 }
75
76 function ciCommit {
77     if isAzurePipelines; then
78         echo "${BUILD_SOURCEVERSION}"
79     elif isGitHubActions; then
80         echo "${GITHUB_SHA}"
81     else
82         echo "ciCommit only works inside CI!"
83         exit 1
84     fi
85 }
86
87 function ciCheckoutPath {
88     if isAzurePipelines; then
89         echo "${BUILD_SOURCESDIRECTORY}"
90     elif isGitHubActions; then
91         echo "${GITHUB_WORKSPACE}"
92     else
93         echo "ciCheckoutPath only works inside CI!"
94         exit 1
95     fi
96 }
97
98 function ciCommandAddPath {
99     if [[ $# -ne 1 ]]; then
100         echo "usage: $0 <path>"
101         exit 1
102     fi
103     path="$1"
104
105     if isAzurePipelines; then
106         echo "##vso[task.prependpath]${path}"
107     elif isGitHubActions; then
108         echo "${path}" >> "${GITHUB_PATH}"
109     else
110         echo "ciCommandAddPath only works inside CI!"
111         exit 1
112     fi
113 }
114
115 function ciCommandSetEnv {
116     if [[ $# -ne 2 ]]; then
117         echo "usage: $0 <name> <value>"
118         exit 1
119     fi
120     name="$1"
121     value="$2"
122
123     if isAzurePipelines; then
124         echo "##vso[task.setvariable variable=${name}]${value}"
125     elif isGitHubActions; then
126         echo "${name}=${value}" >> "${GITHUB_ENV}"
127     else
128         echo "ciCommandSetEnv only works inside CI!"
129         exit 1
130     fi
131 }