This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
Update Deploy Branch #12922
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 Deploy Branch | |
on: | |
schedule: | |
- cron: '*/5 * * * *' # Runs every 5 minutes | |
workflow_dispatch: # Allows manual triggering of the workflow | |
inputs: | |
CHAIN_URL: | |
description: 'The URL of the chain. Example: https://emerynet.api.agoric.net' | |
required: false | |
default: '' | |
DEPLOY_BRANCH: | |
description: 'The branch to deploy. Example: deploy-emerynet' | |
required: false | |
default: '' | |
COMMIT_URL: | |
description: '(optional) Override ReferenceUI value with the url of the commit to deploy' | |
required: false | |
default: '' | |
jobs: | |
run-command: | |
runs-on: ubuntu-latest | |
env: | |
CHAIN_URL: ${{ github.event.inputs.CHAIN_URL || vars.CHAIN_URL }} | |
DEPLOY_BRANCH: ${{ github.event.inputs.DEPLOY_BRANCH || vars.DEPLOY_BRANCH }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Install jq | |
run: sudo apt-get install -y jq | |
- name: Check for required variables | |
id: check_variables | |
run: | | |
SKIP_RUN=false | |
if [ -z "${CHAIN_URL}" ]; then | |
echo "CHAIN_URL is not set. Skipping workflow." | |
SKIP_RUN=true | |
fi | |
if [ -z "${DEPLOY_BRANCH}" ]; then | |
echo "CHAIN_URL is not set. Skipping workflow." | |
SKIP_RUN=true | |
fi | |
echo "SKIP_RUN=$SKIP_RUN" >> $GITHUB_ENV | |
- name: Check if deploy branch needs to be updated | |
if: env.SKIP_RUN == 'false' | |
run: | | |
# Define the URL from the environment variable | |
URL="${{ env.CHAIN_URL }}/agoric/vstorage/data/published.vaultFactory.governance" | |
# Fetch the new value | |
NEW_REFERENCED_UI=$(curl -s $URL | jq -r '.value' | jq -r '.values[]' | jq -r .body | sed 's/^#//' | jq -r .current.ReferencedUI.value) | |
if [ ${{ github.event.inputs.COMMIT_URL }} ]; then | |
NEW_REFERENCED_UI="${{ github.event.inputs.COMMIT_URL }}" | |
fi | |
# Check if the deploy branch exists on the remote | |
if git ls-remote --exit-code --heads origin ${{ env.DEPLOY_BRANCH }}; then | |
echo "Deploy branch exists. Checking out the branch." | |
git fetch origin | |
git checkout ${{ env.DEPLOY_BRANCH }} | |
# Path to store the previous value | |
OLD_REFERENCED_UI_FILE="./DEPLOYED_HASH" | |
# Check if the previous value exists | |
if [ -f "$OLD_REFERENCED_UI_FILE" ]; then | |
# Read the previous value | |
OLD_REFERENCED_UI=$(cat $OLD_REFERENCED_UI_FILE) | |
else | |
# File does not exist, set previous value to empty | |
OLD_REFERENCED_UI="" | |
fi | |
else | |
echo "Deploy branch does not exist. Skipping checkout." | |
# File does not exist, set previous value to empty | |
OLD_REFERENCED_UI="" | |
fi | |
echo "New value: $NEW_REFERENCED_UI" | |
echo "Old value: $OLD_REFERENCED_UI" | |
# Output the values for the next step | |
if [ "$NEW_REFERENCED_UI" != "$OLD_REFERENCED_UI" ]; then | |
SHOULD_DEPLOY=true | |
else | |
SHOULD_DEPLOY=false | |
fi | |
echo "SHOULD_DEPLOY=$SHOULD_DEPLOY" >> $GITHUB_ENV | |
echo "NEW_VALUE=$NEW_REFERENCED_UI" >> $GITHUB_ENV | |
- name: Deploy commit to branch | |
if: env.SHOULD_DEPLOY == 'true' && env.SKIP_RUN == 'false' | |
run: | | |
# Check out the specific commit | |
git fetch origin | |
REPO_URL=$(echo "${{ env.NEW_VALUE }}" | sed -E 's|/commit/.*||').git | |
COMMIT_HASH=$(echo "${{ env.NEW_VALUE }}" | awk -F'/commit/' '{print $2}') | |
git config --global user.name "github-actions" | |
git config --global user.email "[email protected]" | |
# Clone the old repository and checkout the specific commit | |
git clone --single-branch $REPO_URL old-repo | |
cd old-repo | |
# Configure the remote URL with credentials | |
git remote add new-repo "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" | |
git fetch new-repo | |
git checkout -b deploy $COMMIT_HASH | |
# GitHub Actions worker doesn't have the correct permissions to checked in | |
# modified workflow files. To workaround this, we'll remove all files in | |
# .github/workflows that doesn't exist on the remote | |
# Remove files not on the remote | |
rm -rf .github/workflows | |
# Check if the deploy branch exists on the remote | |
if git ls-remote --exit-code --heads origin ${{ env.DEPLOY_BRANCH }}; then | |
# Mirror remote workflows if they exist | |
if git ls-tree --name-only -r new-repo/${{ vars.DEPLOY_BRANCH }}:.github/workflows &> /dev/null; then | |
git checkout new-repo/${{ vars.DEPLOY_BRANCH }} .github/workflows | |
fi | |
fi | |
# Save the new value to the DEPLOYED_HASH file | |
echo "$NEW_VALUE" > ./DEPLOYED_HASH | |
git add ./DEPLOYED_HASH | |
git commit -am "Prepare branch for deploy" | |
# # Push the commit to the deploy branch in the new repository | |
git push -f new-repo HEAD:${{ env.DEPLOY_BRANCH }} |