|
| 1 | ++++ |
| 2 | +title = 'Curl with credentials' |
| 3 | +date = 2025-04-17T16:03:08+01:00 |
| 4 | +draft = true |
| 5 | +ShowReadingTime = true |
| 6 | +tags = ['docker', 'curl', 'credentials'] |
| 7 | +[editPost] |
| 8 | +URL = "https://github.com/ftith/ftith.github.io/edit/main/content" |
| 9 | +Text = "Suggest Changes" |
| 10 | +appendFilePath = true |
| 11 | ++++ |
| 12 | + |
| 13 | +## Curl with basic authentication user |
| 14 | +``` |
| 15 | +curl USER:PASSWORD https://raw.githubusercontent.com/cplee/github-actions-demo/refs/heads/master/package.json |
| 16 | +``` |
| 17 | + |
| 18 | +## Curl with netrc file |
| 19 | +``` |
| 20 | +touch ~/.netrc |
| 21 | +echo "machine github.com login MY_USERNAME password MY_PASSWORD" > ~/.netrc |
| 22 | +curl -o /tmp/package.json --netrc-file ~/.netrc https://raw.githubusercontent.com/cplee/github-actions-demo/refs/heads/master/package.json |
| 23 | +``` |
| 24 | + |
| 25 | + |
| 26 | +## Curl with netrc file within a Dockerfile |
| 27 | +Docker command in your dockerfile: |
| 28 | +``` |
| 29 | +RUN --mount=type=secret,id=curl \ |
| 30 | + curl -o /tmp/package.json --netrc-file /run/secrets/curl https://raw.githubusercontent.com/cplee/github-actions-demo/refs/heads/master/package.json |
| 31 | +``` |
| 32 | + |
| 33 | +Build your dockerfile passing your credentials |
| 34 | +``` |
| 35 | +export CURL_CREDS="machine github.com login MY_USERNAME password MY_PASSWORD" |
| 36 | +docker build --secret id=curl,env=CURL_CREDS . |
| 37 | +``` |
| 38 | + |
| 39 | +## Curl with netrc file within a Dockerfile in github actions |
| 40 | +Workflow |
| 41 | +``` |
| 42 | +- name: Build and Push Docker Image (only for prod and preprod env) |
| 43 | + uses: https://github.com/docker/build-push-action@v4 |
| 44 | + with: |
| 45 | + context: . |
| 46 | + push: ${{ github.event_name != 'pull_request' && (env.TARGET_ENV == 'prod' || env.TARGET_ENV == 'preprod') }} |
| 47 | + secrets: | |
| 48 | + "curl=default ${{ vars.DOMAIN }} login ${{ secrets.USERNAME }} password ${{ secrets.PASSWORD }} protocol https" |
| 49 | +``` |
| 50 | + |
| 51 | +Docker command in your dockerfile: |
| 52 | +``` |
| 53 | +RUN --mount=type=secret,id=curl \ |
| 54 | + curl -o /tmp/package.json --netrc-file /run/secrets/curl https://raw.githubusercontent.com/cplee/github-actions-demo/refs/heads/master/package.json |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +## Sources |
| 59 | +- https://stackoverflow.com/questions/56284902/how-to-add-credentials-to-docker-add-command |
| 60 | +- https://xanderx.com/post/using-netrc-with-curl-for-automatic-authorisation/ |
0 commit comments