Update nvmrc #18
Workflow file for this run
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
name: Update .nvmrc | |
on: | |
pull_request: | |
branches: | |
- main | |
paths: | |
- 'Dockerfile' | |
types: [opened, synchronize] | |
jobs: | |
update-nvmrc: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # pin@v3 | |
with: | |
fetch-depth: 0 | |
- name: Extract Node.js version from Dockerfile | |
id: extract_version | |
run: | | |
VERSION=$(grep -m 1 -oP 'FROM node:\K[0-9]+\.[0-9]+\.[0-9]+' Dockerfile) | |
VERSION=$(echo $VERSION | tr -d '\n\r') | |
echo "NODE_VERSION=$VERSION" >> $GITHUB_ENV | |
- name: Read current .nvmrc version | |
id: read_nvmrc | |
run: | | |
if [ -f .nvmrc ]; then | |
CURRENT_VERSION=$(cat .nvmrc | tr -d '\n\r') | |
echo "CURRENT_VERSION=$CURRENT_VERSION" >> $GITHUB_ENV | |
else | |
echo "CURRENT_VERSION=" >> $GITHUB_ENV | |
fi | |
- name: Check if versions are different | |
id: check_diff | |
run: | | |
if [ "$NODE_VERSION" = "$CURRENT_VERSION" ]; then | |
echo "Versions are the same. No update needed." | |
echo "create_pr=false" >> $GITHUB_ENV | |
else | |
echo "Versions are different. Update needed." | |
echo "create_pr=true" >> $GITHUB_ENV | |
fi | |
- name: Create a new branch if update is needed | |
if: env.create_pr == 'true' | |
id: create_branch | |
run: | | |
BRANCH_NAME=update-nvmrc-${{ github.sha }} | |
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV | |
git checkout -b $BRANCH_NAME | |
- name: Update .nvmrc if update is needed | |
if: env.create_pr == 'true' | |
run: | | |
echo "$NODE_VERSION" > .nvmrc | |
git add .nvmrc | |
git commit -m "Update .nvmrc to Node.js version $NODE_VERSION" | |
- name: Push changes to new branch if update is needed | |
if: env.create_pr == 'true' | |
env: | |
PAT_TOKEN: ${{ secrets.DEV_PAT_NVMRC_ACTION }} | |
run: | | |
git remote set-url origin https://${{ secrets.DEV_PAT_NVMRC_ACTION }}@github.com/${{ github.repository }}.git | |
git push origin $BRANCH_NAME | |
- name: Create pull request if update is needed | |
if: env.create_pr == 'true' | |
uses: peter-evans/create-pull-request@5874ea5902f1f64af5cc21e1112355f468bb9e7a | |
with: | |
token: ${{ secrets.DEV_PAT_NVMRC_ACTION }} | |
commit-message: "Update .nvmrc to Node.js version ${{ env.NODE_VERSION }}" | |
branch: ${{ env.BRANCH_NAME }} | |
title: "Update .nvmrc to Node.js version ${{ env.NODE_VERSION }}" | |
body: "This PR updates .nvmrc to Node.js version ${{ env.NODE_VERSION }}" | |
base: main |