Author: Ashish Jha
What is CI/CD
The acronym CI/CD stands for Continuous Integration and Continuous Deployment/Continuous Delivery.
Continuous Integration refers to the practice where developers work on different git branches and constantly merge the changed code to a central repository.
The code is merged to a working branch, where automated workflows are used to build and test the code.
This makes sure that all the merged codes are together functioning properly and are well tested.
Continuous Deployment refers to the automated process of releasing the merged and tested code from the central repository to the production environment, where users/customers can use it.
What is GitHub Actions
GitHub Actions helps in automation of tasks within the software development life cycle. GitHub Actions are event-driven, which means that it can run a series of commands after a specified event has occurred. For example, every time someone pushes a code change to a repository, it can automatically run a command that builds and deploys the code .
Prerequisites
- GitHub account and knowledge of git. If you do not have an account you can create it here: https://github.com/
- Git installed in the system
- Anypoint Platform account
- Anypoint Studio
Let’s Start with setting up the Mule application
Open the project and go to the pom.xml file.
Add the deployment configuration in the Mule-Maven-Plugin like the screenshot below:
<configuration>
<cloudHubDeployment>
<uri>https://anypoint.mulesoft.com</uri>
<muleVersion>4.3.0</muleVersion>
<username>${anypoint.username}</username>
<password>${anypoint.password}</password>
<applicationName>mule-app-cicd-github</applicationName>
<environment>Sandbox</environment>
<workerType>MICRO</workerType>
<region>us-east-2</region>
<workers>1</workers>
<objectStoreV2>true</objectStoreV2>
</cloudHubDeployment>
</configuration>
Important:
- The muleVersion should be the same as that of used in the project and should be supported by CloudHub in Runtime Manager.
- Application name should be unique.
- objectStorev2 should be set to “true”.
Push the project to the GitHub repository
Step 1: Sign in to your GitHub account and create a repository.
Step 2: Now go to the project location in your system and open the Command Prompt there and initialize the git repository using command : git init
Step 3: Add the files to the git repository using the command: git add .
Step 4: Commit the changes to the git repository using the command:
git commit -m “Initial commit”.
Step 5: Change the branch to main with command: git branch -M main
Step 6: Add remote origin with command git remote add origin https://github.com/<your GitHub username>/<your repository name>.git
Eg: git remote add origin https://github.com/API1619/Demo-app.git
Step 7: Push the project to the github repository using the command: git push -u origin main
Refresh you git repository and check the uploaded project
Setting up the workflow in GitHub
Step 1: Click the actions tab in the repository.
Step 2: On the new page click the “Set up this workflow button”.
Step 3: In the workflow file copy and paste the code from the following link:
https://github.com/API1619/Demo-app/blob/main/.github/workflows/build-and-deploy-on-push.yml
This workflow will first build the MuleSoft project and deploy it to CloudHub. This workflow contains two jobs:
- To build the application.
- To deploy the application to CloudHub.
The Workflow is triggered by a push on the main branch.
For other branches in GitHub and for pull request trigger, add the branch name as shown following:
Build Job
- The Java environment for building the application would be set up because MuleSoft is based on java.
- In the next step packaging will create an output jar file which will be used to deploy the application.
- Every artifact will be stamped with the commit hash. So, we can find which commit is being deployed.
- In the last step of the build job, the build artifact will be uploaded. Which will allow the build and deploy job to share the data between them.
- You can find the artifact file in your repository in the Actions tab after the job is completed.
Deploy Job
- The deploy job will first check the repository and will retrieve all the related dependencies.
- In the second step it will download the artifact file uploaded by the build job.
- In the third and final it will retrieve the Anypoint platform’s username and password from the Secrets to deploy the application to the CloudHub.
Deploy Job will only execute if the Build job is successfully completed.
Give a suitable name to the file and click the “Start commit” button.
Click the “Commit new file” button.
Adding the Anypoint Platform Username and Password as Secret
Step 1: Click on the Settings tab in the repository.
Step 2: On the settings page click the Secrets from the left-side and then click the “New repository secret” button.
Step 3: In the new section that opens Keep the Name field as “USERNAME” and in the Value field provide your Anypoint Platform username and click the “Add secret” button.
Step 4: Click the “New repository secret” button again and in the new section that opens Keep the Name field as “PASSWORD” and in the Value field provide your Anypoint Platform PASSWORD and click the “Add secret” button.
Now check both the secrets are available in the “Repository secrets” section.
Deploying Application through Actions
Step 1: Open your project and make any one minor change (to mock a change in the application).
Step 2: Open the command prompt at the project location and in the command prompt run the command: git pull
Step 3: Now run : git add
Step 4: Do a commit with command : git commit -m “<Your comment”>
Step 5: Push the code with the command : git push -u origin main
Step 6: Go back to your GitHub repository and open the Actions tab and your job should have started.
Step 7: Click on the job and see the logs.
Step 8: Click on the Actions tab again and see that both Build and Deploy jobs have a green tick, indicating successful build and deployment. Also check the Artifact after the job is completed.
Step 9: Sign in to your Anypoint Platform account and go to Runtime Manager. You can see that your app has been deployed and started.
Step 10: Click on your deployed app in Runtime Manager and check the details.