diff --git a/.github/workflows/update_version.yaml b/.github/workflows/update_version.yaml index 2652d14..9cb907f 100644 --- a/.github/workflows/update_version.yaml +++ b/.github/workflows/update_version.yaml @@ -11,17 +11,8 @@ on: jobs: generator: name: Generate Step for new CoreDNS versions - runs-on: ubuntu-latest - outputs: - tags: ${{ steps.fetch-upstream.outputs.tags }} + runs-on: ubuntu-24.04 steps: - - name: Prepare environment - run: | - export DEBIAN_FRONTEND=noninteractive - sudo apt-get -qq update - sudo apt-get -qq install -y jq - sudo snap install yq - - name: Checkout rock repository uses: actions/checkout@v4 with: @@ -36,49 +27,21 @@ jobs: fetch-tags: true fetch-depth: 0 - - name: Itemize all releases - id: releases - run: | - rm -rf versions.txt - for rockcraft in $(find coredns-rock -name 'rockcraft.yaml'); do - echo $(yq '.version' $rockcraft) >> versions.txt - done - - - name: Craft from Upstream tags - id: fetch-upstream + - name: Craft using Make target + id: emit-rockcraft run: | - current_releases=( $(sort -V versions.txt) ) - min_tag="v${current_releases[0]}" - coredns_tags=( $(git -C coredns tag --sort=v:refname | sed -n '/'${min_tag}'/,$p') ) - new_tags=() - - for coredns_tag in "${coredns_tags[@]}" - do - if [[ ! -e coredns-rock/${coredns_tag:1}/rockcraft.yaml ]]; then - new_tag=${coredns_tag:1} - new_tags+=($new_tag) - mkdir -p coredns-rock/${new_tag} - tag=${new_tag} envsubst < coredns-rock/template/rockcraft.yaml.in > coredns-rock/${new_tag}/rockcraft.yaml - fi - done - - if [ ${#new_tags[@]} -eq 0 ]; then - tags='[]' - else - tags=$(printf '%s\n' "${new_tags[@]}" | jq -R . | jq --compact-output -s .) - fi - - echo $tags - echo "tags=$tags" >> $GITHUB_OUTPUT + pushd coredns-rock + make COREDNS_GIT_DIR=../coredns update-component + popd - name: Commit and push new rockcraft.yaml id: commit-rockcraft - if: ${{ steps.fetch-upstream.outputs.tags != '[]' }} + if: ${{ steps.emit-rockcraft.outputs.tags != '[]' }} uses: peter-evans/create-pull-request@v7 with: - commit-message: Update CoreDNS versions with ${{ join(fromJSON(steps.fetch-upstream.outputs.tags), ', ') }} + commit-message: Update CoreDNS versions with ${{ join(fromJSON(steps.emit-rockcraft.outputs.tags), ', ') }} title: "Update CoreDNS versions" - body: Update CoreDNS versions with ${{ join(fromJSON(steps.fetch-upstream.outputs.tags), ', ') }} + body: Update CoreDNS versions with ${{ join(fromJSON(steps.emit-rockcraft.outputs.tags), ', ') }} path: coredns-rock branch: autoupdate/sync/coredns delete-branch: true diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..59095d5 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,26 @@ +# Contributing + +To make contributions to this rock, the only significant changes to be made are in the `Makefile` and the `build/` directory + +## Upgrading the components + +To Upgrade the rocks for new releases of coredns run the following make target: + +```shell +make update-component +``` + +* if `jq` or `yq` are not installed on the system, they will be when the target for those items runs +* this will clone the upstream coredns/coredns repo only for listing tags +* it will create a new rockcraft yaml based on the tags missing from this repo +* Raise a PR from a branch with the new rockcraft.yaml files + + +## Testing + +To test the rocks, run the pytest sanity check test + +```shell +cd tests +tox -e sanity +``` diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9bc5c5b --- /dev/null +++ b/Makefile @@ -0,0 +1,58 @@ +# Makefile for CoreDNS Rock +# Description: This Makefile installs necessary tools and updates components. + +which_jq = $(shell which jq) +which_yq = $(shell which yq) +which_rockcraft = $(shell which rockcraft) +COREDNS_GIT_DIR = /tmp/coredns.git + +.PHONY: JQ +JQ: + @if [ -z "${which_jq}" ]; then \ + echo "Installing jq..."; \ + sudo apt-get update && sudo apt-get install -y jq; \ + else \ + echo "jq is installed @ ${which_jq}"; \ + fi + +.PHONY: YQ +YQ: + @if [ -z "$(which_yq)" ]; then \ + echo "Installing yq..."; \ + sudo snap install yq; \ + else \ + echo "yq is installed @ ${which_yq}"; \ + fi + +.PHONY: ROCKCRAFT +ROCKCRAFT: + @if [ -z "$(which_rockcraft)" ]; then \ + echo "Installing rockcraft..."; \ + sudo snap install rockcraft --classic; \ + else \ + echo "rockcraft is installed @ $(which_rockcraft)"; \ + fi + +.PHONY: install-tools +install-tools: JQ YQ ROCKCRAFT + @echo "Tools installed." + +.PHONY: clone-CoreDNS +clone-CoreDNS: + @echo "Cloning CoreDNS..." + @if [ -d $(COREDNS_GIT_DIR) ]; then \ + echo "CoreDNS already cloned."; \ + else \ + mkdir -p $(COREDNS_GIT_DIR); \ + git clone --bare --filter=blob:none --no-checkout https://github.com/coredns/coredns.git $(COREDNS_GIT_DIR); \ + fi + +.PHONY: update-component +update-component: install-tools clone-CoreDNS + @echo "Updating component..." + @COREDNS_GIT_DIR=$(COREDNS_GIT_DIR) build/craft_release.sh + +# Target to remove the temporary directory +clean: + @rm -rf $(COREDNS_GIT_DIR) + @echo "Temporary directory removed: $(COREDNS_GIT_DIR)" \ No newline at end of file diff --git a/build/craft_release.sh b/build/craft_release.sh new file mode 100755 index 0000000..01c56a9 --- /dev/null +++ b/build/craft_release.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# This script is used to create a new release rock image from the included template +set -eu + +SCRIPT_DIR="$( dirname "${BASH_SOURCE[0]}")" +REPO_DIR=$(dirname ${SCRIPT_DIR}) +VERSIONS=${SCRIPT_DIR}/../versions.txt + +function usage() { + if [[ -z ${COREDNS_GIT_DIR+x} ]]; then + echo "COREDNS_GIT_DIR is not set" > /dev/stderr + echo " Clone with 'git clone --bare --filter=blob:none --no-checkout https://github.com/coredns/coredns.git /tmp/coredns.git'" > /dev/stderr + echo " Re-run with 'COREDNS_GIT_DIR=/tmp/coredns.git $0'" > /dev/stderr + exit 1 + fi +} + +function create_rockcrafts(){ + # Get the current releases from existing rockcraft yamls + rm -rf ${VERSIONS} + for rockcraft in $(find ${REPO_DIR} -name 'rockcraft.yaml'); do + echo $(yq '.version' $rockcraft) >> ${VERSIONS} + done + current_releases=( $(sort -V ${VERSIONS}) ) + min_tag="v${current_releases[0]}" # this is the oldest release tag we support + rm -rf ${VERSIONS} + + # Get the tags from the coredns repo, ignoring all before the min_tag + coredns_tags=( $(git -C ${COREDNS_GIT_DIR} tag --sort=v:refname | sed -n '/'${min_tag}'/,$p') ) + new_tags=() + + for coredns_tag in "${coredns_tags[@]}"; do + if [[ ! -e ${REPO_DIR}/${coredns_tag:1}/rockcraft.yaml ]]; then + new_tag=${coredns_tag:1} + new_tags+=($new_tag) + echo "Creating rockcraft.yaml for ${new_tag}" + mkdir -p ${REPO_DIR}/${new_tag} + unset ignored_template_var + tag=${new_tag} envsubst < ${SCRIPT_DIR}/template/rockcraft.yaml.in > ${REPO_DIR}/${new_tag}/rockcraft.yaml + else + echo "Skipping ${coredns_tag} as it already exists" + fi + done + + if [ ${#new_tags[@]} -eq 0 ]; then + tags='[]' + else + tags=$(printf '%s\n' "${new_tags[@]}" | jq -R . | jq --compact-output -s .) + fi + if [[ -v GITHUB_OUTPUT ]]; then + echo "tags=$tags" >> $GITHUB_OUTPUT + fi +} + + +function main() { + usage + create_rockcrafts +} + +main \ No newline at end of file diff --git a/template/rockcraft.yaml.in b/build/template/rockcraft.yaml.in similarity index 88% rename from template/rockcraft.yaml.in rename to build/template/rockcraft.yaml.in index 4986155..8eb1de0 100644 --- a/template/rockcraft.yaml.in +++ b/build/template/rockcraft.yaml.in @@ -34,4 +34,4 @@ parts: - ca-certificates_data override-build: | make - cp $CRAFT_PART_BUILD/coredns $CRAFT_PRIME + cp $${ignored_template_var}CRAFT_PART_BUILD/coredns $${ignored_template_var}CRAFT_PRIME