Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌋 CI/CD pipeline updates #7

Merged
merged 1 commit into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions .github/workflows/packer-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Instance Refresh
env:
ARTIFACT_NAME: webapp
AWS_REGION: us-east-1
ENVIRONMENT: test

on:
push:
Expand All @@ -17,11 +18,11 @@ jobs:

name: Run unit tests
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: 'Create .env file'
run: |
touch .env
echo ENVIRONMENT=${{ secrets.ENVIRONMENT }} >> .env
echo ENVIRONMENT=${{ env.ENVIRONMENT }} >> .env
echo HOSTNAME=${{ secrets.HOSTNAME }} >> .env
echo PORT=${{ secrets.PORT }} >> .env
echo DATABASE=${{ secrets.DATABASE }} >> .env
Expand All @@ -39,12 +40,12 @@ jobs:
name: Build AWS Custom AMI with REST API app artifacts
steps:
- name: Checkout code from branch
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Build artifacts
run: |
touch .env
echo ENVIRONMENT=${{ secrets.ENVIRONMENT }} >> .env
echo ENVIRONMENT=${{ env.ENVIRONMENT }} >> .env
echo HOSTNAME=${{ secrets.HOSTNAME }} >> .env
echo PORT=${{ secrets.PORT }} >> .env
echo DATABASE=${{ secrets.DATABASE }} >> .env
Expand All @@ -60,26 +61,24 @@ jobs:
aws-secret-access-key: ${{ secrets.AWS_DEV_SECRET_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Packer init
uses: hashicorp/packer-github-actions@master
- name: Setup `packer`
uses: hashicorp/setup-packer@main
id: setup
with:
command: init
target: ami.pkr.hcl
version: 'latest'

- name: Packer init
id: init
run: 'packer init ami.pkr.hcl'

- name: Validate packer template
uses: hashicorp/packer-github-actions@master
with:
command: validate
target: ami.pkr.hcl
id: validate
run: 'packer validate -evaluate-datasources ami.pkr.hcl'

- name: Build AMI using Packer
id: build
run: 'packer build ami.pkr.hcl'

- name: Packer build
uses: hashicorp/packer-github-actions@master
with:
command: build
arguments: '-color=false -on-error=abort'
target: ami.pkr.hcl
env:
PACKER_LOG: 1
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
Expand Down
22 changes: 12 additions & 10 deletions .github/workflows/packer-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,31 @@ name: Packer AMI template validation

on:
pull_request:
branches: [ master ]
branches: [master]

jobs:
validate:
runs-on: ubuntu-latest
name: Validate packer template for the custom AMI
steps:
- name: Checkout code from branch
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Build artifacts
run: |
zip -r webapp.zip ./
pwd ./webapp

- name: Packer init
uses: hashicorp/packer-github-actions@master
- name: Setup `packer`
uses: hashicorp/setup-packer@main
id: setup
with:
command: init
target: ami.pkr.hcl
version: 'latest'

- name: Packer init
id: init
run: 'packer init ami.pkr.hcl'

- name: Validate packer template
uses: hashicorp/packer-github-actions@master
with:
command: validate
target: ami.pkr.hcl
id: validate
run: 'packer validate -evaluate-datasources ami.pkr.hcl'
37 changes: 20 additions & 17 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

name: Unit Test Suite

env:
ENVIRONMENT: test

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:
Expand All @@ -18,19 +21,19 @@ jobs:

name: Run unit tests
steps:
- uses: actions/checkout@v2
- name: 'Create .env file'
run: |
touch .env
echo ENVIRONMENT=${{ secrets.ENVIRONMENT }} >> .env
echo HOSTNAME=${{ secrets.HOSTNAME }} >> .env
echo PORT=${{ secrets.PORT }} >> .env
echo DATABASE=${{ secrets.DATABASE }} >> .env
echo DBUSER=${{ secrets.DBUSER }} >> .env
echo DBPASSWORD=${{ secrets.DBPASSWORD }} >> .env
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn test
- uses: actions/checkout@v3
- name: 'Create .env file'
run: |
touch .env
echo ENVIRONMENT=${{ env.ENVIRONMENT }} >> .env
echo HOSTNAME=${{ secrets.HOSTNAME }} >> .env
echo PORT=${{ secrets.PORT }} >> .env
echo DATABASE=${{ secrets.DATABASE }} >> .env
echo DBUSER=${{ secrets.DBUSER }} >> .env
echo DBPASSWORD=${{ secrets.DBPASSWORD }} >> .env
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn test
2 changes: 1 addition & 1 deletion ami.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ source "amazon-ebs" "ec2" {
# ami_name = "EC2-AMI-${substr(data.git-commit.commit.hash, 0, 8)}"
# ami_name = "EC2-AMI-{{ ${substr(data.git-commit.cwd-head.hash, 0, 8)} }}"
region = "${var.aws_region}"
ami_name = "EC2-AMI-${local.timestamp}"
ami_name = "EC2-AMI-${local.version}"
ami_description = "EC2 AMI for CSYE 6225 built by ${data.git-commit.cwd-head.author}"
ami_users = [
"057919684206", # dev account ID
Expand Down
6 changes: 4 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ app.use(express.json())

app.use('/', health, userRoutes, documentRoute)

db.connectionTest()
db.sequelize.sync()
if (ENVIRONMENT !== 'test') {
db.connectionTest()
db.sequelize.sync()
}
app.listen(PORT, () => {
if (ENVIRONMENT !== 'prod')
logger.info(`Server running at http://${HOSTNAME}:${PORT}`)
Expand Down
2 changes: 1 addition & 1 deletion nodeserver.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ After=network.target
[Service]
Type=simple
User=ubuntu
ExecStart=/usr/bin/node /home/ubuntu/webapp/app.js
ExecStart=node /home/ubuntu/webapp/app.js
EnvironmentFile=/home/ubuntu/webapp/.env
# ExecStartPost=/bin/sh -c "sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 1337"
Restart=on-failure
Expand Down
9 changes: 6 additions & 3 deletions scripts/post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ echo "+-------------------------------------------------------------------------
sudo cp /home/ubuntu/nodeserver.service /lib/systemd/system/nodeserver.service
echo "Enabling the REST API Service"
sudo systemctl daemon-reload
sleep 5
sudo systemctl enable nodeserver
# sudo systemctl start nodeserver
# sudo systemctl restart nodeserver
# sudo systemctl status nodeserver
sleep 5
sudo systemctl start nodeserver
sleep 5
sudo systemctl restart nodeserver
sudo systemctl status nodeserver
APISRVC=$?
if [ $APISRVC -eq 0 ]; then
echo "API service is installed successfully!"
Expand Down