Skip to content

Produce PR dev images (on Request) #18

Produce PR dev images (on Request)

Produce PR dev images (on Request) #18

name: Produce PR dev images (on Request)
on:
workflow_dispatch:
inputs:
pr_number:
description: 'Pull Request Number to build image for'
required: true
type: number
concurrency:
group: prs-${{ github.event.inputs.pr_number }}
cancel-in-progress: true
env:
GHCR_REGISTRY: ghcr.io
REGISTRY: docker.io
IMAGE_NAME: ${{ github.repository }}
YQ_VERSION: v4.44.3
GH_TOKEN: ${{ github.token }}
jobs:
prepare_pr_info:
name: Identify PR Info
runs-on: ${{ github.repository == 'rancher/prometheus-federator' && format('runs-on,image=ubuntu22-full-x64,runner=4cpu-linux-x64,run-id={0}', github.run_id) || 'ubuntu-latest' }}
outputs:
head_sha: ${{ steps.get_head_sha.outputs.head_sha }}
head_sha_short: ${{ steps.get_head_sha.outputs.head_sha_short }}
steps:
# Checkout the code at the head of the specified PR
- name: Checkout the repository
uses: actions/checkout@v3
- name: Get Pull Request Head SHA
id: get_head_sha
run: |
pr_number=${{ github.event.inputs.pr_number }}
echo "Fetching details for PR #$pr_number"
pr_response=$(gh api repos/${{ github.repository }}/pulls/$pr_number)
head_sha=$(echo "$pr_response" | jq -r '.head.sha')
echo "head_sha=${head_sha}" >> $GITHUB_OUTPUT
echo "head_sha_short=${head_sha:0:7}" >> $GITHUB_OUTPUT
echo "PR ${pr_number}" >> $GITHUB_STEP_SUMMARY
echo "PR SHA: ${head_sha:0:7}" >> $GITHUB_STEP_SUMMARY
echo "PR SHA FULL: ${head_sha}" >> $GITHUB_STEP_SUMMARY
build_dev_helm_locker:
name: Build and push helm-locker
runs-on: ${{ github.repository == 'rancher/prometheus-federator' && format('runs-on,image=ubuntu22-full-x64,runner=4cpu-linux-x64,run-id={0}', github.run_id) || 'ubuntu-latest' }}
needs: prepare_pr_info
permissions:
contents: read
packages: write
attestations: write
id-token: write
outputs:
tags: ${{ steps.meta-helm-locker.outputs.tags }}
steps:
# Checkout the code at the head of the specified PR
- name: Checkout PR Head
uses: actions/checkout@v3
with:
ref: ${{ needs.prepare_pr_info.outputs.head_sha }}
# Proceed to build images
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.GHCR_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for helm-locker image
id: meta-helm-locker
uses: docker/metadata-action@v5
with:
images: ${{ env.GHCR_REGISTRY }}/${{ env.IMAGE_NAME }}/helm-locker
tags: |
type=raw,value=pr-${{ github.event.inputs.pr_number }}-${{ needs.prepare_pr_info.outputs.head_sha_short }}
type=raw,value=pr-${{ github.event.inputs.pr_number }}
- name: Build and push helm-locker image
id: push
uses: docker/build-push-action@v5
with:
context: .
file: ./package/Dockerfile-helm-locker
push: true
tags: ${{ steps.meta-helm-locker.outputs.tags }}
labels: ${{ steps.meta-helm-locker.outputs.labels }}
platforms: linux/amd64,linux/arm64
build_dev_helm_project_operator:
name: Build and push helm-project-operator
runs-on: ${{ github.repository == 'rancher/prometheus-federator' && format('runs-on,image=ubuntu22-full-x64,runner=4cpu-linux-x64,run-id={0}', github.run_id) || 'ubuntu-latest' }}
needs: prepare_pr_info
permissions:
contents: read
packages: write
attestations: write
id-token: write
outputs:
tags: ${{ steps.meta-helm-project-operator.outputs.tags }}
steps:
# Checkout the code at the head of the specified PR
- name: Checkout PR Head
uses: actions/checkout@v3
with:
ref: ${{ needs.prepare_pr_info.outputs.head_sha }}
# Proceed to build images
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.GHCR_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Helm-Project-Operator image
id: meta-helm-project-operator
uses: docker/metadata-action@v5
with:
images: ${{ env.GHCR_REGISTRY }}/${{ env.IMAGE_NAME }}/helm-project-operator
tags: |
type=raw,value=pr-${{ github.event.inputs.pr_number }}-${{ needs.prepare_pr_info.outputs.head_sha_short }}
type=raw,value=pr-${{ github.event.inputs.pr_number }}
- name: Build Helm-Project-Operator image
uses: docker/build-push-action@v5
with:
context: .
file: ./package/Dockerfile-helm-project-operator
push: true
tags: ${{ steps.meta-helm-project-operator.outputs.tags }}
labels: ${{ steps.meta-helm-project-operator.outputs.labels }}
platforms: linux/amd64,linux/arm64
build_dev_prometheus_federator:
name: Build and push prometheus-federator
runs-on: ${{ github.repository == 'rancher/prometheus-federator' && format('runs-on,image=ubuntu22-full-x64,runner=4cpu-linux-x64,run-id={0}', github.run_id) || 'ubuntu-latest' }}
needs: prepare_pr_info
permissions:
contents: read
packages: write
attestations: write
id-token: write
outputs:
tags: ${{ steps.meta-prometheus-federator.outputs.tags }}
steps:
# Checkout the code at the head of the specified PR
- name: Checkout PR Head
uses: actions/checkout@v3
with:
ref: ${{ needs.prepare_pr_info.outputs.head_sha }}
# Proceed to build images
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.GHCR_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Prometheus Federator image
id: meta-prometheus-federator
uses: docker/metadata-action@v5
with:
images: ${{ env.GHCR_REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=pr-${{ github.event.inputs.pr_number }}-${{ needs.prepare_pr_info.outputs.head_sha_short }}
type=raw,value=pr-${{ github.event.inputs.pr_number }}
- name: Build prometheus-federator image
uses: docker/build-push-action@v5
with:
context: .
file: ./package/Dockerfile-prometheus-federator
push: true
tags: ${{ steps.meta-prometheus-federator.outputs.tags }}
labels: ${{ steps.meta-prometheus-federator.outputs.labels }}
platforms: linux/amd64,linux/arm64
comment-on-pr:
name: Comment on PR with image details
runs-on: ${{ github.repository == 'rancher/prometheus-federator' && format('runs-on,image=ubuntu22-full-x64,runner=4cpu-linux-x64,run-id={0}', github.run_id) || 'ubuntu-latest' }}
needs:
- prepare_pr_info
- build_dev_helm_locker
- build_dev_helm_project_operator
- build_dev_prometheus_federator
permissions:
pull-requests: write
steps:
- name: Comment on PR with image details
uses: actions/github-script@v6
env:
meta-helm-locker: ${{ needs.build_dev_helm_locker.outputs.tags }}
meta-helm-project-operator: ${{ needs.build_dev_helm_project_operator.outputs.tags }}
meta-prometheus-federator: ${{ needs.build_dev_prometheus_federator.outputs.tags }}
prNumber: ${{ github.event.inputs.pr_number }}
shortSha: ${{ needs.prepare_pr_info.outputs.head_sha_short }}
with:
script: |
const prNumber = context.payload.inputs.pr_number;
const images = [
{
name: 'Helm Locker',
key: 'meta-helm-locker',
url: `https://github.com/${{ github.repository }}/pkgs/container/prometheus-federator%2Fhelm-locker`
},
{
name: 'Helm Project Operator',
key: 'meta-helm-project-operator',
url: `https://github.com/${{ github.repository }}/pkgs/container/prometheus-federator%2Fhelm-project-operator`
},
{
name: 'Prometheus Federator',
key: 'meta-prometheus-federator',
url: `https://github.com/${{ github.repository }}/pkgs/container/prometheus-federator`
}
];
const prepareContainerImageText = function(imageInfo) {
const rawTags = process.env[imageInfo.key];
const tags = rawTags.split(' ')
.map(tag => `\t- \`${tag}\``)
.join('\n');
return `- [**${imageInfo.name}**](${imageInfo.url}) - Tags:\n${tags}`;
}
const commentBody = images
.map(prepareContainerImageText)
.join('\n');
const helmDebugValues = `#### Helm Debug Values:
\`\`\`yaml
image:
pullPolicy: Always
registry: ghcr.io
repository: rancher/prometheus-federator
tag: pr-${process.env.prNumber}-${process.env.shortSha}
\`\`\`\n`;
const fullCommentBody = `${commentBody}\n\n${helmDebugValues}`;
github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: `### Images built for PR #${prNumber}:\n\n${fullCommentBody}`
});