]> git.lizzy.rs Git - rust.git/blob - src/ci/shared.sh
Rollup merge of #67989 - ollie27:rustdoc_unstable, r=GuillaumeGomez
[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 function isMacOS {
42     [[ "${OSTYPE}" = "darwin"* ]]
43 }
44
45 function isWindows {
46     [[ "${OSTYPE}" = "cygwin" ]] || [[ "${OSTYPE}" = "msys" ]]
47 }
48
49 function isLinux {
50     [[ "${OSTYPE}" = "linux-gnu" ]]
51 }
52
53 function isCiBranch {
54     if [[ $# -ne 1 ]]; then
55         echo "usage: $0 <branch-name>"
56         exit 1
57     fi
58     name="$1"
59
60     if isAzurePipelines; then
61         [[ "${BUILD_SOURCEBRANCHNAME}" = "${name}" ]]
62     elif isGitHubActions; then
63         [[ "${GITHUB_REF}" = "refs/heads/${name}" ]]
64     else
65         echo "isCiBranch only works inside CI!"
66         exit 1
67     fi
68 }
69
70 function ciCommit {
71     if isAzurePipelines; then
72         echo "${BUILD_SOURCEVERSION}"
73     elif isGitHubActions; then
74         echo "${GITHUB_SHA}"
75     else
76         echo "ciCommit only works inside CI!"
77         exit 1
78     fi
79 }
80
81 function ciCheckoutPath {
82     if isAzurePipelines; then
83         echo "${BUILD_SOURCESDIRECTORY}"
84     elif isGitHubActions; then
85         echo "${GITHUB_WORKSPACE}"
86     else
87         echo "ciCheckoutPath only works inside CI!"
88         exit 1
89     fi
90 }
91
92 function ciCommandAddPath {
93     if [[ $# -ne 1 ]]; then
94         echo "usage: $0 <path>"
95         exit 1
96     fi
97     path="$1"
98
99     if isAzurePipelines; then
100         echo "##vso[task.prependpath]${path}"
101     elif isGitHubActions; then
102         echo "::add-path::${path}"
103     else
104         echo "ciCommandAddPath only works inside CI!"
105         exit 1
106     fi
107 }
108
109 function ciCommandSetEnv {
110     if [[ $# -ne 2 ]]; then
111         echo "usage: $0 <name> <value>"
112         exit 1
113     fi
114     name="$1"
115     value="$2"
116
117     if isAzurePipelines; then
118         echo "##vso[task.setvariable variable=${name}]${value}"
119     elif isGitHubActions; then
120         echo "::set-env name=${name}::${value}"
121     else
122         echo "ciCommandSetEnv only works inside CI!"
123         exit 1
124     fi
125 }