-
Notifications
You must be signed in to change notification settings - Fork 0
63 lines (53 loc) · 1.92 KB
/
docker.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
name: Build and Push Docker Image
on:
push:
branches:
- app
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 }}