Skip to content

Update buildimages

Update buildimages #93

name: Update buildimages
on:
workflow_dispatch:
inputs:
images_id:
description: 'Images ID'
required: true
type: string
go_version:
description: 'Go version'
required: true
type: string
branch:
description: 'Git branch to use'
required: true
type: string
test_version:
description: 'Whether the images are test images'
required: true
type: boolean
include_otel_modules:
description: 'Whether to also bump the Go version in modules used by OpenTelemetry'
required: true
type: boolean
permissions: {}
jobs:
open-go-update-pr:
runs-on: ubuntu-latest
permissions:
contents: write # push commit and branch
pull-requests: write
steps:
- name: Checkout branch
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
- name: Fetch branch
# this step needs the github repository to be already cloned locally
id: branch_fetch
run: |
if git fetch origin "refs/heads/${{ inputs.branch }}"; then
echo "RESULT=true" >> $GITHUB_OUTPUT
else
echo "RESULT=false" >> $GITHUB_OUTPUT
fi
- name: Checkout branch
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
if: ${{ steps.branch_fetch.outputs.RESULT == 'true' }}
with:
ref: ${{ inputs.branch }}
- name: Setup Python and pip
uses: actions/setup-python@39cd14951b08e74b54015e9e001cdefcf80e669f # v5.1.1
with:
# use Python < 3.12 so that distutil is still available by default
python-version: 3.11
cache: "pip"
- uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
with:
# use the go version from the input, not from the .go-version file
# in case it's a Go update PR
go-version: ${{ inputs.go_version }}
- name: Install python dependencies
run: |
python3 -m pip install -r requirements.txt
- name: Get current Go version
id: current_go_version
run: |
echo "GO_VERSION=$(inv go-version)" >> $GITHUB_OUTPUT
- name: Get current buildimage tag
id: current_buildimage_tag
run: |
echo "BUILDIMAGE_TAG=$(inv pipeline.get-gitlab-config-image-tag)" >> $GITHUB_OUTPUT
- name: Update buildimages IDs and Go version
id: update_build_images
env:
TEST_VERSION_FLAG: ${{ inputs.test_version && '--test-version' || '--no-test-version' }}
INCLUDE_OTEL_MODULES: ${{ inputs.include_otel_modules && '--include-otel-modules' || '' }}
run: |
if [ "${{ steps.current_go_version.outputs.GO_VERSION }}" = "${{ inputs.go_version }}" ]; then
inv -e buildimages.update --image-tag ${{ inputs.images_id }} $TEST_VERSION_FLAG
echo 'MESSAGE=Update buildimages ID to ${{ inputs.images_id }}' >> $GITHUB_OUTPUT
else
inv -e update-go --image-tag ${{ inputs.images_id }} $TEST_VERSION_FLAG $INCLUDE_OTEL_MODULES -v "${{ inputs.go_version }}"
echo 'MESSAGE=Update Go version to ${{ inputs.go_version }}' >> $GITHUB_OUTPUT
fi
- uses: stefanzweifel/git-auto-commit-action@8621497c8c39c72f3e2a999a26b4ca1b5058a842 # v5.0.1
id: autocommit
with:
commit_message: ${{ steps.update_build_images.outputs.MESSAGE }}
branch: ${{ inputs.branch }}
create_branch: true
# allow empty commits, so that the branch always exists if the workflow succeeds
commit_options: '--allow-empty'
skip_dirty_check: true # prevents pushing an empty commit if false
# the action fetches all branches and tags, in our case the branches we care about are already fetched
# if they exist, so we can skip the fetch
skip_fetch: true
- name: Check if PR exists
id: check_pr
env:
GH_TOKEN: ${{ github.token }}
run: |
base_branch=${{ github.ref }} # Get the full ref
base_branch=${base_branch#refs/heads/} # Remove 'refs/heads/' from the ref
# prs variable contains the number of PRs already created that match head and base branches
prs=$(gh pr list \
--repo "$GITHUB_REPOSITORY" \
--head ${{ inputs.branch }} \
--base $base_branch \
--json title \
--jq 'length')
if [ $prs -eq 0 ]; then
echo "CREATE_PR=true" >> $GITHUB_OUTPUT
fi
# Create PR only if there is no pre-existing PR on the branch
- name: Create PR
if: ${{ steps.check_pr.outputs.CREATE_PR == 'true' }}
env:
TMP_PR_BODY_PATH: /tmp/pr_body
GH_TOKEN: ${{ github.token }}
PR_TITLE: "[automated] ${{ steps.update_build_images.outputs.MESSAGE }}"
PR_LABELS: "go-update,team/agent-shared-components"
run: |
# Generate the PR description
inv -e buildimages.generate-pr-body \
${{ steps.current_buildimage_tag.outputs.BUILDIMAGE_TAG }} \
${{ inputs.images_id }} \
${{ steps.current_go_version.outputs.GO_VERSION }} \
${{ inputs.go_version }} \
${{ inputs.test_version && '--test-version' || '' }} > $TMP_PR_BODY_PATH
# Create the PR
gh pr create \
--base ${{ github.ref }} \
--title "$PR_TITLE" \
--body-file $TMP_PR_BODY_PATH \
--label "$PR_LABELS" \
--draft \