1
1
#! /bin/sh
2
2
3
+ function allOSRealPath() {
4
+ case $1 in
5
+ /* ) echo " $1 " ; exit ;;
6
+ * ) echo " $PWD /${1# ./ } " ; exit ;;
7
+ esac
8
+ }
9
+
3
10
function usage {
4
11
echo " usage: codebuild_build.sh [-i image_name] [-a artifact_output_directory] [options]"
5
12
echo " Required:"
6
13
echo " -i Used to specify the customer build container image."
7
14
echo " -a Used to specify an artifact output directory."
8
15
echo " Options:"
9
16
echo " -s Used to specify a source directory. Defaults to the current working directory."
17
+ echo " -c Use the AWS configuration and credentials from your local host. This includes ~/.aws and any AWS_* environment variables."
10
18
echo " -b Used to specify a buildspec override file. Defaults to buildspec.yml in the source directory."
11
19
echo " -e Used to specify a file containing environment variables."
12
20
echo " Environment variable file format:"
@@ -20,12 +28,14 @@ function usage {
20
28
21
29
image_flag=false
22
30
artifact_flag=false
31
+ awsconfig_flag=false
23
32
24
- while getopts " i :a:s:b:e:h" opt; do
33
+ while getopts " ci :a:s:b:e:h" opt; do
25
34
case $opt in
26
35
i ) image_flag=true; image_name=$OPTARG ;;
27
36
a ) artifact_flag=true; artifact_dir=$OPTARG ;;
28
37
b ) buildspec=$OPTARG ;;
38
+ c ) awsconfig_flag=true;;
29
39
s ) source_dir=$OPTARG ;;
30
40
e ) environment_variable_file=$OPTARG ;;
31
41
h ) usage; exit ;;
50
60
exit 1
51
61
fi
52
62
53
-
54
63
if [ -z " $source_dir " ]
55
64
then
56
65
source_dir=" $( pwd) "
57
66
else
58
- source_dir=$( realpath $source_dir )
67
+ source_dir=$( allOSRealPath $source_dir )
59
68
fi
60
69
61
70
docker_command=" docker run -it -v /var/run/docker.sock:/var/run/docker.sock -e \
62
71
\" IMAGE_NAME=$image_name \" -e \
63
- \" ARTIFACTS=$( realpath $artifact_dir ) \" -e \
72
+ \" ARTIFACTS=$( allOSRealPath $artifact_dir ) \" -e \
64
73
\" SOURCE=$source_dir \" "
65
74
66
75
if [ -n " $buildspec " ]
67
76
then
68
- docker_command+=" -e \" BUILDSPEC=$buildspec \" "
77
+ docker_command+=" -e \" BUILDSPEC=$( allOSRealPath $ buildspec) \" "
69
78
fi
70
79
71
80
if [ -n " $environment_variable_file " ]
72
81
then
73
- docker_command+=" -v $( dirname $( realpath $environment_variable_file ) ) :/LocalBuild/envFile/ -e \" ENV_VAR_FILE=$( basename $environment_variable_file ) \" "
82
+ docker_command+=" -v $( dirname $( allOSRealPath $environment_variable_file ) ) :/LocalBuild/envFile/ -e \" ENV_VAR_FILE=$( basename $environment_variable_file ) \" "
83
+ fi
84
+
85
+ if $awsconfig_flag
86
+ then
87
+ if [ -d " $HOME /.aws" ]
88
+ then
89
+ docker_command+=" -e \" AWS_CONFIGURATION=$HOME /.aws\" "
90
+ else
91
+ docker_command+=" -e \" AWS_CONFIGURATION=NONE\" "
92
+ fi
93
+ docker_command+=" $( env | grep ^AWS_ | while read -r line; do echo " -e \" $line \" " ; done ) "
74
94
fi
75
95
76
96
docker_command+=" amazon/aws-codebuild-local:latest"
77
97
98
+ # Note we do not expose the AWS_SECRET_ACCESS_KEY or the AWS_SESSION_TOKEN
99
+ exposed_command=$docker_command
100
+ secure_variables=( " AWS_SECRET_ACCESS_KEY=" " AWS_SESSION_TOKEN=" )
101
+ for variable in " ${secure_variables[@]} "
102
+ do
103
+ exposed_command=" $( echo $exposed_command | sed " s/\($variable \)[^ ]*/\1********\" /" ) "
104
+ done
105
+
78
106
echo " Build Command:"
79
107
echo " "
80
- echo $docker_command
108
+ echo $exposed_command
81
109
echo " "
82
110
83
111
eval $docker_command
0 commit comments