The script automates the following in order to demo a CI/CD workflow for deploying Mule apps into CloudHub.
- Creation of a Private Github repository based on the Anypoint project directory provided
- Generates and configures Github deploy keys, used for communication with Jenkins.
- Creates and configures a Jenkins container image that is used to listen to a configured GitHub repository for deployments to CloudHub.
In order to run the script you will need the following:
- Anypoint Studio and Platform Account
- Docker Desktop
- Git
- Github CLI and Github Personal Access Token
- The files in this repo
- Create a new Anypoint Studio Project
- Install Git and Github CLI
- Run
brew install git gh
- Run
- Github Personal Access Token
-
See Creating a personal access token for additional informaiton.
- Set and select the following for your Personal Access Token:
- Note: NAME_YOUR_TOKEN
- Expiration: SET_DURATION
- Select the repo checkbox
- Select the delete_repo checkbox
- Click Generate token
- Copy the generated token and set it as an environment variable on your system
echo GITHUB_TOKEN={YOUR_TOKEN} >> ~/.bashrc
or~/.zshrc
- Run
source ~/.basrc
orsource ~/.zshrc
to update current terminal session.
Alternative Method
- As an alternative you can use
gh auth login
to create a temporary token. - To run the cleanup script you will need you will need to run
gh auth refresh -h github.com -s delete_repo
in order to add delete repository capabilities.
- Set and select the following for your Personal Access Token:
-
- Install Docker Desktop
Before runing the script you will need to have your GITHUB_TOKEN
set. See Github Personal Access Token or Alternative Method.
The cicd-demo.sh
script takes three arguments. The first is the path to your Mule app (e.g. ~/AnypointStudio/studio-workspace/ci-cd-sample
). Second is your Github username. Third is any name, preferably your dockerhub username as this name is used to create the container image. e.g. jasoria/jenkins:demo
).
Run the script as follows:
bash cicd-demo.sh ${PATH_TO_MULE_PROJECT} ${GITHUB_USERNAME} ${DOCKERHUB_USERNAME}
NOTE: You will need to have your
pom.xml
configured to deploy into CloudHub. See Configure the CloudHub Deployment Strategy for more information. See example-pom.xml or snippet below.Click here to see pom.xml snippet
<plugin> <groupId>org.mule.tools.maven</groupId> <artifactId>mule-maven-plugin</artifactId> <version>${mule.maven.plugin.version}</version> <extensions>true</extensions> <configuration> <cloudHubDeployment> <uri>https://anypoint.mulesoft.com</uri> <muleVersion>${muleVersion}</muleVersion> <username>${username}</username> <password>${password}</password> <applicationName>${appName}</applicationName> <environment>${environment}</environment> <region>${region}</region> <workers>${workers}</workers> <workerType>${workerType}</workerType> </cloudHubDeployment> </configuration> </plugin>NOTE: If using a trial account, Region and Workers (Quantity of workers) can be omitted and workerType will be MICRO. See example-pom.xml for a complete file.
This step is optional depending on preference as you can hard code variables in the
mvn deploy
command used later.
- Open your browser to http://localhost:8080 and login to Jenkins using
admin
andpassword
. - From the Dashboard select Manage Jenkins
- Select Configure System
- In Configure System page scroll down to the Global properties section and check Environment variables.
- Under List of variables select Add and create a USERNAME and PASSWORD variables.
- Click Save to apply the changes.
-
Open your browser to http://localhost:8080 and login to Jenkins using
admin
andpassword
. -
Enter an item name, e.g. ci-cd-demo and select Freestyle project
-
In the Configuration screen input the following information:
- a. In the General section select Github project
- b. Enter the GitHub repository created by the script. e.g. Your project directory:
~/AnypointStudio/studio-workspace/ci-cd-sample
, Created GitHub repository:[email protected]:soriaj/ci-cd-sample.git
into the Project url section.
- In the Source Code Management section select Git and enter the same repository into Repository URL.
- In the Credentials section select jenkins from the drop down.
- Under the Branches to build section enter the Branch Specifier to match your GitHub branch. e.g.
master
ormain
. - In the Build Triggers section select Poll SCM and configure a cron schedule to run every minute. Enter
* * * * *
in the Schedule dialog box. - In the Build Steps section click the Add build step drop down and select Execute shell.
- Add the following to the Command dialog box:
mvn clean package deploy -DmuleDeploy -Dusername=$USERNAME -Dpassword=$PASSWORD -DmuleVersion=4.4.0 -Denvironment=Sandbox -DappName=$REPLACE_WITH_UNIQUE_APP_NAME -DworkerType=MICRO
NOTE: If you did not configure environment variables you can hardcode your username and password where required.
11. Select Save to save the job and return to the Dashboard as see the Build History project running or click Build Now.
The cleanup script will remove the running container, delete the created container image and prompt to delete the created Github Repo.
NOTE:
- If you used the Alternative Method for creating a
GITHUB_TOKEN
you need to refresh your token to allowdelete_repo
permissions prior to running the script. Rungh auth refresh -h github.com -s delete_repo
to update your token.- You will need to manually delete the CloudHub application. Login to Anypoint Platform
- Your project directory will not be deleted but
.git
will be removed from the directory.
To run the cleanup script you need to pass three arguements. The first is the path to your Mule app (e.g. ~/AnypointStudio/studio-workspace/ci-cd-sample
). Second, your GitHub username. Third the username used to create the container image.
Run:
bash cleanup.sh ${PATH_TO_MULE_PROJECT} ${GITHUB_USERNAME} ${DOCKERHUB_USERNAME}
When prompted enter the GitHub repository name to confirm deletion.