Skip to content

Commit

Permalink
Use github.com/dseeley/jenkinslib__docker_for_ansible for the image b…
Browse files Browse the repository at this point in the history
…uild
  • Loading branch information
Dougal Seeley committed Dec 1, 2024
1 parent a87c1b2 commit 46ffdf4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 43 deletions.
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ homepage: https://github.com/dseeley/ansible_vault_pipe

issues: https://github.com/dseeley/ansible_vault_pipe/issues

build_ignore: [ '.idea', '.gitignore', 'Dockerfile*' ]
build_ignore: [ '.idea', 'jenkinsfiles', '.gitignore', 'Dockerfile*' ]

49 changes: 7 additions & 42 deletions jenkinsfiles/Jenkinsfile_release_tag
Original file line number Diff line number Diff line change
@@ -1,45 +1,17 @@
#!groovy
import groovy.json.JsonOutput

@Library('github.com/dseeley/jenkinslib__docker_for_ansible@master') _
import org.dougalseeley.DockerForAnsible

def dockerForAnsible = new DockerForAnsible(this)

def PROJECT_URL_DEFAULT = "https://github.com/dseeley/ansible_vault_pipe"
def PROJECT_BRANCH_DEFAULT = "master" //Set the default git branch to use if we're not running an SCM job (e.g. if we're copying/pasting into a pipeline script)

def pypi_ansible = ["curl", "-s", "-H", "Accept: application/json", "-H", "Content-type: application/json", "GET", "https://pypi.org/pypi/ansible/json"].execute().text
def pypi_ansible_latest = new groovy.json.JsonSlurper().parseText(pypi_ansible).info.version // Use `info.version` instead of `.releases.keySet()[-1]`, to avoid alpha and release candidate versions

//This allows us to create our own Docker image for this specific use-case. Once it is built, it will not be rebuilt, so only adds delay the first time we use it.
def create_custom_image(image_name, build_opts = "") {
// Create a lock to prevent building the same image in parallel
lock('IMAGEBUILDLOCK__' + image_name + '__' + env.NODE_NAME) {
def jenkins_username = sh(script: 'whoami', returnStdout: true).trim()
def jenkins_uid = sh(script: "id -u ${jenkins_username}", returnStdout: true).trim()
def jenkins_gid = sh(script: "id -g ${jenkins_username}", returnStdout: true).trim()

def dockerfile = """
FROM ubuntu:24.04
ARG DEBIAN_FRONTEND=noninteractive
ENV JENKINS_HOME=${env.JENKINS_HOME}
ENV HOME=${env.JENKINS_HOME}
ENV TZ=Europe/London
SHELL ["/bin/bash", "-c"]
RUN groupadd -g ${jenkins_gid} ${jenkins_username} && useradd -m -u ${jenkins_uid} -g ${jenkins_gid} -s /bin/bash ${jenkins_username}
RUN apt-get update \
&& apt-get install -y git iproute2 python3-pip python3-jmespath python3-jinja2 python3-boto3 python3-netaddr python3-paramiko python3-libvirt python3-lxml python3-xmltodict python3-pycdlib python3-google-auth python3-dev python3-setuptools python3-wheel \
&& pip3 --no-cache-dir install --break-system-packages ansible==${params.ANSIBLE_VERSION}
### Install the azcollection/requirements[-azure].txt dependencies (in the default python library location)
RUN pip3 --no-cache-dir install --break-system-packages -r \$(pip3 --no-cache-dir show ansible | grep ^Location | sed -r 's/^Location: (.*)/\\1/')/ansible_collections/azure/azcollection/requirements*.txt
""".stripIndent()

writeFile(file: "Dockerfile_${image_name}", text: dockerfile, encoding: "UTF-8")
custom_build = docker.build(image_name, build_opts + "--network host -f Dockerfile_${image_name} .")

return (custom_build)
}
}


properties([
parameters([
string(name: 'NEW_VERSION', defaultValue: "", description: "Specify either the version to be created (e.g.: v1.0.0), or 'next' to apply the next patch version."),
Expand All @@ -64,17 +36,10 @@ node {
}
}


def docker_parent_net_str = ""
if (sh(script: 'grep -sq "docker\\|lxc" /proc/1/cgroup', returnStatus: true) == 0) {
println("Running in docker. Getting network to pass to docker-in-docker containers...")
def docker_parent_net_id = sh(script: 'docker inspect $(grep -oP \'(?<=docker-)[a-f0-9]+(?=\\.scope)\' /proc/1/cgroup | head -1) -f "{{ range .NetworkSettings.Networks }}{{println .NetworkID}}{{end}}" | head -n 1', returnStdout: true).trim()
docker_parent_net_str = "--network ${docker_parent_net_id}"
println("docker_parent_net_str: ${docker_parent_net_str}")
}
def docker_parent_net_str = dockerForAnsible.get_parent_network()

/*** Create a custom docker image within this Jenkinsfile ***/
create_custom_image("ubuntu_ansible${params.ANSIBLE_VERSION}", "").inside("--init ${docker_parent_net_str}") {
dockerForAnsible.build_image(ansible_version: "${params.ANSIBLE_VERSION}").inside("--init ${docker_parent_net_str}") {
def new_tag_version = params.NEW_VERSION
stage('Create new version') {
withCredentials([usernamePassword(credentialsId: params.GIT_CREDS, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
Expand Down

0 comments on commit 46ffdf4

Please sign in to comment.