Update Version #279
This file contains hidden or 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
| name: Update Version | |
| on: | |
| schedule: | |
| - cron: '0 8 * * *' | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| env: | |
| DEPENDENCY_NAME: Samba | |
| DOCKERFILE_PATH: samba/Dockerfile | |
| DOCKERFILE_VERSION_KEY: SAMBA_VERSION | |
| DOCKERFILE_OS_VERSION_KEY: ALPINE_VERSION | |
| jobs: | |
| update-version: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v6 | |
| with: | |
| token: ${{ secrets.BOT_GH_TOKEN != '' && secrets.BOT_GH_TOKEN || secrets.GITHUB_TOKEN }} | |
| - name: Configure Git user | |
| run: | | |
| git config user.name github-actions[bot] | |
| git config user.email 41898282+github-actions[bot]@users.noreply.github.com | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v4 | |
| with: | |
| username: ${{ secrets.DOCKER_HUB_USERNAME }} | |
| password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | |
| - name: Get current version | |
| id: get-current | |
| run: | | |
| get_version() { grep "ARG $1=" "$DOCKERFILE_PATH" | cut -d '=' -f2 | tr -d '[:space:]'; } | |
| version="$(get_version "$DOCKERFILE_VERSION_KEY")" | |
| os_version="$(get_version "$DOCKERFILE_OS_VERSION_KEY")" | |
| echo "Current version: $version (os: $os_version)" | |
| echo "version=$version" >> $GITHUB_OUTPUT | |
| echo "os_version=$os_version" >> $GITHUB_OUTPUT | |
| - name: Get latest version | |
| id: get-latest | |
| run: | | |
| version="$(docker run --rm alpine sh -c "apk --no-cache --no-progress update >/dev/null && apk upgrade >/dev/null && apk search -v -e samba | tr ' ' '\n' | grep 'samba-' | cut -d'-' -f2-")" | |
| if [ -z "$version" ]; then | |
| echo "Failed to fetch latest version" >&2 | |
| exit 1 | |
| fi | |
| # Retrieve OS version | |
| os_version="$(docker run --rm alpine cat /etc/alpine-release)" | |
| if [ -z "$os_version" ]; then | |
| echo "Failed to fetch Alpine version" >&2 | |
| exit 1 | |
| fi | |
| echo "Latest version: $version (os: $os_version)" | |
| echo "version=$version" >> $GITHUB_OUTPUT | |
| echo "os_version=$os_version" >> $GITHUB_OUTPUT | |
| - name: Check if update is needed | |
| id: check | |
| run: | | |
| if [ "${{ steps.get-latest.outputs.version }}" = "${{ steps.get-current.outputs.version }}" ] \ | |
| && [ "${{ steps.get-latest.outputs.os_version }}" = "${{ steps.get-current.outputs.os_version }}" ]; then | |
| echo "up_to_date=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "up_to_date=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Pull request metadata | |
| if: steps.check.outputs.up_to_date == 'false' | |
| id: pr | |
| env: | |
| GH_TOKEN: ${{ secrets.BOT_GH_TOKEN != '' && secrets.BOT_GH_TOKEN || secrets.GITHUB_TOKEN }} | |
| run: | | |
| version="${{ steps.get-latest.outputs.version }}" | |
| title="Update $DEPENDENCY_NAME to v$version (os: ${{ steps.get-latest.outputs.os_version }})" | |
| prs=$(gh pr list --search "$title" --json title | jq length) | |
| if [ "$prs" -gt 0 ]; then | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| fi | |
| echo "title=$title" >> $GITHUB_OUTPUT | |
| echo "body=Bumps $DEPENDENCY_NAME to version $version" >> $GITHUB_OUTPUT | |
| echo "head_branch=update/v$version" >> $GITHUB_OUTPUT | |
| echo "base_branch=main" >> $GITHUB_OUTPUT | |
| - name: Create Pull Request | |
| if: steps.pr.outputs.exists == 'false' && steps.check.outputs.up_to_date == 'false' | |
| env: | |
| GH_TOKEN: ${{ secrets.BOT_GH_TOKEN != '' && secrets.BOT_GH_TOKEN || secrets.GITHUB_TOKEN }} | |
| run: | | |
| version="${{ steps.get-latest.outputs.version }}" | |
| branch="${{ steps.pr.outputs.head_branch }}" | |
| git checkout -b "$branch" | |
| update_dockerfile_version() { sed -i "s|ARG $1=.*|ARG $1=$2|" "$DOCKERFILE_PATH"; } | |
| update_dockerfile_version "$DOCKERFILE_VERSION_KEY" "$version" | |
| update_dockerfile_version "$DOCKERFILE_OS_VERSION_KEY" "${{ steps.get-latest.outputs.os_version }}" | |
| sed -i -E "s|-\s*\[\`[0-9]+\`,\s*\`[0-9.]+\`,\s*\`latest\` \(${DOCKERFILE_PATH}\)\]|- [\`$(echo "$version" | cut -d'.' -f1,1)\`, \`$(echo "$version" | cut -d'.' -f1,2)\`, \`latest\` (${DOCKERFILE_PATH})]|" README.md | |
| git commit -am "Update version to $version" | |
| git push origin "$branch" | |
| gh pr create --title "${{ steps.pr.outputs.title }}" \ | |
| --body "${{ steps.pr.outputs.body }}" \ | |
| --base "${{ steps.pr.outputs.base_branch }}" --head "$branch" |