Guest User

Untitled

a guest
Jan 23rd, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. # CircleCI Convert
  2.  
  3. A quick and dirty conversion tool for CircleCI 1.0 to CircleCI 2.0 configurations.
  4.  
  5. ## Introduction
  6.  
  7. This tool will **help** with the conversion of CircleCI 1.0 configurations to CircleCI 2.0.
  8.  
  9. What this tool will *not* do is create workflows. This is left at the discression of the user.
  10.  
  11. ## Requirements
  12.  
  13. * **ruby 2.3.7p456** - tested with the built-in ruby on **macOS** Mojave (10.14.2)
  14. * **circle.rb** - the ruby code for the conversion tool some place you can reference it
  15. * **circle.yml** - a CircleCI 1.0 yaml configuration
  16. * an editor to look at and validate the resulting `config.yml` file
  17.  
  18. ## Quick Start
  19.  
  20. Assuming you have a project in ./project with the following typical structure:
  21.  
  22. ```
  23. ./circle.rb - The circleci-convert.rb code
  24. ./project - Your Project Directory
  25. ./project/circle.yml - The original circle.yml file
  26. ./project/... - The rest of your project code
  27. ```
  28.  
  29. To convert this project, we need to do a couple things.
  30.  
  31. First, create a directory `.circleci` in your project;
  32.  
  33. ```
  34. # cd project
  35. # mkdir -p ./circleci
  36. ```
  37.  
  38. Lets move the existing circleci config to something that circle won't try to read;
  39.  
  40. ```
  41. # mv circle.yml old-circle.yml
  42. ```
  43.  
  44. And now, give the conversion tool a run with;
  45.  
  46. ```
  47. # ruby ../circle.rb old-circle.yml > .circle/config.yml
  48. ```
  49.  
  50. At this point, you will have modified your project to a CircleCI 2.0 acceptable configuration. Now would be a good time to use your prefered code editor and review the changes of `old-circle.yml` compared to `.cirlceci/config.yml` and start to look through for some of the possible issues that the conversion tool doesn't account for.
  51.  
  52. For the most part, you could check in your code in a branch and see that the CircleCI tests will run as 2.0.
  53.  
  54. ## Common Issues
  55.  
  56. ### ENV Variables with interpolation
  57.  
  58. In some configurations you will have something like the following in your original `circle.yml`
  59.  
  60. ```
  61. machine:
  62. node:
  63. version: 6.9.1
  64. environment:
  65. PROJECT_VERSION: ${CIRCLE_SHA1}
  66. DOCKER_NAME: myproject-container
  67. DEPLOY_NAME: myproject-app
  68. SSH_KEY: -i ${HOME}/.ssh/mysecretkey_id_rsa.pub
  69. services:
  70. - docker
  71.  
  72. **[- snip -]**
  73. ```
  74.  
  75. running the conversion tool will result in the following yaml:
  76.  
  77. ```
  78. ---
  79. version: 2
  80. jobs:
  81. build:
  82. docker:
  83. - image: circleci/node:6.9.1
  84. environment:
  85. PROJECT_VERSION: "${CIRCLE_SHA1}"
  86. DOCKER_NAME: myproject-container
  87. DEPLOY_NAME: myproject-app
  88. SSH_KEY: "-i ${HOME}/.ssh/mysecretkey_id_rsa.pub"
  89. steps:
  90. - setup_remote_docker
  91. - add_ssh_keys
  92. - checkout
  93. - run:
  94. command: 'echo FIXME: please add some command for dependency resolution'
  95. - run:
  96. command: 'echo FIXME: please add some command for compilation'
  97. - run:
  98. command: 'echo FIXME: please add some command for testing'
  99. ```
  100.  
  101. Unfortunatly, CircleCI 2.0 does not support interpolation of job environment variables. For example, `PROJECT_VERSION` in the job shell steps will actually contain `"${CIRCLE_SHA1}"` and **NOT** contain the contents of the `CIRCLE_SHA1` environment variable.
  102.  
  103. The fix for this is to edit the `.config.yml` to the following:
  104.  
  105. ```
  106. ---
  107. version: 2
  108. jobs:
  109. build:
  110. docker:
  111. - image: circleci/node:6.9.1
  112. environment:
  113. DOCKER_NAME: myproject-container
  114. DEPLOY_NAME: myproject-app
  115. steps:
  116. - setup_remote_docker
  117. - add_ssh_keys
  118. - run:
  119. name: Setup Environment Variables - From CircleCI docs
  120. command: |-
  121. echo 'export PROJECT_VERSION="$CIRCLE_SHA1"' >> $BASH_ENV
  122. echo 'export SSH_KEY="-i ${HOME}/.ssh/mysecretkey_id_rsa.pub"' >> $BASH_ENV
  123. - checkout
  124. - run:
  125. command: 'echo FIXME: please add some command for dependency resolution'
  126. - run:
  127. command: 'echo FIXME: please add some command for compilation'
  128. - run:
  129. command: 'echo FIXME: please add some command for testing'
  130.  
  131. ```
  132.  
  133. Key things to notice here;
  134.  
  135. - I've moved the two environment variables `PROJECT_VERSION` and `SSH_KEY` to an "early" run step where I `echo` the assignments into an environment script that is executed before each run step which is contained in the ENV var `$BASH_ENV`.
  136. - I've also removed the references to those variables in the build - environment section.
  137.  
  138.  
  139. ### SSH Keys
  140.  
  141. In some cases, project deployments require SSH keys that have been added to the CircleCI project environments. The convert script detects the most simple usage of SSH keys, however, if your project fails to deploy with complaints about missing keys, you may need to add the build step `add_ssh_keys` early in the build job.
  142.  
  143. ### GCloud docker push - `/var/run/docker.sock` doesn't exist
  144.  
  145. In the case where your project is using GCLOUD to push a DOCKER image to the GCloud Container Respository, you may find that the `gcloud docker -- push` step in circleci will error complainging that `/var/run/docker.sock` doesn't exist. This is because CircleCI 2.0 is building your project inside of a docker container and connects to a remote docker host. You will need to fix with the following:
  146.  
  147. In your original `circle.yml`, this step probably looks like this:
  148. ```
  149. - sudo gcloud docker -- push gcr.io/myorg/${DOCKER_NAME}:${PROJECT_VERSION}-${CIRCLE_BRANCH}
  150. - sudo gcloud docker -- push gcr.io/myorg/${DOCKER_NAME}:latest-${CIRCLE_BRANCH}
  151. ```
  152.  
  153. The conversion tool will convert that to the following:
  154.  
  155. ```
  156. - run:
  157. command: sudo /opt/google-cloud-sdk/bin/gcloud docker -- push gcr.io/myorg/${DOCKER_NAME}:${PROJECT_VERSION}-${CIRCLE_BRANCH}
  158. - run:
  159. command: sudo /opt/google-cloud-sdk/bin/gcloud docker -- push gcr.io/myorg/${DOCKER_NAME}:latest-${CIRCLE_BRANCH}
  160. ```
  161.  
  162. You will need to EDIT that to the following:
  163.  
  164. ```
  165. - run:
  166. command: sudo /opt/google-cloud-sdk/bin/gcloud docker --docker-host=$DOCKER_HOST -- --tlsverify --tlscacert $DOCKER_CERT_PATH/ca.pem --tlscert $DOCKER_CERT_PATH/cert.pem --tlskey $DOCKER_CERT_PATH/key.pem push gcr.io/myorg/${DOCKER_NAME}:${PROJECT_VERSION}-${CIRCLE_BRANCH}
  167. - run:
  168. command: sudo /opt/google-cloud-sdk/bin/gcloud docker --docker-host=$DOCKER_HOST -- --tlsverify --tlscacert $DOCKER_CERT_PATH/ca.pem --tlscert $DOCKER_CERT_PATH/cert.pem --tlskey $DOCKER_CERT_PATH/key.pem push gcr.io/myorg/${DOCKER_NAME}:latest-${CIRCLE_BRANCH}
  169. ```
  170.  
  171. If you want to go one step further and change this to a single runstep, you can do the following:
  172.  
  173. ```
  174. - run:
  175. command: |-
  176. sudo /opt/google-cloud-sdk/bin/gcloud docker --docker-host=$DOCKER_HOST -- --tlsverify --tlscacert $DOCKER_CERT_PATH/ca.pem --tlscert $DOCKER_CERT_PATH/cert.pem --tlskey $DOCKER_CERT_PATH/key.pem push gcr.io/myorg/${DOCKER_NAME}:${PROJECT_VERSION}-${CIRCLE_BRANCH}
  177. sudo /opt/google-cloud-sdk/bin/gcloud docker --docker-host=$DOCKER_HOST -- --tlsverify --tlscacert $DOCKER_CERT_PATH/ca.pem --tlscert $DOCKER_CERT_PATH/cert.pem --tlskey $DOCKER_CERT_PATH/key.pem push gcr.io/myorg/${DOCKER_NAME}:latest-${CIRCLE_BRANCH}
  178. ```
  179.  
  180. ## CAVEATS
  181.  
  182. 1. Messy, hard to read `config.yml`
  183. Very messy but it is a good starting place for your new circleci workflow.
  184. 2. Does not create Workflows
  185. We may fix this in future revisions. Technically, it creates a workflow with one job and no workflow definition.
  186. 3. Deployment step is a big IF ELSE
  187. We may fix this in future revisions.
Add Comment
Please, Sign In to add comment