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

Enable https #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
# https://hub.docker.com/r/jenkins/jenkins/tags/
FROM jenkins/jenkins:2.102-alpine
FROM jenkins/jenkins:2.104-alpine

# set maintainer
LABEL maintainer "@cloudposse"

# change user
USER root

RUN apk update && apk upgrade && \
apk add --no-cache bash git openssh gettext make docker
# install required packages
RUN readonly PACKAGES=" \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivan-pinatti what are your thoughts on converting this instead to an ARG?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@osterman I did it for testing purposes on my environment, however, AWS CodeBuild is running an older version of Docker that doesn't support it. So, for now we can't change.

I already submitted a PR to AWS CodeBuild adding a newer Docker Image version of Ubuntu and on top of that a newer version of Docker.

The problem is that I submitted both more than a month ago and I did not receive any interaction from AWS, it seems that they are not much attentive to the repo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, that's crazy! Feels like this feature has been in docker for ages now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, do you mean that they don't support multi-line ARG? I see you're using it everywhere else. Could you add a link to your PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@osterman I was referring to the Jenkins version itself, in the first lines of the Dockerfile, ex:

# set default version if no argument was provided
ARG JENKINS_VERSION="2.103"
FROM jenkins/jenkins:${JENKINS_VERSION}-slim

This is not supported yet.

Regarding to pass the package list as argument I don't see any gain on doing it at the moment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding to pass the package list as argument I don't see any gain on doing it at the moment.

personal bias is in favor of reducing the inline bash, when the dockerfile language defines a convention for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean, you prefer this:

# install required packages
RUN apk update && apk upgrade && apk add --no-cache bash=4.4.12-r2 docker=17.10.0-r0 gettext=0.19.8.1-r1 git=2.15.0-r1 make=4.2.1-r0 openssh=7.5_p1-r8 openssl=1.0.2n-r0

Instead of this:

# install required packages
RUN readonly PACKAGES=" \
      bash=4.4.12-r2 \
      docker=17.10.0-r0 \
      gettext=0.19.8.1-r1 \
      git=2.15.0-r1 \
      make=4.2.1-r0 \
      openssh=7.5_p1-r8 \
      openssl=1.0.2n-r0 \
    " \
    && apk update \
    && apk upgrade \
    && apk add --no-cache ${PACKAGES}

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More like:

ARG PACKAGES="bash=4.4.12-r2 docker=17.10.0-r0 gettext=0.19.8.1-r1 git=2.15.0-r1 make=4.2.1-r0 openssh=7.5_p1-r8 openssl=1.0.2n-r0"
RUN apk update && apk upgrade && apk add --no-cache ${PACKAGES} ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering this as integrated part of Terraform Jenkins AWS, we would have to add it as a parameter into AWS CodeBuild code section as well, and I see no advantage in doing it. At least not with the current solution design.

Even though, I would prefer to have it as multi-line for better readability and improved code maintenance, something like:

# install required packages
ARG PACKAGES=" \
      bash=4.4.12-r2 \
      docker=17.10.0-r0 \
      gettext=0.19.8.1-r1 \
      git=2.15.0-r1 \
      make=4.2.1-r0 \
      openssh=7.5_p1-r8 \
      openssl=1.0.2n-r0 \
    " \

RUN apk update \
    && apk upgrade \
    && apk add --no-cache ${PACKAGES}

However, I'm not sure that Docker supports ARG multi-line syntax yet, as seen here:
(https://github.com/moby/moby/issues/35950)[https://github.com/moby/moby/issues/35950]

bash=4.4.12-r2 \
docker=17.10.0-r0 \
gettext=0.19.8.1-r1 \
git=2.15.0-r1 \
make=4.2.1-r0 \
openssh=7.5_p1-r8 \
openssl=1.0.2n-r0 \
" \
&& apk update \
&& apk upgrade \
&& apk add --no-cache ${PACKAGES}

# Allow the jenkins user to run docker
RUN adduser jenkins docker

# generate a self-signed certificate and configure HTTPS
ARG JENKINS_URL="jenkins.local"
ARG COMPANY_NAME="Cloud Posse, LLC"
ARG COUNTRY_CODE="US"

ENV JENKINS_URL=${JENKINS_URL} \
COMPANY_NAME=${COMPANY_NAME} \
COUNTRY_CODE=${COUNTRY_CODE}

RUN mkdir --parents /var/lib/jenkins \
&& openssl genrsa -out /var/lib/jenkins/key.pem \
&& openssl req -new \
-subj "/CN=${JENKINS_URL}/O=${COMPANY_NAME}/C=${COUNTRY_CODE}" \
-key /var/lib/jenkins/key.pem \
-out /var/lib/jenkins/csr.pem \
&& openssl x509 -req \
-days 365 \
-in /var/lib/jenkins/csr.pem \
-signkey /var/lib/jenkins/key.pem \
-out /var/lib/jenkins/cert.pem \
&& chown jenkins:jenkins /var/lib/jenkins/*.pem

ENV JENKINS_OPTS --httpPort=8080 \
--httpsPort=8083 \
--httpsCertificate=/var/lib/jenkins/cert.pem \
--httpsPrivateKey=/var/lib/jenkins/key.pem


# Drop back to the regular jenkins user
USER jenkins

Expand All @@ -18,7 +61,13 @@ USER jenkins
# http://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html
# https://aws.amazon.com/articles/4035
# https://stackoverflow.com/questions/29579589/whats-the-recommended-way-to-set-networkaddress-cache-ttl-in-elastic-beanstalk
ENV JAVA_OPTS="-Djenkins.install.runSetupWizard=false -Dhudson.DNSMultiCast.disabled=true -Djava.awt.headless=true -Dsun.net.inetaddr.ttl=60 -Duser.timezone=PST -Dorg.jenkinsci.plugins.gitclient.Git.timeOut=60"
ARG TIME_ZONE="PST"
ENV JAVA_OPTS="-Djenkins.install.runSetupWizard=false \
-Dhudson.DNSMultiCast.disabled=true \
-Djava.awt.headless=true \
-Dsun.net.inetaddr.ttl=60 \
-Duser.timezone=${TIME_ZONE} \
-Dorg.jenkinsci.plugins.gitclient.Git.timeOut=60"

# Preinstall plugins
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
Expand All @@ -31,4 +80,5 @@ COPY init.groovy /usr/share/jenkins/ref/init.groovy.d/
# Configure `Amazon EC2` plugin to start slaves on demand
COPY init-ec2.groovy /usr/share/jenkins/ref/init.groovy.d/

EXPOSE 8080
# HTTP 8080 - HTTPS 8083
EXPOSE 8080 8083
4 changes: 2 additions & 2 deletions Dockerrun.aws.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
}
],
"Image": {
"Name": "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG",
"Name": "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_REPO_NAME}:${IMAGE_TAG}",
"Update": "true"
},
"Ports": [
{
"ContainerPort": "8080"
"ContainerPort": "8083"
}
]
}
12 changes: 10 additions & 2 deletions buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ phases:
commands:
- echo "Build started on $(date)"
- echo "Building the Docker image..."
- docker build --tag ${IMAGE_REPO_NAME} .
- >
docker build
--tag ${IMAGE_REPO_NAME}
--file Dockerfile
--build-arg JENKINS_URL="${JENKINS_URL}"
--build-arg COMPANY_NAME="${COMPANY_NAME}"
--build-arg COUNTRY_CODE="${COUNTRY_CODE}"
--build-arg TIME_ZONE="${TIME_ZONE}"
.
- docker tag ${IMAGE_REPO_NAME}:${IMAGE_TAG} ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_REPO_NAME}:${IMAGE_TAG}
- docker tag ${IMAGE_REPO_NAME}:${IMAGE_TAG} ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_REPO_NAME}:${STAGE}
post_build:
commands:
- echo "Build completed on $(date)"
- echo "Pushing the Docker image to ECR..."
- docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_REPO_NAME}:${IMAGE_TAG}
- docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_REPO_NAME}:${STAGE}
- docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_REPO_NAME}:${STAGE}
- envsubst < "Dockerrun.aws.json.template" > "Dockerrun.aws.json"
artifacts:
files:
Expand Down
14 changes: 14 additions & 0 deletions init.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import org.jenkinsci.plugins.*
import org.jenkinsci.plugins.saml.*


// get environment variables
def environment_variables = System.getenv()
def jenkins_url = environment_variables['JENKINS_URL']

// get Jenkins location configuration
def jenkinsLocationConfiguration = jenkins.model.JenkinsLocationConfiguration.get()

// set Jenkins URL
jenkinsLocationConfiguration.setUrl('https://' + jenkins_url)

// save current Jenkins state to disk
jenkinsLocationConfiguration.save()


def isValidString = { value ->
if (value != null && value instanceof String && value.trim() != "") {
return true
Expand Down