Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set buildArgs from the task #522

Open
jdomag opened this issue Feb 18, 2022 · 0 comments
Open

How to set buildArgs from the task #522

jdomag opened this issue Feb 18, 2022 · 0 comments

Comments

@jdomag
Copy link

jdomag commented Feb 18, 2022

Use case:
I'm building a docker image where I install packages from AWS CodeArtifact repository. Authentication to CodeArtifact is based on the token that expires every X minutes.

Dockerfile:

FROM python:3.9.10-slim

ARG CODEARTIFACT_PIP_URL

COPY python/ ./

RUN apt-get update \
    && apt-get install -y gcc python3-dev libev-dev

RUN python -m pip install --upgrade pip
RUN pip install -r requirements.txt --extra-index-url=${CODEARTIFACT_PIP_URL}

EXPOSE 8080

CMD [ "python", "./run.py" ]

So I must pass codeArtifact url (that contains token) as a buildArg to Dockerfile.
I have a task that generates a url as below:


task buildArgsOverride {
    group docker
    description "Set buildArgs of docker from task instead of project configuration phase"
    doLast {
            println("${docker.buildArgs}")
            String token = "aws codeartifact get-authorization-token --domain ${image.codeArtifactDomain} --domain-owner 123456 --query authorizationToken --output text".execute().text
            String url = "https://aws:${token}@domain-123456.d.codeartifact.eu-central-1.amazonaws.com/pypi/store/simple"
            def newBuildArgs = [:] 
            docker.getBuildArgs().each { newBuildArgs.put("$it.key","$it.value")}
            newBuildArgs.put("CODEARTIFACT_PIP_URL",url)
            docker.buildArgs(newBuildArgs)
            println("${docker.buildArgs}")
    
    }
}

tasks.getByName("docker").dependsOn("buildArgsOverride")


Running a task returns correct result however issuing 'gradle docker' doesn't pass buildArgs to docker build command. Any idea how to solve this?
I don't want to set it up in configuration block
docker {
}
because it will run on every gradle run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant