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