refactor GitHub Actions workflow #16
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 latest code from the repo | |
- name: Checkout 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: 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 | |
- 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 | |
- name: Build Docker image | |
run: | | |
docker build -t ${{ secrets.DOCKER_USER }}/gitops-app:${{ steps.get_version.outputs.version }} app/. | |
# Step 7: Push Docker image to Docker Hub | |
- 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 | |
- name: Reset app branch | |
run: | | |
git fetch origin app | |
git reset --hard origin/app | |
- name: Create Pull Request | |
uses: peter-evans/create-pull-request@v7 | |
with: | |
branch: main-with-app |