Skip to content

Update buildimages #105

Update buildimages

Update buildimages #105

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
env:
TARGET_BRANCH: ${{ inputs.branch }}
# this step needs the github repository to be already cloned locally
id: branch_fetch
run: |
if git fetch origin "refs/heads/$TARGET_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 }}
persist-credentials: false
- name: Setup Python and pip
uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0
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 buildimages.get-tag)" >> $GITHUB_OUTPUT
- name: Update buildimages IDs and Go version
id: update_build_images
env:
TEST_VERSION_FLAG: ${{ inputs.test_version && '--test' || '--no-test' }}
INCLUDE_OTEL_MODULES: ${{ inputs.include_otel_modules && '--include-otel-modules' || '' }}
CURRENT_GO_VERSION: ${{ steps.current_go_version.outputs.GO_VERSION }}
INPUT_GO_VERSION: ${{ inputs.go_version }}
IMAGES_ID: ${{ inputs.images_id }}
run: |
if [ "$CURRENT_GO_VERSION" = "$INPUT_GO_VERSION" ]; then
inv -e buildimages.update --tag "$IMAGES_ID" "$TEST_VERSION_FLAG"
echo "MESSAGE=Update buildimages ID to $IMAGES_ID" >> $GITHUB_OUTPUT
else
inv -e update-go --image-tag "$IMAGES_ID" "$TEST_VERSION_FLAG" $INCLUDE_OTEL_MODULES -v "$INPUT_GO_VERSION"
echo "MESSAGE=Update Go version to $INPUT_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 }}
BASE_BRANCH: ${{ github.ref_name }}
INPUT_BRANCH: ${{ inputs.branch }}
run: |
# prs variable contains the number of PRs already created that match head and base branches
prs=$(gh pr list \
--repo "$GITHUB_REPOSITORY" \
--head "$INPUT_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"
CURRENT_BUILDIMAGE_TAG: ${{ steps.current_buildimage_tag.outputs.BUILDIMAGE_TAG }}
IMAGES_ID: ${{ inputs.images_id }}
CURRENT_GO_VERSION: ${{ steps.current_go_version.outputs.GO_VERSION }}
INPUT_GO_VERSION: ${{ inputs.go_version }}
INPUT_TEST_VERSION: ${{ inputs.test_version && '--test' || '' }}
GITHUB_REF: ${{ github.ref }}
run: |
# Generate the PR description
inv -e buildimages.generate-pr-body \
"$CURRENT_BUILDIMAGE_TAG" \
"$IMAGES_ID" \
"$CURRENT_GO_VERSION" \
"$INPUT_GO_VERSION" \
$INPUT_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 \