From 9daff71fb354fa7304369914ec6aeec4f6a41d60 Mon Sep 17 00:00:00 2001 From: Julien Cabillot Date: Wed, 6 Oct 2021 19:45:44 -0400 Subject: [PATCH] feat: init --- Dockerfile | 7 +++++++ Jenkinsfile | 39 +++++++++++++++++++++++++++++++++++++++ entrypoint.sh | 27 +++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 Dockerfile create mode 100644 Jenkinsfile create mode 100755 entrypoint.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..962b6a6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM alpine:3 + +RUN apk add --no-cache bind-tools curl + +COPY entrypoint.sh / + +ENTRYPOINT /entrypoint.sh diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..57a3775 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,39 @@ +pipeline { + environment { + registry = 'https://registry.hub.docker.com' + registryCredential = 'dockerhub_jcabillot' + dockerImage = 'jcabillot/dynhost' + } + + agent any + + triggers { + cron('@midnight') + } + + stages { + stage('Clone repository') { + steps{ + checkout scm + } + } + + stage('Build image') { + steps{ + sh 'docker build --force-rm=true --no-cache=true --pull -t ${dockerImage} .' + } + } + + stage('Deploy Image') { + steps{ + script { + withCredentials([usernamePassword(credentialsId: 'dockerhub_jcabillot', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) { + sh 'docker login --username ${DOCKER_USER} --password ${DOCKER_PASS}' + sh 'docker push ${dockerImage}' + } + } + } + } + } +} + diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..45d6b0e --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env sh + +# Account configuration +#HOST= +#LOGIN= +#PASSWORD= + +# Get current IPv4 and corresponding configured +HOST_IP=$(dig +short $HOST A) +CURRENT_IP=$(curl -m 5 -4 ifconfig.co 2>/dev/null) +if [ -z $CURRENT_IP ] +then + CURRENT_IP=$(dig +short myip.opendns.com @resolver1.opendns.com) +fi +CURRENT_DATETIME=$(date -R) + +# Update dynamic IPv4, if needed +if [ -z $CURRENT_IP ] || [ -z $HOST_IP ] +then + echo "[$CURRENT_DATETIME]: No IP retrieved" +else + if [ "$HOST_IP" != "$CURRENT_IP" ] + then + RES=$(curl --silent --show-error -m 5 -L --location-trusted --user "$LOGIN:$PASSWORD" "https://www.ovh.com/nic/update?system=dyndns&hostname=$HOST&myip=$CURRENT_IP") + echo "[$CURRENT_DATETIME]: IPv4 has changed - request to OVH DynHost: $RES" + fi +fi