Update README.md #37
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: Unit Test, Build Docker Image, Deploy to AWS EC2 | |
on: | |
pull_request: | |
branches: | |
- 'main' | |
- 'development' | |
push: | |
branches: | |
- 'main' | |
- 'development' | |
jobs: | |
#job 1: unit test | |
unit-test: | |
name: 'Do unit test to the project' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Setup github action (CI) | |
uses: actions/checkout@v2 | |
- name: Setup golang | |
uses: actions/setup-go@v2 | |
with: | |
go-version: 1.17 | |
- name: Running unit test | |
run: go test ./... -cover | |
#job 2: build docker image, push registry | |
build-push-docker: | |
name: 'Build image and push to registry' | |
runs-on: ubuntu-latest | |
needs: unit-test | |
steps: | |
- uses: actions/checkout@v2 | |
- name: build docker | |
run: docker build -t daffaalex22/kampus_merdeka:1.0.0 . | |
- name: login to docker hub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKER_HUB_USERNAME }} | |
password: ${{ secrets.DOCKER_HUB_PASSWORD }} | |
- name: push the previously built image to registry | |
run: docker push daffaalex22/kampus_merdeka:1.0.0 | |
#job 3: pull and deploy from AWS EC2 | |
deployment-to-ec2: | |
name: 'Deploy the image pushed to dockehub registry, to EC2' | |
runs-on: ubuntu-latest | |
needs: build-push-docker | |
steps: | |
- uses: actions/checkout@v2 | |
- name: configuration SSH | |
env: | |
SSH_USER: ${{ secrets.SSH_USERNAME }} | |
SSH_KEY: ${{ secrets.SSH_KEY }} | |
SSH_HOST: ${{ secrets.SSH_HOST }} | |
run: | | |
mkdir -p ~/.ssh/ | |
echo "$SSH_KEY" > ~/.ssh/jobdir.pem | |
chmod 400 ~/.ssh/jobdir.pem | |
cat >>~/.ssh/config <<END | |
Host development | |
HostName $SSH_HOST | |
User $SSH_USER | |
IdentityFile ~/.ssh/jobdir.pem | |
StrictHostKeyChecking=no | |
END | |
- name: Connect EC2 & Remove All Container & Pull from Registry & Run on Container | |
run: ssh development 'docker rm -f $(docker ps -a -q) && docker pull daffaalex22/kampus_merdeka:1.0.0 && docker run -d -p 8080:8080 --name kampus_merdeka daffaalex22/kampus_merdeka:1.0.0' |