-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add
update-version
script and workflow
- Loading branch information
1 parent
fafe834
commit 49753a1
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/bin/bash | ||
|
||
set -ex | ||
|
||
# Git commiter | ||
GIT_USER="${GITHUB_ACTOR}" | ||
GIT_MAIL="${GITHUB_ACTOR}@users.noreply.github.com" | ||
|
||
# Script output colors | ||
GREEN='\033[0;32m' | ||
YELLOW='\033[0;33m' | ||
RED='\033[0;31m' | ||
NO_COLOR='\e[0m' | ||
|
||
# Get current version | ||
CURRENT_VERSION="$(awk '/^.*_version:/{print $2}' 'defaults/main.yml' | tr -d \')" | ||
|
||
# Get latest version | ||
SOFTWARE="$1" | ||
URL="https://api.github.com/repos/prometheus-community/${SOFTWARE}/releases/latest" | ||
LATEST_VERSION="$(curl --silent $URL | jq '.tag_name' | tr -d '"v')" | ||
|
||
# Compare current and latest versions | ||
if [[ $CURRENT_VERSION == $LATEST_VERSION ]]; then | ||
echo -e "${GREEN}Newest version is used.${NO_COLOR}" | ||
exit 0 | ||
fi | ||
|
||
# Update latest version | ||
sed -i "s/_version:.*$/_version: '${LATEST_VERSION}'/" 'defaults/main.yml' | ||
sed -i "s/${CURRENT_VERSION}/${LATEST_VERSION}/" 'README.md' | ||
|
||
# Repository variables | ||
REPO_NAME=$(git config --get remote.origin.url | sed -e 's|^https://github.com/||') | ||
UPDATE_VERSION_BRANCH="update-to-${LATEST_VERSION}" | ||
UPDATE_VERSION_COMMIT="fix(version): ${SOFTWARE} updated to \`${LATEST_VERSION}\` release" | ||
|
||
# Create an update branch | ||
REMOTE_BRANCH="$(curl --silent https://api.github.com/repos/${REPO_NAME}/branches/${UPDATE_VERSION_BRANCH} | jq -r .name)" | ||
|
||
if [ "${REMOTE_BRANCH}" == null ] ; then | ||
git config user.name "${GIT_USER}" | ||
git config user.email "${GIT_MAIL}" | ||
git checkout -b "${UPDATE_VERSION_BRANCH}" | ||
|
||
# Push new version | ||
git add defaults/main.yml README.md | ||
git commit --signoff -m "${UPDATE_VERSION_COMMIT}" | ||
|
||
echo -e "${GREEN}Pushing to ${UPDATE_VERSION_BRANCH} branch.${NO_COLOR}" | ||
if ! git push "https://${GITHUB_TOKEN}:@github.com/${REPO_NAME}" --set-upstream "${UPDATE_VERSION_BRANCH}"; then | ||
echo -e "${RED}Branch push failed.${NO_COLOR}" | ||
exit 1 | ||
fi | ||
else | ||
echo -e "${YELLOW}Branch is already on remote.${NO_COLOR}" | ||
fi | ||
|
||
# Create Pull Request | ||
PR_MESSAGE="The upstream [${SOFTWARE}](https://github.com/prometheus-community/${SOFTWARE}/releases) released new software version - **${LATEST_VERSION}**!\n\nThis automated PR updates code to bring new version into repository." | ||
JSON="$(printf '{"title":"%s","body":"%s","head":"%s","base":"%s"}' "${UPDATE_VERSION_COMMIT}" "${PR_MESSAGE}" "${UPDATE_VERSION_BRANCH}" "main")" | ||
|
||
curl -L \ | ||
-X POST \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "Authorization: Bearer ${GITHUB_TOKEN}"\ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
"https://api.github.com/repos/${REPO_NAME}/pulls" \ | ||
-d "${JSON}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
name: 'Check for a new upstream version' | ||
on: | ||
schedule: | ||
- cron: '59 23 * * 1' | ||
workflow_dispatch: | ||
|
||
env: | ||
SOFTWARE: 'windows_exporter' | ||
|
||
jobs: | ||
update-version: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: write | ||
pull-requests: write | ||
steps: | ||
- name: 'Checkout the codebase' | ||
uses: actions/checkout@v3 | ||
|
||
- name: 'Update version' | ||
run: .github/scripts/update-version.sh $SOFTWARE | ||
env: | ||
GITHUB_ACTOR: ${{ github.actor }} | ||
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} |