-
Notifications
You must be signed in to change notification settings - Fork 0
95 lines (93 loc) · 3.84 KB
/
docker.yml
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
name: Publish Docker image
on:
push:
branches: [main, develop]
paths-ignore:
- "apps/skde/**"
pull_request:
branches: [main, develop, dependency_updates]
paths-ignore:
- "apps/skde/**"
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
push_to_registries:
name: Push Docker image to docker hub
runs-on: ubuntu-latest
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: "eu-west-1"
steps:
- name: Set release variables
if: github.ref == 'refs/heads/main'
run: |
echo "S3Key='prod-mong-api.json'" >> $GITHUB_ENV
echo "env_name=prod-mong-api" >> $GITHUB_ENV
echo "version_label=${GITHUB_SHA:0:7}_prod_${RANDOM}" >> $GITHUB_ENV
echo "descr='Release tag deploy'" >> $GITHUB_ENV
echo "cloudfront_id=${{ secrets.CLOUDFRONT_ID_PROD }}" >> $GITHUB_ENV
- name: Set QA variables
if: github.ref == 'refs/heads/develop'
run: |
echo "S3Key='qa-mong-api.json'" >> $GITHUB_ENV
echo "env_name=qa-mong-api" >> $GITHUB_ENV
echo "version_label=${GITHUB_SHA:0:7}_qa_${RANDOM}" >> $GITHUB_ENV
echo "descr='Main commits deploy'" >> $GITHUB_ENV
echo "cloudfront_id=${{ secrets.CLOUDFRONT_ID_QA }}" >> $GITHUB_ENV
- name: Check out the repo
uses: actions/checkout@v4
- run: corepack enable && yarn install
- run: yarn workspace api run build
- name: Lint Dockerfile
uses: hadolint/hadolint-action@v3.1.0
with:
dockerfile: "apps/api/Dockerfile"
- name: Prepare
id: docker_meta
uses: docker/metadata-action@v5
with:
images: hnskde/mong-api
tags: |
type=ref,event=branch
type=ref,event=pr
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Test docker image
run: |
docker build -t test --file apps/api/Dockerfile .
docker run -e ORIGIN="skde.no" -d test
sleep 5 # wait for it to die, if not working
docker kill $(docker ps --filter ancestor=test -q)
- name: Build and push to Docker Hub
uses: docker/build-push-action@v6
with:
context: .
file: ./apps/api/Dockerfile
build-args: version=${{ env.version_label }}
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Update elasticbeanstalk
if: github.event_name != 'pull_request'
run: |
aws elasticbeanstalk create-application-version --application-name mong-api --source-bundle S3Bucket="elasticbeanstalk-eu-west-1-247247832514",S3Key=${{ env.S3Key }} --version-label ${{ env.version_label }} --description ${{ env.descr }}
aws elasticbeanstalk update-environment --environment-name ${{ env.env_name }} --version-label ${{ env.version_label }}
if [[ ${{ github.ref }} == 'refs/heads/main' ]]; then
aws elasticbeanstalk update-environment --environment-name verify-mong-api --version-label ${{ env.version_label }}
fi
- name: Create CloudFront invalidation
if: github.event_name != 'pull_request'
run: |
aws cloudfront create-invalidation \
--distribution-id ${{ env.cloudfront_id }}\
--paths "/*"