Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Test, Build and Publish #35

Test, Build and Publish

Test, Build and Publish #35

# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
name: Test, Build and Publish
on: [workflow_dispatch]
env:
DOWNSTREAM_K3S_VERSION: 'v1.23.10-k3s1'
jobs:
run-e2e-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Install Rancher CLI
run: |
# download an older CLI to avoid https://github.com/rancher/rancher/issues/37574
mkdir -p /home/runner/.local/bin
wget -q https://github.com/rancher/cli/releases/download/v2.6.0/rancher-linux-amd64-v2.6.0.tar.gz
tar -xz --strip-components=2 -f rancher-linux-amd64-v2.6.0.tar.gz -C /home/runner/.local/bin
rancher --version
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up k3d control-plane cluster
uses: AbsaOSS/k3d-action@v2
with:
cluster-name: "upstream"
args: >-
-p "80:80@agent:0:direct"
-p "443:443@agent:0:direct"
--image docker.io/rancher/k3s:${{ env.DOWNSTREAM_K3S_VERSION }}
--api-port 6443
--agents 1
--k3s-arg '--kubelet-arg=eviction-hard=imagefs.available<1%,nodefs.available<1%@agent:*'
--k3s-arg '--kubelet-arg=eviction-minimum-reclaim=imagefs.available=1%,nodefs.available=1%@agent:*'
--network "nw01"
- name: Set up k3d downstream cluster
uses: AbsaOSS/k3d-action@v2
with:
cluster-name: "downstream"
args: >-
-p "81:80@agent:0:direct"
-p "444:443@agent:0:direct"
--image docker.io/rancher/k3s:${{ env.DOWNSTREAM_K3S_VERSION }}
--api-port 6644
--agents 1
--k3s-arg '--kubelet-arg=eviction-hard=imagefs.available<1%,nodefs.available<1%@agent:*'
--k3s-arg '--kubelet-arg=eviction-minimum-reclaim=imagefs.available=1%,nodefs.available=1%@agent:*'
--network "nw01"
- name: Set up latest Rancher
env:
url: "172.18.0.1.omg.howdoi.website"
cluster: "k3d-upstream"
run: |
./.github/scripts/setup-latest-rancher.sh
- name: Register Rancher's downstream clusters
env:
url: "172.18.0.1.omg.howdoi.website"
run: |
kubectl config use-context k3d-upstream
kubectl cluster-info
./.github/scripts/wait-for-loadbalancer.sh
./.github/scripts/register-downstream-clusters.sh
# wait for cluster to settle
sleep 30
- name: Add fake Project
run : ./.github/scripts/create-fake-project.sh
- name: Add fake cluster
run : ./.github/scripts/create-fake-cluster.sh
- name: Add fake Backup
run : ./.github/scripts/create-fake-backup.sh
- name: Add fake Restore
run : ./.github/scripts/create-fake-restore.sh
- name: Wait for env to settle
run : sleep 60
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
- name: Run Go tests
run: |
kubectl config use-context k3d-upstream
go test -v ./...
build-and-package:
runs-on: ubuntu-latest
needs: run-e2e-tests
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Create folder
run: mkdir ./bin/
- name: Build
run: go build -v -o ./bin/prometheus-rancher-exporter
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Retrieve latest Docker image version
id: get_version
run: |
echo "image_version=$(curl -s "https://registry.hub.docker.com/v2/repositories/virtualthoughts/prometheus-rancher-exporter/tags/" | jq -r '.results[].name' | grep -v latest | sort -V | tail -n 1)" >> $GITHUB_OUTPUT
- name: Increment version
id: increment_version
run: |
# Increment the retrieved version
echo "updated_version=$(echo "${{ steps.get_version.outputs.image_version }}" | awk -F. -v OFS=. '{$NF++;print}')" >> $GITHUB_OUTPUT
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: |
virtualthoughts/prometheus-rancher-exporter:${{ steps.increment_version.outputs.updated_version }}
virtualthoughts/prometheus-rancher-exporter:latest
- name: Update Kubernetes YAML manifest
run: |
# Install yq
curl -sL https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -o yq
chmod +x yq
sudo mv yq /usr/local/bin/
# Find and update the image tag in the YAML file
IMAGE_NAME="virtualthoughts/prometheus-rancher-exporter"
NEW_TAG="${{ steps.increment_version.outputs.updated_version }}"
OLD_TAG=$(yq eval '.spec.template.spec.containers[] | select(.name == "rancher-exporter").image' manifests/exporter.yaml | cut -d":" -f2)
NEW_IMAGE="${IMAGE_NAME}:${NEW_TAG}"
sed -i "s|${IMAGE_NAME}:${OLD_TAG}|${NEW_IMAGE}|" manifests/exporter.yaml
- name: Commit and push changes
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
git add manifests/exporter.yaml
git commit -m "Update Kubernetes manifest"
git push