Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add GitHub Action for Dockerfile release update and slack notification #38

Merged
merged 83 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
b4cc331
first commit, added workflow for check release
mamu0 Jul 1, 2024
6092329
test check release
mamu0 Jul 1, 2024
2a6406c
test check release
mamu0 Jul 1, 2024
23e3a4c
test check release
mamu0 Jul 1, 2024
b821c64
test check release with changelog
mamu0 Jul 1, 2024
56d62d9
test with some fix
mamu0 Jul 1, 2024
c3d5f28
test with some fix 2
mamu0 Jul 1, 2024
9e9dfca
test with some fix 3
mamu0 Jul 1, 2024
80f5655
test with some fix 4
mamu0 Jul 1, 2024
b70d237
test with some fix 5
mamu0 Jul 1, 2024
e13ddae
test with some fix 6
mamu0 Jul 1, 2024
617e74d
test with some fix 7
mamu0 Jul 1, 2024
43978ec
test minor
mamu0 Jul 1, 2024
870529d
test new dockerfile change
mamu0 Jul 1, 2024
e376816
test new dockerfile change 2
mamu0 Jul 1, 2024
9a7e6c9
test new dockerfile change 3
mamu0 Jul 1, 2024
614b436
test new dockerfile change 4
mamu0 Jul 1, 2024
cd20f5a
test new dockerfile change 5
mamu0 Jul 1, 2024
a0d1e01
test new dockerfile change 6
mamu0 Jul 1, 2024
51a1e4f
test new dockerfile change 7
mamu0 Jul 1, 2024
9dad6e9
test new dockerfile change 8
mamu0 Jul 1, 2024
7c7084e
test new dockerfile change 9
mamu0 Jul 1, 2024
b9233d3
test new dockerfile change 10
mamu0 Jul 1, 2024
a4bfd55
test new dockerfile change 11
mamu0 Jul 1, 2024
0009446
test new dockerfile change 12
mamu0 Jul 1, 2024
1ea5b39
test new dockerfile change 13
mamu0 Jul 1, 2024
1cbcf96
test new dockerfile change 14
mamu0 Jul 1, 2024
2a80265
test new dockerfile change 15
mamu0 Jul 1, 2024
96eb9a4
test new dockerfile change 15
mamu0 Jul 1, 2024
cd32b26
test new dockerfile change 15
mamu0 Jul 1, 2024
9faf225
test new dockerfile change 15
mamu0 Jul 1, 2024
272e1cd
test new dockerfile change 15
mamu0 Jul 1, 2024
08a5b10
test new dockerfile change 15
mamu0 Jul 1, 2024
ceca726
test new dockerfile change 15
mamu0 Jul 1, 2024
f2e76fa
test new dockerfile change 15
mamu0 Jul 1, 2024
26e33b6
test new dockerfile change 15
mamu0 Jul 1, 2024
66430f3
test new dockerfile change 15
mamu0 Jul 1, 2024
9fcd105
test new dockerfile change 15
mamu0 Jul 1, 2024
2bc21f4
test new dockerfile change 16
mamu0 Jul 1, 2024
42686ea
test new dockerfile change 17
mamu0 Jul 1, 2024
25cfba8
test new dockerfile change 17
mamu0 Jul 1, 2024
d9db7ea
test new dockerfile change 18
mamu0 Jul 1, 2024
34857ad
test new dockerfile change 19
mamu0 Jul 1, 2024
026143e
test new dockerfile change 19
mamu0 Jul 1, 2024
117c582
test new dockerfile change 19
mamu0 Jul 1, 2024
93048b3
test branch creation
mamu0 Jul 1, 2024
f53ef8a
test branch creation 2
mamu0 Jul 1, 2024
2ba4564
test branch creation 3
mamu0 Jul 1, 2024
80d3d9e
test branch creation 4
mamu0 Jul 1, 2024
a385efe
test slack notification
mamu0 Jul 1, 2024
c95bad9
test slack notification and new branch version
mamu0 Jul 1, 2024
4e5c284
test slack notification
mamu0 Jul 1, 2024
c58495d
test branch creation
mamu0 Jul 1, 2024
086ac69
test sha
mamu0 Jul 2, 2024
3771a76
test sha arch
mamu0 Jul 2, 2024
d99ec32
fixed form and sha
mamu0 Jul 2, 2024
deb9ea7
added new PR action end gitignore update
mamu0 Jul 2, 2024
4d4f575
removed on push trigger
mamu0 Jul 2, 2024
215632a
minor
mamu0 Jul 2, 2024
73b9252
minor
mamu0 Jul 2, 2024
f7ee064
edit actions/github-script to use sha
mamu0 Jul 2, 2024
f717fff
replaced PR creation with git commands
mamu0 Jul 3, 2024
a2ff9f1
fixed branch exist check
mamu0 Jul 3, 2024
a4f8a08
test GH commands for variables
mamu0 Jul 3, 2024
583031a
added new slack action
mamu0 Jul 4, 2024
f3a5c4e
edit action name
mamu0 Jul 4, 2024
2c93eb1
test token
mamu0 Jul 4, 2024
9a80237
test token
mamu0 Jul 4, 2024
33dfd75
re enabled branch creation, PR and slack notification
mamu0 Jul 4, 2024
154570b
removed release.json
mamu0 Jul 4, 2024
34c42bc
removed duplicate env, updated ubuntu ref and added environment
mamu0 Jul 5, 2024
f788080
test env variable set
mamu0 Jul 10, 2024
606c2de
restore
mamu0 Jul 10, 2024
6dfb21e
branch exist var fixed
mamu0 Jul 15, 2024
91461c2
fix: re added release.json check - test no file
mamu0 Jul 31, 2024
0fd3f5e
fix: re added release.json check - test 2 no file
mamu0 Jul 31, 2024
9b3c8b8
fix: re added release.json check - test branch exist
mamu0 Jul 31, 2024
e9e7819
fix: re added release.json check - test file exist not updated
mamu0 Jul 31, 2024
f853a85
fix: re added release.json check - test file exist updated
mamu0 Jul 31, 2024
d48fc5a
fix: removed env configuration and GH PAT secret call
mamu0 Jul 31, 2024
cfcd341
fix: minor
mamu0 Jul 31, 2024
f2826c8
fix: Update Dockerfile to latest
mamu0 Jul 31, 2024
ad39d01
fix: removed old vars
mamu0 Jul 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions .github/workflows/check-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
name: Check for New Release and Update Dockerfile

on:
workflow_dispatch:
schedule:
- cron: '0 9 * * *' # Execute everyday at 9 AM

jobs:
check-release:
runs-on: ubuntu-latest
mamu0 marked this conversation as resolved.
Show resolved Hide resolved
permissions:
id-token: write
contents: write
pull-requests: write
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
name: Checkout

- name: Get latest release from another repo
id: get_release
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const latestRelease = await github.rest.repos.getLatestRelease({
owner: 'actions',
repo: 'runner'
});
const releaseInfo = {
tag_name: latestRelease.data.tag_name,
sha: latestRelease.data.target_commitish,
};
console.log(JSON.stringify(releaseInfo));
return JSON.stringify(releaseInfo);

- name: Define release outputs
id: release_outputs
run: |
release_info="${{ steps.get_release.outputs.result }}"
tag_name=$(echo "$release_info" | jq -r '.tag_name')
sha=$(echo "$release_info" | jq -r '.sha')
short_tag_name=$(echo "$tag_name" | sed 's/^v//')

echo "tag_name=$tag_name" >> "$GITHUB_OUTPUT"
echo "sha=$sha" >> "$GITHUB_OUTPUT"
echo "short_tag_name=$short_tag_name" >> "$GITHUB_OUTPUT"

# Check if the release has changed
# if release.json file exist and the current tag and sha are the same as the latest release, then no changes have been made
# if release.json file does not exist or exist but the current tag and sha are different, then the release will be considered as changed

- name: Check if release has changed
id: check_release_changes
run: |
new_tag=${{ steps.release_outputs.outputs.tag_name }}
new_sha=${{ steps.release_outputs.outputs.sha }}

if [ -f release.json ]; then
current_tag=$(jq -r '.current_tag' release.json)
current_sha=$(jq -r '.current_sha' release.json)

if [[ "$current_tag" == "$new_tag" && "$current_sha" == "$new_sha" ]]; then
echo "No changes in release."
echo "release_changed=false" >> "$GITHUB_OUTPUT"
else
echo "Release has changed."
echo "release_changed=true" >> "$GITHUB_OUTPUT"
# Update release.json
jq -n --arg new_tag "$new_tag" --arg new_sha "$new_sha" \
'{current_tag: $new_tag, current_sha: $new_sha}' > release.json

cat release.json
fi

else
echo "Release is changed, file release.json does not exist."
echo "release_changed=true" >> "$GITHUB_OUTPUT"

# Create release.json
jq -n --arg new_tag "$new_tag" --arg new_sha "$new_sha" \
'{current_tag: $new_tag, current_sha: $new_sha}' > release.json

cat release.json
fi

# Retrieve the SHA of the latest release for the linux/amd64 architecture.
# To accomplish this, we fetch all versions and identify the 5th version, which corresponds to the latest release with that specific architecture.
# Reference: https://docs.github.com/en/rest/packages/packages?apiVersion=2022-11-28#list-package-versions-for-a-package-owned-by-an-organization
## NOTE: The SHA obtained in this step differs from the previous one, as it represents the branch commit SHA used for change verification.

- name: Get Docker image digests
if: steps.check_release_changes.outputs.release_changed == 'true'
id: get_digests
run: |
digests=$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /orgs/actions/packages/container/actions-runner/versions)
latest_digest=$(echo "$digests" | jq -r '.[] | select(.metadata.container.tags | index("latest")) | .name')
echo $latest_digest
echo "arch_sha_name=$latest_digest" >> "$GITHUB_OUTPUT"
env:
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
mamu0 marked this conversation as resolved.
Show resolved Hide resolved

- name: Get changelog file
id: changelog
run: |
if [ ! -f CHANGELOG.md ]; then
echo "Changelog file does not exist. Creating one..."
touch CHANGELOG.md
fi

- name: Update Changelog
if: steps.check_release_changes.outputs.release_changed == 'true'
id: update_changelog
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
mamu0 marked this conversation as resolved.
Show resolved Hide resolved
const fs = require('fs');
const changelogPath = 'CHANGELOG.md';
const dockerImage = `docker pull ghcr.io/actions/runner:${{ steps.release_outputs.outputs.tag_name }}@${{ steps.get_digests.outputs.arch_sha_name }}`;

const changelogEntry = `## ${{ steps.release_outputs.outputs.tag_name }}\n- Docker Image: \`${dockerImage}\`\n`;

const changelogContent = fs.readFileSync(changelogPath, 'utf8');
fs.writeFileSync(changelogPath, `${changelogEntry}\n${changelogContent}`);

- name: Update Dockerfile
if: steps.check_release_changes.outputs.release_changed == 'true'
id: update_dockerfile
run: |
echo "Updating Dockerfile..."

# Construct the new FROM statement
new_from="FROM ghcr.io/actions/actions-runner:${{ steps.release_outputs.outputs.short_tag_name }}@${{ steps.get_digests.outputs.arch_sha_name }} AS base"

# Replace the first line with the new FROM statement
sed -i "1s|.*|$new_from|" Dockerfile

echo "Dockerfile updated."

## NOTE: To permit the PR creation the option "Allow GitHub Actions to create and approve pull requests" must be enabled in Repository Settings -> Actions -> General.
# Reference: https://github.com/peter-evans/create-pull-request
# https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request?tool=cli
- name: Create PR
if: steps.check_release_changes.outputs.release_changed == 'true'
uses: peter-evans/create-pull-request@v6
mamu0 marked this conversation as resolved.
Show resolved Hide resolved
with:
commit-message: 'Update Dockerfile image release to ${{ steps.release_outputs.outputs.tag_name }} with sha ${{ steps.get_digests.outputs.arch_sha_name }}'
title: '[${{ steps.release_outputs.outputs.short_tag_name }}] Update Dockerfile image Release to ${{ steps.release_outputs.outputs.tag_name }}'
delete-branch: true
base: main
env:
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
mamu0 marked this conversation as resolved.
Show resolved Hide resolved

# How to configure: https://github.com/ravsamhq/notify-slack-action
- name: Drift Notification
if: ${{ always() && (env.SLACK_WEBHOOK_URL) && steps.check_release_changes.outputs.release_changed == 'true' }}
uses: ravsamhq/notify-slack-action@2.3.0
with:
status: ${{ job.status }}
notification_title: "New Release identified [${{ steps.release_outputs.outputs.tag_name }}]"
message_format: |
{emoji} *{workflow}* Release:
- Tag: ${{ steps.release_outputs.outputs.tag_name }}
- Branch SHA: ${{ steps.release_outputs.outputs.sha }}
- Release URL: https://github.com/actions/runner/releases/tag/${{ steps.release_outputs.outputs.tag_name }}
footer: "Linked Repo <{repo_url}|{repo}>"
env:
SLACK_WEBHOOK_URL: ${{ env.SLACK_WEBHOOK_URL }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.tmp
.secrets
4 changes: 4 additions & 0 deletions release.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
mamu0 marked this conversation as resolved.
Show resolved Hide resolved
"current_tag": "v2.317.0",
"current_sha": "6dc005ee3690a0303e0d7269b6f60295d22dcfad"
}
Loading