grant repo write access to github actions bot #6
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 | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Checkout the latest code from the repo | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
# 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 }} | |
# Step 8: Merge app branch into main if build succeeds | |
- name: Merge app branch into main | |
if: success() # Only run this step if all previous steps succeeded | |
run: | | |
git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
git config --global user.name "GitHub Actions" | |
git fetch origin | |
git checkout main | |
git merge --no-ff app | |
git push origin main | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |