Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make and publish container images #85

Merged
merged 14 commits into from
Apr 22, 2024
Merged
3 changes: 0 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ data/
.tarballs/

!.build/linux-amd64/
!.build/linux-armv7/
!.build/linux-arm64/
!.build/linux-ppc64le/
!.build/linux-s390x/
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
name: CI
on:
workflow_dispatch:
inputs:
docker_hub_registry:
type: string
description: DockerHub registry
default: docker.io
docker_hub_organization:
type: string
description: DockerHub organization
default: mahendrapaipuri
quay_registry:
type: string
description: Quay registry
default: quay.io
quay_organization:
type: string
description: Quay organization
default: mahendrapaipuri
push:
paths:
- "go.sum"
Expand Down Expand Up @@ -43,3 +60,27 @@ jobs:
packaging:
needs: [build]
uses: ./.github/workflows/step_packaging.yml

docker:
needs: [build]
uses: ./.github/workflows/step_images.yml
# Since workflow_dispatch inputs are only available on manual triggers
# we need to set default values to the context vars here
with:
registry: ${{ inputs.docker_hub_registry || 'docker.io' }}
organization: ${{ inputs.docker_hub_organization || 'mahendrapaipuri' }}
secrets:
login: ${{ secrets.DOCKER_HUB_LOGIN }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}

quay:
needs: [build]
uses: ./.github/workflows/step_images.yml
# Since workflow_dispatch inputs are only available on manual triggers
# we need to set default values to the context vars here
with:
registry: ${{ inputs.quay_registry || 'quay.io' }}
organization: ${{ inputs.quay_organization || 'mahendrapaipuri' }}
secrets:
login: ${{ secrets.QUAY_LOGIN }}
password: ${{ secrets.QUAY_PASSWORD }}
44 changes: 43 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
name: Release

on:
workflow_dispatch:
inputs:
docker_hub_registry:
type: string
description: DockerHub registry
default: docker.io
docker_hub_organization:
type: string
description: DockerHub organization
default: mahendrapaipuri
quay_registry:
type: string
description: Quay registry
default: quay.io
quay_organization:
type: string
description: Quay organization
default: mahendrapaipuri
push:
# Run workflow on new tags
tags:
Expand Down Expand Up @@ -33,6 +51,30 @@ jobs:
needs: [cross-build]
uses: ./.github/workflows/step_packaging.yml

publish-docker-images:
needs: [cross-build]
uses: ./.github/workflows/step_images.yml
# Since workflow_dispatch inputs are only available on manual triggers
# we need to set default values to the context vars here
with:
registry: ${{ inputs.quay_registry || 'docker.io' }}
organization: ${{ inputs.quay_organization || 'mahendrapaipuri' }}
secrets:
login: ${{ secrets.DOCKER_HUB_LOGIN }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}

publish-quay-images:
needs: [cross-build]
uses: ./.github/workflows/step_images.yml
# Since workflow_dispatch inputs are only available on manual triggers
# we need to set default values to the context vars here
with:
registry: ${{ inputs.quay_registry || 'quay.io' }}
organization: ${{ inputs.quay_organization || 'mahendrapaipuri' }}
secrets:
login: ${{ secrets.QUAY_LOGIN }}
password: ${{ secrets.QUAY_PASSWORD }}

publish:
needs: [packaging]
runs-on: ubuntu-latest
Expand All @@ -55,7 +97,7 @@ jobs:
go mod download

- name: Download release artifacts
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-artifacts
path: .tarballs
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/step_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
promu --config .promu-cgo.yml crossbuild -v -p linux/amd64 -p linux/arm64

- name: Upload go build artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: build-go-artifacts
path: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/step_cross-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
promu checksum .tarballs

- name: Upload go build artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: build-go-artifacts
path: |
Expand Down
70 changes: 70 additions & 0 deletions .github/workflows/step_images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: images
run-name: Container images

on:
workflow_call:
inputs:
registry:
type: string
description: Registry
organization:
type: string
description: Organization
secrets:
login:
required: true
password:
required: true

jobs:
images:
name: images
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Get target branch
id: target_branch
run: |
if [[ "${GITHUB_EVENT_NAME}" == "push" ]]; then
echo "name=$(echo ${GITHUB_REF##*/})" >> $GITHUB_OUTPUT
elif [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
echo "name=$(echo $GITHUB_BASE_REF)" >> $GITHUB_OUTPUT
else
echo "name=nightly" >> $GITHUB_OUTPUT
fi

- name: Download go build artifacts
uses: actions/download-artifact@v4
with:
name: build-go-artifacts

- name: Setup build environment
run: |
docker version
docker run --rm --privileged tonistiigi/binfmt:latest --install all

- name: Build container images
run: |
chmod +x -R .build
make docker DOCKER_IMAGE_TAG=${{ steps.target_branch.outputs.name }} DOCKER_REPO=${{ inputs.registry }}/${{ inputs.organization }}
docker images

- name: Test container images
run: make test-docker DOCKER_IMAGE_TAG=${{ steps.target_branch.outputs.name }} DOCKER_REPO=${{ inputs.registry }}/${{ inputs.organization }}

- name: Publish image for current branch/tag
if: github.ref == 'refs/heads/main'
run: |
echo ${{ secrets.password }} | docker login -u ${{ secrets.login }} --password-stdin ${{ inputs.registry }}
make docker-publish DOCKER_IMAGE_TAG=${{ steps.target_branch.outputs.name }} DOCKER_REPO=${{ inputs.registry }}/${{ inputs.organization }}
make docker-manifest DOCKER_IMAGE_TAG=${{ steps.target_branch.outputs.name }} DOCKER_REPO=${{ inputs.registry }}/${{ inputs.organization }}

- name: Publish image with latest tag
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
run: |
echo ${{ secrets.password }} | docker login -u ${{ secrets.login }} --password-stdin ${{ inputs.registry }}
make docker-tag-latest DOCKER_IMAGE_TAG=${{ steps.target_branch.outputs.name }} DOCKER_REPO=${{ inputs.registry }}/${{ inputs.organization }}
make docker-publish DOCKER_IMAGE_TAG=latest DOCKER_REPO=${{ inputs.registry }}/${{ inputs.organization }}
make docker-manifest DOCKER_IMAGE_TAG=latest DOCKER_REPO=${{ inputs.registry }}/${{ inputs.organization }}
2 changes: 1 addition & 1 deletion .github/workflows/step_packaging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: go install github.com/goreleaser/nfpm/v2/cmd/nfpm@latest

- name: Download go build artifacts
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: build-go-artifacts

Expand Down
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ ARG OS="linux"
COPY .build/${OS}-${ARCH}/ceems_exporter /bin/ceems_exporter
COPY .build/${OS}-${ARCH}/ceems_api_server /bin/ceems_api_server
COPY .build/${OS}-${ARCH}/ceems_lb /bin/ceems_lb
COPY build/config/ceems_api_server/tsdb-config.yml /etc/ceems_api_server/tsdb-config.yml
COPY build/config/ceems_lb/config.yml /etc/ceems_lb/config.yml
COPY LICENSE /LICENSE

RUN mkdir /ceems && chown -R nobody:nobody /ceems /etc/ceems_api_server /etc/ceems_lb

USER nobody
WORKDIR /bin
WORKDIR /ceems
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Nicked from node_exporter repo and modified to current exporter
# Nicked from node_exporter repo and modified for current repo needs

# Ensure that 'all' is the default target otherwise it will be the first target from Makefile.common.
all::

# Needs to be defined before including Makefile.common to auto-generate targets
DOCKER_ARCHS ?= amd64 armv7 arm64 ppc64le s390x
DOCKER_ARCHS ?= amd64 arm64

include Makefile.common

Expand All @@ -15,7 +15,7 @@ PROMTOOL ?= $(FIRST_GOPATH)/bin/promtool
PREFIX := $(shell pwd)/bin

TEST_DOCKER ?= false
DOCKER_IMAGE_NAME ?= batchjob-exporter
DOCKER_IMAGE_NAME ?= ceems
MACH ?= $(shell uname -m)
CGROUPS_MODE ?= $([ $(stat -fc %T /sys/fs/cgroup/) = "cgroup2fs" ] && echo "unified" || ( [ -e /sys/fs/cgroup/unified/ ] && echo "hybrid" || echo "legacy"))

Expand Down Expand Up @@ -230,7 +230,9 @@ skip-checkrules: $(PROMTOOL)
.PHONY: test-docker
test-docker:
@echo ">> testing docker image"
./scripts/test_image.sh "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-amd64:$(DOCKER_IMAGE_TAG)" 9010
./scripts/test_image.sh "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-amd64:$(DOCKER_IMAGE_TAG)" 9010 ceems_exporter
./scripts/test_image.sh "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-amd64:$(DOCKER_IMAGE_TAG)" 9020 ceems_api_server
./scripts/test_image.sh "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-amd64:$(DOCKER_IMAGE_TAG)" 9030 ceems_lb --config.file=/etc/ceems_lb/config.yml

.PHONY: skip-test-docker
skip-test-docker:
Expand Down
9 changes: 5 additions & 4 deletions scripts/test_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ set -exo pipefail

docker_image=$1
port=$2
app="${@:3}"

container_id=''

wait_start() {
for in in {1..10}; do
if /usr/bin/curl -s -m 5 -f "http://localhost:${port}/metrics" > /dev/null; then
if curl -s -m 5 "http://localhost:${port}" > /dev/null; then
docker_cleanup
exit 0
else
Expand All @@ -20,15 +21,15 @@ wait_start() {
}

docker_start() {
container_id=$(docker run -d -p "${port}":"${port}" "${docker_image}")
container_id=$(docker run -d -p "${port}":"${port}" "${docker_image}" ${app})
}

docker_cleanup() {
docker kill "${container_id}"
}

if [[ "$#" -ne 2 ]] ; then
echo "Usage: $0 quay.io/prometheus/batchjob-exporter:v0.1.0 9010" >&2
if [[ "$#" -lt 3 ]] ; then
echo "Usage: $0 quay.io/mahendrapaipuri/ceems-exporter:v0.1.0 9010 ceems_exporter <args>" >&2
exit 1
fi

Expand Down
Loading