NHSBookingandReferrals Back up simplifier project #22
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: NHSBookingandReferrals Back up simplifier project | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: '0 16 * */1 *' | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Sleep random #Used to stop error due to multiple actions wanting PRs to be merged at the same time | |
run: sleep $((RANDOM %30))m $((RANDOM % 59))s #sleeps for a random number of minutes and seconds | |
- name: Set today | |
id: today | |
run: echo "TODAY=$(date)" >> $GITHUB_OUTPUT | |
- name: Set branch name | |
id: branch-name | |
run: echo "BRANCH=$(echo ${{ steps.today.outputs.TODAY }} | tr -d '[:space:]' | tr -d ':')" >> $GITHUB_OUTPUT | |
- name: Download and commit simplifier source code | |
run: | | |
cd NHSBookingandReferrals | |
rm -rf backup | |
mkdir -p backup | |
cd backup | |
wget --user ${{secrets.SIMPLIFIER_USER}} --password ${{secrets.SIMPLIFIER_PWD}} ${{ vars.NHSBOOKINGANDREFERRALS_SIMPLIFIER_ADDRESS }} -O export.zip | |
unzip export.zip -x settings.style | |
mv export.zip .. | |
cd .. | |
export NUMLINES=$(wc -l < README.md) #Updates README file. Shows when last updated but also used to ensure changes made for the PR / merge steps below | |
export NUMLINES=$(($NUMLINES-2)) | |
head -$NUMLINES README.md > README1.md | |
echo "" >> README1.md | |
echo "Updated at: $(date)" >> README1.md | |
mv README1.md README.md | |
- name: Create branch and commit | |
run: | | |
git config --global user.name "Workflows.Backup" | |
git config --global user.email "[email protected]" | |
git checkout -b ${{ steps.branch-name.outputs.BRANCH }} | |
git add -A | |
git commit -m "${{ steps.today.outputs.TODAY }}" | |
git push origin ${{ steps.branch-name.outputs.BRANCH }} | |
- name: Create Pull Request | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const { repo, owner } = context.repo; | |
const result = await github.rest.pulls.create({ | |
title: '${{ steps.today.outputs.TODAY }}', | |
owner, | |
repo, | |
head: '${{ steps.branch-name.outputs.BRANCH }}', | |
base: 'main', | |
body: [ | |
'This PR is auto-generated by', | |
'[actions/github-script](https://github.com/actions/github-script).' | |
].join('\n') | |
}); | |
github.rest.issues.addLabels({ | |
owner, | |
repo, | |
issue_number: result.data.number, | |
labels: ['backup', 'automated pr'] | |
}); | |
- name: Wait for PR to complete | |
run: sleep 10s | |
- name: "Merge pull request" | |
uses: "actions/github-script@v6" | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const { repo, owner } = context.repo; | |
const head = '${{ steps.branch-name.outputs.BRANCH }}' | |
await new Promise(r => setTimeout(r, 2000)); | |
const { data } = await github.rest.pulls.list({ owner, repo, head, per_page: 1 }); | |
if (data.length) { | |
const pull_number = data[0].number; | |
await github.rest.pulls.merge({ owner, repo, pull_number, merge_method: "squash"}); | |
} | |