Init container for k8s operator #238
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "Init container for k8s operator" | |
on: | |
workflow_dispatch: | |
inputs: | |
release_tag: | |
description: "Release tag of the agent" | |
required: true | |
init_image_tag: | |
description: "Image tag" | |
required: true | |
default: "0" | |
force: | |
description: "Force build" | |
required: false | |
default: "false" | |
is_internal: | |
description: "Internal release" | |
required: false | |
default: "true" | |
jobs: | |
set_image_tag_variable: | |
strategy: | |
matrix: | |
agents: | |
[ | |
{ name: "linux", file: "agent.zip", platform: "linux/amd64" }, | |
{ | |
name: "alpine", | |
file: "agent-alpine.zip", | |
platform: "linux/amd64", | |
}, | |
{ | |
name: "linux-arm64", | |
file: "agent-arm64.zip", | |
platform: "linux/arm64", | |
}, | |
{ | |
name: "alpine-arm64", | |
file: "agent-alpine-arm64.zip", | |
platform: "linux/arm64", | |
}, | |
] | |
runs-on: ubuntu-latest | |
name: Build and push Docker image | |
steps: | |
- name: Set release tag | |
shell: bash | |
run: | | |
# check that tag is matching regex x.y.x-release.<commit hash> or force flag is enabled | |
if [[ ! ${{ inputs.release_tag }} =~ ^[0-9]+\.[0-9]+\.[0-9]+-release\.[0-9a-f]+$ ]] ; then | |
echo "Tag ${{ inputs.release_tag }} is not matching regex x.y.x-release.<commithash>" | |
if [[ "${{ inputs.force }}" == "true" ]] ; then | |
echo "Force flag is enabled. Continue" | |
else | |
exit 1 | |
fi | |
fi | |
echo "TAG_NAME=$(echo ${{ inputs.release_tag }} | sed -E 's/^([0-9]*\.[0-9]*\.[0-9]*).*/\1/')-init.${{ inputs.init_image_tag }}" >> "$GITHUB_OUTPUT" | |
id: set_tag | |
- uses: actions/checkout@v4 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to DockerHub | |
if: ${{ success() }} | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USER }} | |
password: ${{ secrets.DOCKERHUB_PASS }} | |
- name: Configure AWS credentials for artifacts bucket | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.RELEASE_ARTIFACTS_MANAGER_KEY }} | |
aws-secret-access-key: ${{ secrets.RELEASE_ARTIFACTS_MANAGER_SECRET }} | |
aws-region: us-east-1 | |
- name: Set docker image tags | |
id: set_docker_tags | |
run: | | |
python3 -m pip install semver | |
existing_tags=() | |
# Set repository suffix based on is_internal input | |
repo_suffix="" | |
if [[ "${{ inputs.is_internal }}" == "true" ]] ; then | |
repo_suffix="-internal" | |
fi | |
# Get Docker Hub JWT token | |
TOKEN=$(curl -s -H "Content-Type: application/json" \ | |
-X POST -d '{"username": "${{ secrets.DOCKERHUB_USER }}", "password": "${{ secrets.DOCKERHUB_PASS }}"}' \ | |
"https://hub.docker.com/v2/users/login/" | jq -r .token) | |
if [[ -z "$TOKEN" ]] ; then | |
echo "Failed to get Docker Hub authentication token" | |
exit 1 | |
fi | |
# Define the base repository name | |
DOCKER_REPO="lightruncom/k8s-operator-init-java-agent-${{ matrix.agents.name }}${repo_suffix}" | |
# Check if repository exists | |
repo_check=$(curl -s -f -H "Authorization: Bearer ${TOKEN}" \ | |
"https://hub.docker.com/v2/namespaces/lightruncom/repositories/k8s-operator-init-java-agent-${{ matrix.agents.name }}${repo_suffix}") | |
if [[ $? -ne 0 ]] ; then | |
echo "Error: Repository ${DOCKER_REPO} does not exist in Docker Hub. Please create it first." | |
exit 1 | |
fi | |
dockerhub_tags=$(curl -s -H "Authorization: Bearer ${TOKEN}" \ | |
"https://hub.docker.com/v2/namespaces/lightruncom/repositories/k8s-operator-init-java-agent-${{ matrix.agents.name }}${repo_suffix}/tags?page_size=50" | jq -r ".results[].name") | |
if [[ $? -ne 0 ]] ; then | |
echo "Failed to fetch existing tags" | |
exit 1 | |
fi | |
while IFS= read -r line; do | |
existing_tags+=("$line") | |
done < <(echo $dockerhub_tags) | |
for tag in $existing_tags | |
do | |
if [[ "$tag" == "latest" ]] ; then | |
continue | |
fi | |
echo "Comparing existing tag: $tag with new: ${{steps.set_tag.outputs.TAG_NAME}}" | |
if [[ $(pysemver compare $tag ${{steps.set_tag.outputs.TAG_NAME}}) -ge 0 ]] ; then | |
echo "Existing tag: $tag is greater or equal than new: ${{ inputs.release_tag }}. Skip adding latest tag" | |
echo "DOCKER_TAGS=${DOCKER_REPO}:${{steps.set_tag.outputs.TAG_NAME}}" >> "$GITHUB_OUTPUT" | |
exit 0 | |
fi | |
done | |
echo "Adding latest tag to ${{steps.set_tag.outputs.TAG_NAME}}" | |
echo "DOCKER_TAGS=${DOCKER_REPO}:${{steps.set_tag.outputs.TAG_NAME}},${DOCKER_REPO}:latest" >> "$GITHUB_OUTPUT" | |
- name: Download agent artifacts | |
run: | | |
aws s3 cp s3://${{ secrets.RELEASE_ARTIFACTS_BUCKET }}/artifacts/${{ inputs.release_tag }}/${{ matrix.agents.file }} ./lightrun-init-agent/ | |
- name: Build and push ${{ matrix.agents.name }} container | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./lightrun-init-agent/Dockerfile | |
push: true | |
platforms: ${{ matrix.agents.platform }} | |
tags: ${{steps.set_docker_tags.outputs.DOCKER_TAGS}} | |
build-args: | | |
FILE=${{ matrix.agents.file }} | |
- name: Slack Notification | |
if: always() | |
uses: rtCamp/action-slack-notify@v2.2.0 | |
env: | |
SLACK_CHANNEL: devops-alerts | |
SLACK_COLOR: ${{ job.status }} | |
SLACK_MESSAGE: "Tag ${{ inputs.release_tag }} ${{ inputs.is_internal == 'true' && '(Internal)' || '' }} | Platform ${{ matrix.agents.name }}" | |
SLACK_TITLE: Init container ${{ inputs.is_internal == 'true' && '(Internal)' || '' }} build status - ${{ job.status }} | |
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} |