forked from php-actions/composer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposer-action.bash
executable file
·180 lines (158 loc) · 4.64 KB
/
composer-action.bash
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
set -e
github_action_path=$(dirname "$0")
docker_tag=$(cat ./docker_tag)
echo "Docker tag: $docker_tag" >> output.log 2>&1
phar_url="https://getcomposer.org/download/latest-"
if [ "$ACTION_VERSION" == "latest" ]
then
phar_url="${phar_url}stable/composer.phar"
else
phar_url="${phar_url}${ACTION_VERSION}/composer.phar"
fi
curl --silent -H "User-agent: cURL (https://github.com/php-actions)" -L "$phar_url" > "${github_action_path}/composer.phar"
chmod +x "${github_action_path}/composer.phar"
# command_string is passed directly to the docker executable. It includes the
# container name and version, and this script will build up the rest of the
# arguments according to the action's input values.
command_string="composer"
# In case there is need to install private repositories, SSH details are stored
# in these two places, which are mounted on the Composer docker container later.
mkdir -p ~/.ssh
touch ~/.gitconfig
if [ -n "$ACTION_SSH_KEY" ]
then
echo "Storing private key file for root" >> output.log 2>&1
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
ssh-keyscan -t rsa gitlab.com >> ~/.ssh/known_hosts
ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts
if [ -n "$ACTION_SSH_DOMAIN" ]
then
if [ -n "$ACTION_SSH_PORT" ]
then
ssh-keyscan -t rsa -p $ACTION_SSH_PORT "$ACTION_SSH_DOMAIN" >> ~/.ssh/known_hosts
else
ssh-keyscan -t rsa "$ACTION_SSH_DOMAIN" >> ~/.ssh/known_hosts
fi
fi
echo "$ACTION_SSH_KEY" > ~/.ssh/action_rsa
echo "$ACTION_SSH_KEY_PUB" > ~/.ssh/action_rsa.pub
chmod 600 ~/.ssh/action_rsa
echo "PRIVATE KEY:" >> output.log 2>&1
md5sum ~/.ssh/action_rsa >> output.log 2>&1
echo "PUBLIC KEY:" >> output.log 2>&1
md5sum ~/.ssh/action_rsa.pub >> output.log 2>&1
echo "[core]" >> ~/.gitconfig
echo "sshCommand = \"ssh -i ~/.ssh/action_rsa\"" >> ~/.gitconfig
else
echo "No private keys supplied" >> output.log 2>&1
fi
if [ -n "$ACTION_COMMAND" ]
then
command_string="$command_string $ACTION_COMMAND"
fi
if [ -n "$ACTION_WORKING_DIR" ]
then
command_string="$command_string --working-dir=$ACTION_WORKING_DIR"
fi
# If the ACTION_ONLY_ARGS has _not_ been passed, then we build up the arguments
# that have been specified. The else condition to this if statement allows
# the developer to specify exactly what arguments to pass to Composer.
if [ -z "$ACTION_ONLY_ARGS" ]
then
if [ "$ACTION_COMMAND" = "install" ]
then
case "$ACTION_DEV" in
yes)
# Default behaviour
;;
no)
command_string="$command_string --no-dev"
;;
*)
echo "Invalid input for action argument: dev (must be yes or no)"
exit 1
;;
esac
case "$ACTION_PROGRESS" in
yes)
# Default behaviour
;;
no)
command_string="$command_string --no-progress"
;;
*)
echo "Invalid input for action argument: progress (must be yes or no)"
exit 1
;;
esac
fi
case "$ACTION_INTERACTION" in
yes)
# Default behaviour
;;
no)
command_string="$command_string --no-interaction"
;;
*)
echo "Invalid input for action argument: interaction (must be yes or no)"
exit 1
;;
esac
case "$ACTION_QUIET" in
yes)
command_string="$command_string --quiet"
;;
no)
# Default behaviour
;;
*)
echo "Invalid input for action argument: quiet (must be yes or no)"
exit 1
;;
esac
if [ -n "$ACTION_ARGS" ]
then
command_string="$command_string $ACTION_ARGS"
fi
else
command_string="$command_string $ACTION_ONLY_ARGS"
fi
if [ -n "$ACTION_MEMORY_LIMIT" ]
then
memory_limit="--env COMPOSER_MEMORY_LIMIT=$ACTION_MEMORY_LIMIT"
else
memory_limit=''
fi
echo "Command: $command_string" >> output.log 2>&1
mkdir -p /tmp/composer-cache
export COMPOSER_CACHE_DIR="/tmp/composer-cache"
unset ACTION_SSH_KEY
unset ACTION_SSH_KEY_PUB
dockerKeys=()
while IFS= read -r line
do
dockerKeys+=( $(echo "$line" | cut -f1 -d=) )
done <<<$(docker run --rm "${docker_tag}" env)
while IFS= read -r line
do
key=$(echo "$line" | cut -f1 -d=)
if printf '%s\n' "${dockerKeys[@]}" | grep -q -P "^${key}\$"
then
echo "Skipping env variable $key" >> output.log
else
echo "$line" >> DOCKER_ENV
fi
done <<<$(env)
echo "::set-output name=full_command::${command_string}"
docker run --rm \
--volume "${github_action_path}/composer.phar":/usr/local/bin/composer \
--volume ~/.gitconfig:/root/.gitconfig \
--volume ~/.ssh:/root/.ssh \
--volume "${GITHUB_WORKSPACE}":/app \
--volume "/tmp/composer-cache":/tmp/composer-cache \
--workdir /app \
--env-file ./DOCKER_ENV \
--network host \
${memory_limit} \
${docker_tag} ${command_string}