How To Deploy With GitLab CI/CD?
Dear Friend,
Last two weeks, we have been building the pipeline for the build and test stages. This week we will be deploying the artefact to a server.
If you haven't read previous articles on CI CD, you can check them here
Pre-requisite
Stage 1: Build a pipeline with .gitlab-ci.yml
As we have discussed the build and test previously, let's jump to the deployment stage.
The important part we need to focus on is ${HEROKU_PRODUCTION_KEY}
Two essential things here.
We can discuss the steps in Stage 2
image: maven:lates
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- mvn verify -DskipTests=true
artifacts:
paths:
- target/*.jar
test_job:
stage: test
script:
- mvn clean test
deploy_job:
stage: deploy
when: manual
script:
- HEROKU_API_KEY=${HEROKU_PRODUCTION_KEY} mvn heroku:deploy
Stage 2: Prepare the target deployment server.
Once you sign-up with Heroku, navigate to New --> Create new app
Key in the App name and choose preferred region then click Create app
Navigate to your profile and click Account settings
Scroll down until you see field named API Key
领英推荐
Click on Reveal button and copy the key.
Now, the actions are complete at Heroku end. We need to copy the API Key & app name to GitLab to establish connectivity.
Now, navigate to Settings ( at left pane ) --> CI/CD --> Variables --> click Expand button
Click Add variable and add Keys as below
Key HEROKU_APP_NAME Value - Copy from Heroku ( The name you provided in the previous step)
Key HEROKU_PRODUCTION_KEY Value - The Key copied in the earlier step
We are using this key in .gitlab-ci.yml
All done to kick start the build
Now, you can either change a file or trigger the pipeline by navigating to CI/CD --> Pipelines --> Run pipeline
As you see above the pipeline's build, test and deploy stages are passing. Now let's verify if the artefact deployed at Heroku.
To verify, navigate to your app at Heroku, you can see the activities log.
TL;DR
What's Next Step ?
What we have seen here is the simplest form of deployment. And we scratched the surface. Real-time apps are complex and need a sophisticated configuration.
Docker, Kubernetes and Terraform are powering the production-grade apps.
We will discuss them in the coming weeks.