release v2.0.0 #29
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: Build and Push Docker Image | |
on: | |
push: | |
branches: | |
- app | |
permissions: | |
contents: write | |
pull-requests: write | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Checkout the 'app' branch code | |
- name: Checkout app branch code | |
uses: actions/checkout@v4 | |
# Step 2: Install jq to parse package.json | |
- name: Install jq | |
run: sudo apt-get install -y jq | |
# 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: Set up Docker in the workflow | |
- name: Set up Docker | |
uses: docker/setup-buildx-action@v3 | |
# Step 5: Log in to Docker Hub using secrets | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USER }} | |
password: ${{ secrets.DOCKER_PASS }} | |
# Step 6: Build Docker image with version tag | |
- name: Build Docker image | |
run: | | |
docker build -t ${{ secrets.DOCKER_USER }}/gitops-app:${{ env.APP_VERSION }} app/. | |
# Step 7: Push Docker image to Docker Hub | |
- name: Push Docker image | |
run: | | |
docker push ${{ secrets.DOCKER_USER }}/gitops-app:${{ env.APP_VERSION }} | |
# Step 8: Checkout the 'main' branch code | |
- name: Checkout main branch code | |
uses: actions/checkout@v4 | |
with: | |
ref: main | |
# Step 9: 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 10: Compare versions between 'app' and 'main' branches | |
- name: Compare versions | |
id: version_check | |
run: | | |
if [ "$APP_VERSION" != "$MAIN_VERSION" ]; then | |
echo "Version changed [v$MAIN_VERSION -> v$APP_VERSION]: Creating PR." | |
echo "VERSION_CHANGED=true" >> $GITHUB_ENV | |
else | |
echo "No version change [v$APP_VERSION]: Skipping PR." | |
echo "VERSION_CHANGED=false" >> $GITHUB_ENV | |
fi | |
# Step 11: Reset 'app' branch to the latest code | |
- name: Reset app branch | |
run: | | |
git fetch origin app | |
git reset --hard origin/app | |
# Step 12: Create a Pull Request only if the version changed and Docker image was built successfully | |
- name: Create Pull Request | |
if: success() && env.VERSION_CHANGED == 'true' | |
uses: peter-evans/create-pull-request@v7 | |
with: | |
branch: merge-app-${{ env.APP_VERSION }} | |
title: "Automated PR: Merge 'app' (v${{ env.APP_VERSION }}) into 'main'" | |
labels: automated, release |