2

Platform: GitHub

CI/CD: GitHub Actions

Additional technologies: Docker with Ubuntu base image

I'm creating a Dockerfile and I need to install a package (such as Terraform). Every time the 'current_version' within "https://checkpoint-api.hashicorp.com/v1/check/terraform" changes, then I want a pull request made to the repo (to change a JSON file with version info).

I've also looked into options such as Dependabot and Renovate. Maybe create a Cron job?

I would appreciate some advice as to how to do this.

Here's how the code would look on a high-level:

FROM ubuntu:18.04

RUN apt-get install # install packages such as curl and wget

wget -O terraform.zip https://releases.hashicorp.com/terraform/${JSON_FILE.TF_VERSION}/terraform_${JSON_FILE.TF_VERSION}_linux_amd64.zip

1 Answer 1

2

Assuming a docker file like this...

FROM ubuntu:18.04

ARG TF_VERSION=0.12.25

RUN apt-get install wget -y

RUN wget -O terraform.zip https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_amd64.zip

In your GitHub action you would need to get the current version from your Dockerfile.

export CURRENT_TF_VERSION=$(grep -Eo '[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*' Dockerfile)

Then the latest version of terraform.

export LATEST_TF_VERSION=$(curl -s 'https://checkpoint-api.hashicorp.com/v1/check/terraform' | jq -r .current_version)

Now in bash or using if and outputs in GitHub actions compare the two values. If they are not the same then find and replace the current version with the latest version in your Dockerfile.

sed -i "s/${CURRENT_TF_VERSION}/${LATEST_TF_VERSION}/" Dockerfile 

Then you can use the create-pull-request GitHub action to create a pull request.

The create-pull-request documentation doesn't mention what happens if you don't have any changes but I asked here. If it doesn't throw an error when you don't have changes then you could simply get the latest value and run sed.

sed "s/[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*/${LATEST_TF_VERSION}/" Dockerfile 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.