Skip to content

Commit

Permalink
refactor workflow to compare versions between 'app' and 'main' before…
Browse files Browse the repository at this point in the history
… creating PR
  • Loading branch information
bullet-ant committed Sep 15, 2024
1 parent b1bca02 commit 240ab20
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,76 @@ jobs:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the latest code from the repo
- name: Checkout code
# Step 1: Checkout the 'app' branch code
- name: Checkout app branch code
uses: actions/checkout@v4
with:
ref: app

# Step 2: Install jq to parse package.json
- name: Install jq
run: sudo apt-get install -y jq

# Step 3: Set up Docker in the workflow
# Step 3: Get version from 'app' branch
- name: Get version from app branch
id: app_version
run: |
APP_VERSION=$(cat app/package.json | jq -r .version)
echo "APP_VERSION=$APP_VERSION" >> $GITHUB_ENV
echo "Version on 'app' branch: $APP_VERSION"
# Step 4: Checkout the 'main' branch code
- name: Checkout main branch code
uses: actions/checkout@v4
with:
ref: main

# Step 5: Get version from 'main' branch
- name: Get version from main branch
id: main_version
run: |
MAIN_VERSION=$(cat app/package.json | jq -r .version)
echo "MAIN_VERSION=$MAIN_VERSION" >> $GITHUB_ENV
echo "Version on 'main' branch: $MAIN_VERSION"
# Step 6: Set up Docker in the workflow
- name: Set up Docker
uses: docker/setup-buildx-action@v3

# Step 4: Log in to Docker Hub using secrets
# Step 7: Log in to Docker Hub using secrets (Optional)
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PASS }}

# Step 5: Extract version from package.json
- name: Extract version from package.json
id: get_version
run: |
VERSION=$(cat app/package.json | jq -r .version)
echo "VERSION=$VERSION"
echo "::set-output name=version::$VERSION"
# Step 6: Build Docker image with version tag
# Step 8: Build Docker image with version tag (Optional)
- name: Build Docker image
run: |
docker build -t ${{ secrets.DOCKER_USER }}/gitops-app:${{ steps.get_version.outputs.version }} app/.
docker build -t ${{ secrets.DOCKER_USER }}/gitops-app:${{ env.APP_VERSION }} app/.
# Step 7: Push Docker image to Docker Hub
# Step 9: Push Docker image to Docker Hub (Optional)
- name: Push Docker image
run: |
docker push ${{ secrets.DOCKER_USER }}/gitops-app:${{ steps.get_version.outputs.version }}
- name: Checkout main
uses: actions/checkout@v4
with:
ref: main
docker push ${{ secrets.DOCKER_USER }}/gitops-app:${{ env.APP_VERSION }}
- name: Reset app branch
# Step 10: Compare versions between 'app' and 'main' branches
- name: Compare versions
id: version_check
run: |
git fetch origin app
git reset --hard origin/app
if [ "$APP_VERSION" != "$MAIN_VERSION" ]; then
echo "Version changed: Creating PR."
echo "VERSION_CHANGED=true" >> $GITHUB_ENV
else
echo "No version change: Skipping PR."
echo "VERSION_CHANGED=false" >> $GITHUB_ENV
fi
# Step 11: Create a Pull Request only if the version changed and Docker image was built successfully
- name: Create Pull Request
if: success()
if: success() && env.VERSION_CHANGED == 'true'
uses: peter-evans/create-pull-request@v7
with:
branch: merge-app
delete-branch: true
title: "Automated PR: Merge 'app' into 'main'"
branch: merge-app-${{ env.APP_VERSION }}
title: "Automated PR: Merge 'app' into 'main' (Version ${{ env.APP_VERSION }})"
labels: image-built

0 comments on commit 240ab20

Please sign in to comment.