Create and publish a Docker image #716
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: Create and publish a Docker image | |
on: | |
push: | |
branches: | |
- "main" | |
merge_group: | |
pull_request: | |
branches: | |
- main | |
schedule: | |
- cron: '0 4 * * *' | |
permissions: | |
contents: read | |
# We don't want to cancel any redundant runs on main so we use run_id when head_ref is | |
# not available | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | |
cancel-in-progress: true | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
# TODO: Add some check and fail if this version doesn't match in ./Dockerfile and otelcol-builder.yaml | |
OTEL_VERSION: v0.106.1 | |
jobs: | |
build-and-push-image: | |
runs-on: ubuntu-latest | |
timeout-minutes: 60 | |
permissions: | |
contents: read | |
packages: write # Needed so image can be uploaded to GHCR | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
submodules: 'true' | |
token: ${{ github.token }} | |
- name: Log in to the Container registry - GitHub | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
# We don't give dependabot access to secrets needed to push to the registry, but we still | |
# want dependabot PRs to run rest of the checks (minus docker login + push) | |
- name: Check for Secrets Availability | |
id: secrets-check | |
run: | | |
if [ "${{ secrets.DOCKER_HUB_USERNAME_PROD_ACCOUNT }}" != "" ]; then | |
echo "available=true" >> $GITHUB_OUTPUT; | |
else | |
echo "available=false" >> $GITHUB_OUTPUT; | |
fi | |
- name: Log in to the Container registry - DockerHub | |
if: ${{ steps.secrets-check.outputs.available == 'true' }} | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_HUB_USERNAME_PROD_ACCOUNT }} | |
password: ${{ secrets.DOCKER_HUB_PASSWORD_PROD_ACCOUNT }} | |
- name: Create Docker Build Context | |
run: docker context create builder | |
continue-on-error: true | |
- name: Set up Docker Buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v3 | |
with: | |
endpoint: builder | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
- name: Get current time | |
id: current-time | |
run: | | |
echo "date=$(date +'%Y%m%dT%H%M%S')" >> $GITHUB_OUTPUT | |
# This image contains only DataSet exporter and minimum needed contrib components | |
- name: Build and Push Image To GHCR | |
if: ${{ github.event_name != 'schedule' && github.ref_name == 'main' }} | |
uses: docker/build-push-action@v6 | |
with: | |
file: Dockerfile | |
context: . | |
tags: | | |
${{ env.REGISTRY }}/${{ github.repository }}:latest | |
${{ steps.meta.outputs.tags }} | |
${{ env.REGISTRY }}/${{ github.repository }}:${{ github.sha }} | |
${{ env.REGISTRY }}/${{ github.repository }}:${{env.OTEL_VERSION}} | |
${{ env.REGISTRY }}/${{ github.repository }}:${{steps.current-time.outputs.date}} | |
labels: ${{ steps.meta.outputs.labels }} | |
platforms: linux/amd64,linux/arm64 | |
cache-from: | | |
type=gha,scope=main-otel-dev | |
type=gha,scope=${{ github.ref_name }}-otel-dev | |
cache-to: type=gha,mode=max,scope=${{ github.ref_name }}-otel-dev | |
# This image contains latest version of DataSet exporter + all other otel contrib components | |
# which means it can act as a drop in replacement for upstream otel contrib image | |
# NOTE: We need to run this action twice since we can't load image into Docker + push at the same | |
# time if arm is used. See https://github.com/docker/buildx/issues/59 or details. | |
- name: Build Dev Image | |
uses: docker/build-push-action@v6 | |
with: | |
file: Dockerfile.contrib_datasetexporter_latest | |
context: . | |
push: false | |
load: true | |
tags: | | |
scalyr/opentelemetry-collector-contrib:${{ github.sha }} | |
scalyr/opentelemetry-collector-contrib:${{steps.current-time.outputs.date}} | |
scalyr/opentelemetry-collector-contrib:datasetexporter-latest | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: | | |
type=gha,scope=main-otel-dev-upstream-compat | |
type=gha,scope=${{ github.ref_name }}-otel-dev-upstream-compat | |
cache-to: type=gha,mode=max,scope=${{ github.ref_name }}-otel-dev-upstream-compat | |
# TODO: Run more complete sanity checks before pushing to confirm that image works | |
- name: Test Image | |
run: | | |
# Needs to match version defined in otelcol-builder.datasetexporter-latest.yaml | |
expected_version=$(cat otelcol-builder.datasetexporter-latest.yaml |grep "version:" | head -1 | awk '{print $2}') | |
echo "Expected version: ${expected_version}" | |
echo "Check for components" | |
docker run scalyr/opentelemetry-collector-contrib:${{ github.sha }} "components" | |
echo "Check for expected version: ${expected_version}" | |
docker run scalyr/opentelemetry-collector-contrib:${{ github.sha }} "components" | grep "version: ${expected_version}" | |
echo "Check for dataset" | |
docker run scalyr/opentelemetry-collector-contrib:${{ github.sha }} "components" | grep "name: dataset" | |
- name: Push Dev Image To Docker Hub Account | |
if: ${{ github.event_name != 'schedule' && github.ref_name == 'main' }} | |
uses: docker/build-push-action@v6 | |
with: | |
file: Dockerfile.contrib_datasetexporter_latest | |
context: . | |
push: ${{ github.ref_name == 'main' }} | |
tags: | | |
scalyr/opentelemetry-collector-contrib:${{ github.sha }} | |
scalyr/opentelemetry-collector-contrib:${{steps.current-time.outputs.date}} | |
scalyr/opentelemetry-collector-contrib:datasetexporter-latest | |
labels: ${{ steps.meta.outputs.labels }} | |
platforms: linux/amd64,linux/arm64 | |
cache-from: | | |
type=gha,scope=main-otel-dev-upstream-compat | |
type=gha,scope=${{ github.ref_name }}-otel-dev-upstream-compat | |
cache-to: type=gha,mode=max,scope=${{ github.ref_name }}-otel-dev-upstream-compat | |
- name: Notify Slack on Failure | |
if: ${{ failure() && github.ref_name == 'main' }} | |
uses: act10ns/slack@44541246747a30eb3102d87f7a4cc5471b0ffb7d # v2.1.0 | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} | |
with: | |
status: ${{ job.status }} | |
steps: ${{ toJson(steps) }} | |
channel: '#eng-dataset-o11y' |