Skip to content

Commit 9a1e763

Browse files
meringmikelalcon
authored andcommitted
GitHub PR#239 Improve Docker build
-- 98c2b8e by maleo <[email protected]>: Refactoring of Dockerfile This allows passing args to Copybara directly via CMD as an alternative to using environment variables. Closes #239 GitOrigin-RevId: 42493ee Change-Id: I245c22161dcec9756cf5a122514909e5521ccc78
1 parent 53b4ee3 commit 9a1e763

File tree

4 files changed

+59
-47
lines changed

4 files changed

+59
-47
lines changed

.docker/copybara

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
3+
# Handle environment variables for backwards compatibility
4+
if [ -n "${COPYBARA_SUBCOMMAND}" ] || [ -n "${COPYBARA_CONFIG}" ] || [ -n "${COPYBARA_WORKFLOW}" ] || [ -n "${COPYBARA_SOURCEREF}" ] || [ -n "${COPYBARA_OPTIONS}" ]; then
5+
echo "Detected \$COPYBARA_* environment variables, overwriting shell args"
6+
ARGS=()
7+
if [ -n "${COPYBARA_SUBCOMMAND}" ]; then
8+
ARGS+=("${COPYBARA_SUBCOMMAND}")
9+
else
10+
ARGS+=("migrate")
11+
fi
12+
if [ -n "${COPYBARA_CONFIG}" ]; then
13+
ARGS+=("${COPYBARA_CONFIG}")
14+
else
15+
ARGS+=("copy.bara.sky")
16+
fi
17+
if [ -n "${COPYBARA_WORKFLOW}" ]; then
18+
ARGS+=("${COPYBARA_WORKFLOW}")
19+
else
20+
ARGS+=("default")
21+
fi
22+
if [ -n "${COPYBARA_SOURCEREF}" ]; then
23+
ARGS+=("${COPYBARA_SOURCEREF}")
24+
fi
25+
if [ -n "${COPYBARA_OPTIONS}" ]; then
26+
ARGS+=(${COPYBARA_OPTIONS}) # no quotation marks to split them by space
27+
fi
28+
echo "Setting arguments to: \"${ARGS[@]}\""
29+
set -- "${ARGS[@]}"
30+
fi
31+
32+
exec java -jar /opt/copybara/copybara_deploy.jar "$@"

.docker/entrypoint.sh

-17
This file was deleted.

Dockerfile

+12-19
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,23 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
FROM gcr.io/bazel-public/bazel:6.0.0 AS build
16-
17-
USER root
15+
ARG BAZEL_VERSION=6.0.0
16+
FROM gcr.io/bazel-public/bazel:${BAZEL_VERSION} AS build
1817
COPY . .
19-
RUN ./cloudbuild.sh build //java/com/google/copybara:copybara_deploy.jar
20-
RUN mkdir -p /tmp/copybara && \
21-
cp bazel-bin/java/com/google/copybara/copybara_deploy.jar /tmp/copybara/
18+
RUN bazel build //java/com/google/copybara:copybara_deploy.jar
2219

23-
USER ubuntu
2420
FROM golang:latest AS buildtools
2521
RUN go install github.com/bazelbuild/buildtools/buildozer@latest
2622
RUN go install github.com/bazelbuild/buildtools/buildifier@latest
2723

2824
FROM openjdk:11-jre-slim
29-
WORKDIR /usr/src/app
30-
ENV COPYBARA_CONFIG=copy.bara.sky \
31-
COPYBARA_SUBCOMMAND=migrate \
32-
COPYBARA_OPTIONS='' \
33-
COPYBARA_WORKFLOW=default \
34-
COPYBARA_SOURCEREF=''
35-
COPY --from=build /tmp/copybara/ /opt/copybara/
25+
# Install git and cleanup apt cache afterwards to keep image size small
26+
RUN apt-get update && apt-get install -y \
27+
git \
28+
&& rm -rf /var/lib/apt/lists/*
29+
COPY --from=build /home/ubuntu/bazel-bin/java/com/google/copybara/copybara_deploy.jar /opt/copybara/
3630
COPY --from=buildtools /go/bin/buildozer /go/bin/buildifier /usr/bin/
37-
COPY .docker/entrypoint.sh /usr/local/bin/copybara
38-
RUN chmod +x /usr/local/bin/copybara
39-
RUN apt-get update && \
40-
apt-get install -y git && \
41-
apt-get clean
31+
COPY .docker/copybara /usr/local/bin/copybara
32+
ENTRYPOINT ["/usr/local/bin/copybara"]
33+
CMD ["migrate", "copy.bara.sky"]
34+
WORKDIR /usr/src/app

README.md

+15-11
Original file line numberDiff line numberDiff line change
@@ -167,40 +167,44 @@ Once this has finished building, you can run the image like so from the root of
167167
the code you are trying to use Copybara on:
168168

169169
```sh
170-
docker run -it -v "$(pwd)":/usr/src/app copybara copybara
170+
docker run -it -v "$(pwd)":/usr/src/app copybara help
171171
```
172172

173-
A few environment variables exist to allow you to change how you run copybara:
174-
* `COPYBARA_CONFIG=copy.bara.sky`
175-
* allows you to specify a path to a config file, defaults to root `copy.bara.sky`
173+
#### Environment variables
174+
175+
In addition to passing cmd args to the container, you can also set the following
176+
environment variables as an alternative:
176177
* `COPYBARA_SUBCOMMAND=migrate`
177178
* allows you to change the command run, defaults to `migrate`
178-
* `COPYBARA_OPTIONS=''`
179-
* allows you to specify options for copybara, defaults to none
179+
* `COPYBARA_CONFIG=copy.bara.sky`
180+
* allows you to specify a path to a config file, defaults to root `copy.bara.sky`
180181
* `COPYBARA_WORKFLOW=default`
181182
* allows you to specify the workflow to run, defaults to `default`
182183
* `COPYBARA_SOURCEREF=''`
183184
* allows you to specify the sourceref, defaults to none
185+
* `COPYBARA_OPTIONS=''`
186+
* allows you to specify options for copybara, defaults to none
184187

185188
```sh
186189
docker run \
187-
-e COPYBARA_CONFIG='other.config.sky' \
188190
-e COPYBARA_SUBCOMMAND='validate' \
191+
-e COPYBARA_CONFIG='other.config.sky' \
189192
-v "$(pwd)":/usr/src/app \
190-
-it copybara copybara
193+
-it copybara
191194
```
192195

193196
#### Git Config and Credentials
194197

195198
There are a number of ways by which to share your git config and ssh credentials
196-
with the Docker container, an example with macOS is below:
199+
with the Docker container, an example is below:
197200

198201
```sh
199202
docker run \
203+
-v ~/.gitconfig:/root/.gitconfig:ro \
200204
-v ~/.ssh:/root/.ssh \
201-
-v ~/.gitconfig:/root/.gitconfig \
205+
-v ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK} -e SSH_AUTH_SOCK
202206
-v "$(pwd)":/usr/src/app \
203-
-it copybara copybara
207+
-it copybara
204208
```
205209

206210
## Documentation

0 commit comments

Comments
 (0)