Skip to content

Commit

Permalink
Use image updater from hitobito
Browse files Browse the repository at this point in the history
  • Loading branch information
Kagemaru committed Oct 15, 2024
1 parent aa5474a commit 4bf737f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 23 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/build-int-on-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ jobs:
build-image:
uses: ./.github/workflows/reusable_lint_test_build_update.yaml
with:
# repository: gitlab.puzzle.ch/pitc_ruby/deployments/decidim.git
# file: ./deployments/${{ needs.settings.outputs.namespace_name }}/kustomization.yaml
# image_tag: ${{ needs.version.outputs.version }}
# image_ref: rails
# identifier: ${{ needs.settings.outputs.namespace_name }}

image_repo: "https://harbor.puzzle.ch"
image_url: "harbor.puzzle.ch/decidim/stzh-int-rails"
configmanagement: "gitlab.puzzle.ch/pitc_ruby/deployments/decidim/"
configmanagement_branch: "main"
kustomization_path: "./deployments/pitc-stzh-decidim-int/kustomization.yaml"
kustomization_path: "./deployments/pitc-decidim-stzh-int/kustomization.yaml"
secrets: inherit
22 changes: 20 additions & 2 deletions .github/workflows/reusable_lint_test_build_update.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,30 @@ jobs:
repository_url: ${{ inputs.image_repo }}
images: ${{ inputs.image_url }}
flavor: latest=true
update_tag:

extract_info:
needs: [build]
runs-on: 'ubuntu-latest'
outputs:
version: ${{ steps.extracted.outputs.version }}
steps:
- name: Save JSON
run: echo '${{ needs.build.outputs.json }}' > build_info.json
- name: Extract version
id: get_version
uses: mikefarah/yq@master
with:
cmd: yq -r '(.tags[] | select(. | test("sha-"))) | sub(".*sha-","")' build_info.json
- name: Prepare outputs
id: extracted
run: echo 'version=${{ steps.get_version.outputs.result }}' >> "$GITHUB_OUTPUT"

update_tag:
needs: [build, extract_info]
uses: ./.github/workflows/reusable_update_image.yaml
secrets: inherit
with:
repository: ${{ inputs.configmanagement }}
repository_branch: ${{ inputs.configmanagement_branch }}
file: ${{ inputs.kustomization_path }}
json: ${{ needs.build.outputs.json }}
image_tag: ${{ needs.extract_info.outputs.version }}
59 changes: 39 additions & 20 deletions .github/workflows/reusable_update_image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,65 +19,84 @@ on:
required: false
default: 'git'
repository_branch:
description: "The branch that should be edited"
description: 'Branch on the repository to edit'
type: string
required: false
default: main
default: 'main'
file:
description: 'File in the repo to update'
type: string
required: true
json:
description: 'Image tag to update the image to'
image_tag:
description: 'Tag to be set'
type: string
required: true
image_ref:
description: 'Name of the image to update'
type: string
required: false
default: rails
identifier:
description: 'Identifier for commit-message, could be a namespace or project-name'
type: string
required: false
default: 'deployment'

secrets:
REPOSITORY_TOKEN:
description: 'Token for repository login'
description: 'Token for repository login, e.g. gitlab-token as Dev with R/W rights on content'
required: true

jobs:
write_tag:
environment: deploy
runs-on: 'ubuntu-latest'
steps:
- name: Pull from repository
env:
REPO_URL: ${{ inputs.repository }}
REPO_USER: ${{ inputs.repository_username }}
REPO_TOKEN: ${{ secrets.REPOSITORY_TOKEN }}
REPO_BRANCH: ${{ inputs.repository_branch }}
REPO_TOKEN: ${{ secrets.REPOSITORY_TOKEN }}
run: |
git clone https://${REPO_USER}:${REPO_TOKEN}@${REPO_URL} ./management_repo
cd ./management_repo
git switch ${REPO_BRANCH}
git config user.name "Decidim Github Action Image updater"
git config user.name "Github Action Image updater"
git config user.email "[email protected]"
git remote set-url origin "https://${REPO_USER}:${REPO_TOKEN}@${REPO_URL}"
- name: Update image tag
git switch ${REPO_BRANCH}
cd ../
- name: Read old tag
id: get_old_image_tag
uses: mikefarah/yq@master
env:
FILE: ./management_repo/${{ inputs.file }}
IMAGE_REF: ${{ inputs.image_ref }}
with:
cmd: |
yq '.images.[] | select(.name == env(IMAGE_REF)).newTag' "${FILE}"
- name: Update image tag if it is newer
if: inputs.image_tag != steps.get_old_image_tag.outputs.result
id: set_image_tag
uses: mikefarah/yq@master
env:
FILE: ${{ inputs.file }}
IMAGE_REF: ${{ inputs.image_ref }}
JSON: ${{ inputs.json }}
NEW_TAG: ${{ inputs.image_tag }}
OLD_TAG: ${{ steps.get_old_image_tag.outputs.result }}
FILE: ./management_repo/${{ inputs.file }}
with:
cmd: |
ls -lha
# TAG=$(echo "${JSON}" | yq -r '.tags[] | select(. | test("sha-"))')
# echo $TAG
# yq -i '.images.[] | select(.name == env(IMAGE_REF)) += {"newTag": env(TAG)}' "${FILE}"
yq -i '(.images.[] | select(.name == env(IMAGE_REF)).newTag) |= env(NEW_TAG)' "${FILE}"
- name: Commit & push back to repository
if: inputs.image_tag != steps.get_old_image_tag.outputs.result
env:
REPO_BRANCH: ${{ inputs.repository_branch }}
IMAGE_REF: ${{ inputs.image_ref }}
TAG: ${{ inputs.tag }}
OLD_TAG: ${{ steps.get_old_image_tag.outputs.result }}
NEW_TAG: ${{ inputs.image_tag }}
PARENT: ${{ inputs.identifier }}
run: |
cd ./management_repo
echo git diff
git add .
git commit -m "Setting image reference of '${IMAGE_REF} to '${TAG}'"
git push origin master
git commit -m "Update ${PARENT}/${IMAGE_REF} from '${OLD_TAG}' to '${NEW_TAG}'"
git push origin "${REPO_BRANCH}"

0 comments on commit 4bf737f

Please sign in to comment.