-
Notifications
You must be signed in to change notification settings - Fork 0
133 lines (113 loc) · 5.17 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
on:
push:
branches:
- main
- development
- qa
- staging
jobs:
deploy:
runs-on: self-hosted
env:
DOTNET_VERSION: 8.x.x
DOTNET_SRC: ./
NEXUS_SOURCE: https://hubrepopanda.pandatech.it/repository/nuget-hosted/index.json
NUGET_SOURCE: https://api.nuget.org/v3/index.json
DOCKER_REPO_BASE: repopanda.pandatech.it/pandatech/repository/pandatech
SERVICE_NAME: ${{ github.event.repository.name }}
K8S_NAMESPACE: ${{
(github.ref == 'refs/heads/staging' && 'staging') ||
(github.ref == 'refs/heads/qa' && 'qa') ||
(github.ref == 'refs/heads/development' && 'dev')
}}
#(github.ref == 'refs/heads/main' && 'production') ||
K8S_CLUSTER_ADDRESS: https://10.0.8.11:6443
DOTNET_INSTALL_DIR: $HOME/dotnet
steps:
- uses: actions/checkout@v3
- name: Setup .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
include-prerelease: false
dotnet-install-dir: ${{ env.DOTNET_INSTALL_DIR }}
- name: remove old nuget sources
run: |
dotnet nuget remove source pandatech
dotnet nuget remove source nuget.org
continue-on-error: true
- name: Add local nuget source
run: |
dotnet nuget add source ${{ env.NEXUS_SOURCE }} -n pandatech -u ${{ secrets.nexus_username }} -p ${{ secrets.nexus_password }} --store-password-in-clear-text
continue-on-error: true
- name: Add NuGet.org source
run: |
dotnet nuget add source ${{ env.NUGET_SOURCE }} -n nuget.org
continue-on-error: true
- name: Restore
run: dotnet restore ${{env.DOTNET_SRC}} --verbosity normal --no-cache --force --disable-parallel
- name: Build
run: dotnet build ${{env.DOTNET_SRC}} --no-restore --verbosity normal --configuration Release
- name: Publish
run: dotnet publish ${{env.DOTNET_SRC}} --no-build --verbosity normal --configuration Release --output ./publish
- name: Set DOCKER_TAG
run: echo "DOCKER_TAG=$(date +'%Y%m%d%H%M%S')-$GITHUB_RUN_NUMBER" >> $GITHUB_ENV
- name: Display DOCKER_TAG
run: echo "DOCKER_TAG=${{ env.DOCKER_TAG }}"
- name: Set DOCKER_REPO_PATH
run: |
if [ "${{ github.event_name }}" == "push" ] && [ "${{ startsWith(github.ref, 'refs/heads/') }}" == "true" ]; then
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
DOCKER_REPO_PATH="prod"
elif [ "${{ github.ref }}" == "refs/heads/development" ]; then
DOCKER_REPO_PATH="dev"
else
DOCKER_REPO_PATH=$(echo "${{ github.ref }}" | sed 's#^refs/heads/##')
fi
else
DOCKER_REPO_PATH="unknown-branch"
fi
echo "DOCKER_REPO_PATH=${DOCKER_REPO_PATH}" >> $GITHUB_ENV
- name: Display DOCKER_REPO_PATH
run: echo "DOCKER_REPO_PATH=${{ env.DOCKER_REPO_PATH }}"
- name: Build Docker Image
run: |
DOCKER_TAG=$(date +'%Y%m%d%H%M%S')-$GITHUB_RUN_NUMBER
DOCKER_REPO_BASE=repopanda.pandatech.it/pandatech/repository/pandatech
DOCKER_REPO_PATH=${{ env.DOCKER_REPO_PATH }}
if [ "${{ env.DOCKER_REPO_PATH }}" == "main" ]; then
DOCKER_REPO=${DOCKER_REPO_BASE}/prod
elif [ "${{ env.DOCKER_REPO_PATH }}" == "development" ]; then
DOCKER_REPO=${DOCKER_REPO_BASE}/dev
else
DOCKER_REPO=${DOCKER_REPO_BASE}/${DOCKER_REPO_PATH}
fi
docker build . -t $DOCKER_REPO/${{ env.SERVICE_NAME }}:${DOCKER_TAG} -t $DOCKER_REPO/${{ env.SERVICE_NAME }}:latest
- name: Display Docker Image Tags
run: docker images $DOCKER_REPO/${{ env.SERVICE_NAME }}
- name: Login to Nexus
run: docker login ${{ env.DOCKER_REPO_BASE }} -u ${{ secrets.nexus_username }} -p ${{ secrets.nexus_password }}
- name: Push Docker Image to Nexus
run: |
DOCKER_TAG=${{ env.DOCKER_TAG }}
DOCKER_REPO=${DOCKER_REPO_BASE}/${DOCKER_REPO_PATH}
docker push "${DOCKER_REPO}/${SERVICE_NAME}:${DOCKER_TAG}"
docker push "${DOCKER_REPO}/${SERVICE_NAME}:latest"
- name: Update Deployment
if: github.ref != 'refs/heads/main'
run: |
export KUBECONFIG=/home/runner/.kube/config
sed -i "s|namespace: .*|namespace: ${{ env.K8S_NAMESPACE }}|g" ./kubernetes/deployment.yaml
sed -i "s|${{ env.DOCKER_REPO }}/${{ env.SERVICE_NAME }}:IMAGE_TAG_PLACEHOLDER|${{ env.DOCKER_REPO }}/${{ env.SERVICE_NAME }}:${{ env.DOCKER_TAG }}|g" ./kubernetes/deployment.yaml
sed -i "s|namespace: .*|namespace: ${{ env.K8S_NAMESPACE }}|g" ./kubernetes/service.yaml
sed -i "s|namespace: .*|namespace: ${{ env.K8S_NAMESPACE }}|g" ./kubernetes/ingress.yaml
kubectl apply -f ./kubernetes/deployment.yaml -n ${{ env.K8S_NAMESPACE }}
env:
DOCKER_TAG: ${{ env.DOCKER_TAG }}
K8S_CLUSTER_ADDRESS: ${{ env.K8S_CLUSTER_ADDRESS }}
- name: 'Cleanup build folder'
run: |
ls -la ./
rm -rf ./* || true
rm -rf ./.??* || true
ls -la ./