Declaring variables in drone.yml and passing to Dockerfile

Hey guys,

Unsure if this is even doable. I’ve read a few of the other topics here, but not quite getting it (just started with Drone, so apologies!)

I’m trying to reduce redundancies between drone.yml and my dockerfile.

global-variables:
environment: &default_environment
DNSCRYPT_VERSION: 2.0.45-r1

kind: pipeline
type: docker
name: publish

steps:

  • name: publish
    image: plugins/docker
    settings:
    username:
    from_secret: docker_username
    password:
    from_secret: docker_password
    build-args:
    - $DNSCRYPT_VERSION
    purge: true
    repo: modem7/dnstest
    tags:
    - latest
    - $DNSCRYPT_VERSION

FROM alpine:edge

ARG DNSCRYPT_VERSION
ENV DNSCRYPT_V=$DNSCRYPT_VERSION

LABEL description=“A flexible DNS proxy, with support for modern encrypted DNS protocols
such as DNSCrypt v2 and DNS-over-HTTP/2.”
url=“GitHub - DNSCrypt/dnscrypt-proxy: dnscrypt-proxy 2 - A flexible DNS proxy, with support for encrypted DNS protocols.
version={$DNSCRYPT_V}
ENV LOCAL_PORT 53
RUN apk update &&
apk add --no-cache \
dnscrypt-proxy=$DNSCRYPT_V
drill &&
rm -rf /var/cache/apk/* && rm -rf /tmp/*

EXPOSE $LOCAL_PORT/udp

COPY dnscrypt-proxy.toml /etc/dnscrypt-proxy/dnscrypt-proxy.toml
COPY example-dnscrypt-proxy.toml /etc/dnscrypt-proxy/example-dnscrypt-proxy.toml

USER dnscrypt

HEALTHCHECK --interval=20s --timeout=20s --retries=3 --start-period=10s
CMD drill -p $LOCAL_PORT one.one.one.one @127.0.0.1 || exit 1

CMD /usr/bin/dnscrypt-proxy -config /etc/dnscrypt-proxy/dnscrypt-proxy.toml

I’m most likely trying to overcomplicate matters, but effectively my goal is to try to enter the version number only once, and it gets passed through to both the tag and the dockerfile. Is this even doable?

Thanks!

See the following thread to better understand why your above example does not work:
https://discourse.drone.io/t/using-environment-variables-in-plugin-settings/7598

If you want to keep the yaml DRY you can use yaml anchors:
https://medium.com/@kinghuang/docker-compose-anchors-aliases-extensions-a1e4105d70bd