yaml-update-action
yaml-update-action copied to clipboard
Update YAML property with dynamic values
YAML Update Action
Update a single value in an existing YAML File. Push this updated YAML to an existing branch or create a new branch. Open a PullRequest to a configurable targetBranch. It is also posible to change the file locally without commiting the change.
Use Cases
Change a local YAML file without commiting the change
By default the actual file in your workspace did not change. This Action creates an in memory copy of your YAML file and sends it to GitHub via the REST API. To achieve an actual update of your local YAML file within your workflow use the following configuration:
name: 'workflow'
on:
push:
branches:
- main
jobs:
test-update-file:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Update values.yaml
uses: fjogeleit/yaml-update-action@main
with:
valueFile: 'file.yaml'
propertyPath: 'file.version'
value: v1.0.1
commitChange: false
updateFile: true
Update Helm Chart after a new Docker Image was build
Update the image version configuration inside of my helm values.yaml
after the related GitHub Workflow build and pushed a new version of my Docker Image to the GitHub Package Registry.
env:
IMAGE_NAME: image
jobs:
push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build app image
run: docker build . --tag image
- name: Log into registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
- name: Push app image
id: image
run: |
IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$FILES_IMAGE_NAME
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "main" ] && VERSION=$(echo ${{ github.sha }} | cut -c1-8)
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag image $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
echo "::set-output name=version::$VERSION"
- name: Update Image Version in the related HelmChart values.yaml
uses: fjogeleit/yaml-update-action@main
with:
valueFile: 'deployment/helm/values.yaml'
propertyPath: 'backend.version'
value: ${{ steps.image.outputs.version }}
branch: deployment/${{ steps.image.outputs.version }}
targetBranch: development
createPR: true
message: 'Update Image Version to ${{ steps.image.outputs.version }}'
Input Arguments
Base Configurations
Argument | Description | Default |
---|---|---|
valueFile | relative path from the Workspace Directory | required Field |
propertyPath | PropertyPath for the new value, JSONPath supported | required Field |
value | New value for the related PropertyPath | required Field |
labels | Comma separated list of labels, e.g. "feature, yaml-updates" | 'yaml-updates' |
updateFile | By default the actual file is not updated, to do so set this property to 'true' | false |
workDir | relative location of the configured repository |
. |
Git related Configurations
Argument | Description | Default |
---|---|---|
commitChange | Commit the change to branch with the given message | true |
message | Commit message for the changed YAML file | '' |
createPR | Create a PR from branch to targetBranch. Use 'true' to enable it | true |
title | Custom title for the created Pull Request | 'Merge: {{message}}' |
description | Custom description for the created Pull Request | '' |
targetBranch | Opens a PR from branch to targetBranch if createPR is set to 'true' | master |
repository | The Repository where the YAML file is located and should be updated. You have to checkout this repository too and set the working-directory for this action to the same as the repository. See the example below | ${{github.repository}} |
branch | The updated YAML file will be commited to this branch, branch will be created if not exists | master |
masterBranchName | Branch name of your master branch | master |
githubAPI | BaseURL for all GitHub REST API requests | https://api.github.com |
token | GitHub API Token which is used to create the PR, have to have right permissions for the selected repository | ${{github.token}} |
commitUserName | Name used for the commit user | GitHub Actions |
commitUserEmail | Email address used for the commit user | [email protected] |
Output
-
commit
Git Commit SHA -
pull_request
Git PR Informations
Debug Informations
Enable Debug mode to get informations about
- YAML parse and update results
- Git Steps
Known Issues
In this first version the updated YAML file will not be patched. It is parsed into JSON, after the update its converted back to YAML. This means that comments and blank lines will be removed in this process and the intend of the updated content can be different to the previous.
By default each value will be interpreted as string. To use other kinds of value types you can use the specified YAML tags as shown here: JS-YAML -Supported YAML types. Use this syntax as string, see the test workflows as example
Advaned Example with an separate target repository
env:
IMAGE_NAME: image
jobs:
push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
path: main
- name: Build app image
run: docker build . --tag image
working-directory: ./main
- name: Log into registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
- name: Push app image
id: image
run: |
IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$FILES_IMAGE_NAME
....
echo "::set-output name=version::$VERSION"
- name: Checkout Target Repository
uses: actions/checkout@v2
with:
repository: owner/target-repository
path: infrastructure
token: ${{ secrets.GITHUB_TOKEN }}
- name: Update Image Version in the related HelmChart values.yaml
uses: fjogeleit/yaml-update-action@main
with:
valueFile: 'deployment/helm/values.yaml'
propertyPath: 'backend.version'
value: ${{ steps.image.outputs.version }}
repository: owner/target-repository
branch: deployment/${{ steps.image.outputs.version }}
targetBranch: development
createPR: true
message: 'Update Image Version to ${{ steps.image.outputs.version }}'
token: ${{ secrets.GITHUB_TOKEN }}
workDir: infrastructure