Solution 1:
Install aws-cli in Jenkins container using a Dockerfile and use the credentials already stored on your host.
Working example:
Dockerfile:
FROM jenkins/jenkins:lts
USER root
# Set timezone
ENV TZ="UTC"
RUN apt-get -y update --fix-missing
# Install useful tools
RUN apt-get -y install nano wget ntp git curl libcurl4 libcurl4-openssl-dev zip unzip jq
# Install AWS Cli:
# https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
RUN unzip awscliv2.zip
RUN ./aws/install
docker-compose.yml:
Version: '3.3'
services:
jenkins:
container_name: jenkins
build:
context: ./
dockerfile: Dockerfile
hostname: jenkinslocal
user: root
ports:
- 8500:8080
volumes:
# local persistent storage for docker to keep plugins, jobs, workspaces, etc
- ./jenkins-data:/var/jenkins_home
# mount the source file for project for easy access in pipeline
# and in project pipeline -> configure SCM -> GIT url can be:
# file:///projects/project1
- /Users/bogdan/docker/project1:/projects/project1
# make docker available in container
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker
#provide AWS CLI credentials from host
- /Users/bogdan/.aws:/root/.aws
Solution 2: Use docker image with aws-cli provided by aws with credentials from host.
In this case, if you don't have other tools to install, you don't need a Dockerfile, just the docker-compose.yml file
Working example:
Version: '3.3'
services:
jenkins:
container_name: jenkins
# image from docker hub
image: jenkins/jenkins:lts
hostname: jenkinslocal
user: root
ports:
- 8500:8080
volumes:
# local persistent storage for docker to keep plugins, jobs, workspaces, etc
- ./jenkins-data:/var/jenkins_home
# mount the source file for project for easy access in pipeline
# and in project pipeline -> configure SCM -> GIT url can be:
# file:///projects/project1
- /Users/bogdan/docker/project1:/projects/project1
# make docker available in container
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker
Documentation can be found here: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-docker.html
After Jenkins is up and running you can login to Jenkins container:
docker exec -it jenkins bash
and run AWS-CLI like this:
docker run --rm -it -v ~/.aws:/root/.aws amazon/aws-cli --version
Using this command in Jenkins pipeline will be difficult and the script will not run on a real production server, so you need to create an alias to use the command "aws" :
alias aws='docker run --rm -it -v ~/.aws:/root/.aws -v amazon/aws-cli'
now you can test:
aws --version
For avoid login to Jenkins container you can add the command to docker-compose.yml file:
command: sh -c "alias aws='docker run --rm -it -v ~/.aws:/root/.aws -v amazon/aws-cli'"
Important!
Solution 2 can be difficult to use in some situations because aliases are not available, by default, in bash scripts.